alann-maulana / flutter_beacon

An hybrid iBeacon scanner and transmitter SDK for Flutter Android and iOS.
Apache License 2.0
118 stars 146 forks source link

Not working on Android #97

Open brunoshine opened 3 years ago

brunoshine commented 3 years ago

Hi,

I've created a simple app, that works on iOS device (ranging and monitoring events are triggered) but does not work on android device, as beacons are active, monitoring does not get called (no logs) and ranging reports empty beacons array.

Any thoughts on what might be happening? thanks.

iOS result:

flutter: ranging:
flutter: {"region":{"identifier":"escritorio","proximityUUID":"8F191C34-EC29-4DA1-8EAA-5BF43F5E5D94"},"beacons":[]}
flutter: ranging:
flutter: {"region":{"identifier":"sala","proximityUUID":"B5B4D5A0-8DFB-4552-A139-71D47EA79CC3"},"beacons":[]}
flutter: ranging:
flutter: {"region":{"identifier":"cozinha","proximityUUID":"B9407F30-F5F8-466E-AFF9-25556B57FE6D"},"beacons":[{"proximityUUID":"B9407F30-F5F8-466E-AFF9-25556B57FE6D","major":100,"minor":2,"rssi":-45,"accuracy":0.09,"proximity":"immediate"},{"proximityUUID":"B9407F30-F5F8-466E-AFF9-25556B57FE6D","major":100,"minor":1,"rssi":-88,"accuracy":0.17,"proximity":"immediate"}]}

Android result:

I/flutter ( 5402): {"region":{"identifier":"escritorio","proximityUUID":"8f191c34-ec29-4da1-8eaa-5bf43f5e5d94"},"beacons":[]}
I/flutter ( 5402): ranging:
I/flutter ( 5402): {"region":{"identifier":"sala","proximityUUID":"b5b4d5a0-8dfb-4552-a139-71d47ea79cc3"},"beacons":[]}
I/flutter ( 5402): ranging:
I/flutter ( 5402): {"region":{"identifier":"cozinha","proximityUUID":"b9407f30-f5f8-466e-aff9-25556b57fe6d"},"beacons":[]}

code:

void startBLE() async {
    try {
      // or if you want to include automatic checking permission
      await flutterBeacon.initializeAndCheckScanning;

      final regions = <Region>[];

        regions.add(Region(
            identifier: 'escritorio',
            proximityUUID: '8f191c34-ec29-4da1-8eaa-5bf43f5e5d94'));
        regions.add(Region(
            identifier: 'sala',
            proximityUUID: 'b5b4d5a0-8dfb-4552-a139-71d47ea79cc3'));
        regions.add(Region(
            identifier: 'cozinha',
            proximityUUID: 'b9407f30-f5f8-466e-aff9-25556b57fe6d'));

      // to start ranging beacons
      var _streamRanging =
          flutterBeacon.ranging(regions).listen((RangingResult result) {
        // result contains a region and list of beacons found
        // list can be empty if no matching beacons were found in range
        print("ranging:");
        print(result);
      });

      // to start monitoring beacons
      var _streamMonitoring =
          // result contains a region, event type and event state
          flutterBeacon.monitoring(regions).listen((MonitoringResult result) {
        print("monitoring:");
        print(result.toJson);
      });

      // // to stop monitoring beacons
      // _streamMonitoring.cancel();
    } on PlatformException catch (e) {
      print(e);
    }
  }
dariocavada commented 3 years ago

I had the same problem and solved using uppercase UUID.

6a84c716-0f2a-1ce9-f210-6a63bd873dd9 --> this does not work.

6A84C716-0F2A-1CE9-F210-6A63BD873DD9 --> this works !

brunoshine commented 3 years ago

Hi @dariocavada thank you for your reply but replacing for uppercase did not worked for me. Still not showing the beacons of the raging event nor on the monitoring. :/

s-h-x commented 3 years ago

Can you try to (manually) add Location permission to "ALWAYS" for your app ?

BEFORE : no beacon when ranging (normal location permission dialog does not show the "Always" setting in my case, but only "when in use") AFTER after manually setting this : got my beacon in the ranging results.

It seems that using very recent versions of SDK, using flutter_beacons requires the location permission at "ALWAYS" level and and those permissions in manifest :

<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION"/>

1 year ago -don't ask me which version I was using back then- I did not have this issue I'm just using this app in a Demo, so this is no big deal for me, but I guess a number of users other than you and me have met this limitation ?

The SDK11 is a little ambiguous regarding how to properly get background location setting to work but I guess in the case of beaconning this seems to be mandatory. see https://developer.android.com/training/location/background for details

Hope this helps !

dariocavada commented 3 years ago

I have the same error with the targetSdkVersion 30, but if I use the version 28 it works...

Unfortunately The Play Store wants the targetSdkVersion 30 ...


//compileSdkVersion 30
    compileSdkVersion 28

    sourceSets {
        main.java.srcDirs += 'src/main/kotlin'
    }

    defaultConfig {
        // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
        applicationId "com.example.flutter_beacon_example"
        minSdkVersion 18
        //targetSdkVersion 30
        targetSdkVersion 28
        versionCode flutterVersionCode.toInteger()
        versionName flutterVersionName
    }
dariocavada commented 3 years ago

SOLVED:

as mentioned here: #

https://altbeacon.github.io/android-beacon-library/

for version 30 (Android 10, 11) is necessary to add manually ACCESS_FINE_LOCATION

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

or if you want also background scanning:

<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
echb commented 1 year ago

@brunoshine i have the same code as you (with my uuid of course) but i cannot get the rssi, it always return 0

[{"proximityUUID":"253623D3-D090-B787-1744-DDFE8B6F6D6B","major":15,"minor":1,"rssi":0,"accuracy":-1.0,"proximity":"unknown"}]

version flutter_beacon: ^0.5.1 i followed as the example (https://pub.dev/packages/flutter_beacon), am i missing a permission?

  <!-- When in use -->
  <key>NSLocationWhenInUseUsageDescription</key>
  <string>Reason why app needs location</string>
  <!-- Always -->
  <!-- for iOS 11 + -->
  <key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
  <string>Reason why app needs location</string>
  <!-- for iOS 9/10 -->
  <key>NSLocationAlwaysUsageDescription</key>
  <string>Reason why app needs location</string>
  <!-- Bluetooth Privacy -->
  <!-- for iOS 13 + -->
  <key>NSBluetoothAlwaysUsageDescription</key>
  <string>Reason why app needs bluetooth</string>

any idea?