umair13adil / simple_beacons_flutter

A flutter plugin project to range & monitor iBeacons.
Apache License 2.0
35 stars 42 forks source link

Background mode - iOS #37

Open kmigntw opened 3 years ago

kmigntw commented 3 years ago

Hi, when the app is in the background the library scans the beacons for about 15 seconds and then it stops. If I resume the app and put it in the background immediatly the library starts scanning for 15 seconds again and stops. How to increate the scanning to at least few minutes? Here is the code I use to listen for new scanned beacons.

 final StreamController<String> _beaconEventsController =
      StreamController<String>.broadcast();

    try {
      BeaconsPlugin.listenToBeacons(_beaconEventsController);

      await BeaconsPlugin.addRegion("Center", BEACON_UUID);

      _beaconEventsController.stream.listen(
          (data) async {
            await checkAllRequirements();
            if (data.isNotEmpty) {
              print('SCANNING');
              BeaconPayload beaconPayload = BeaconPayload.fromJson(data);
            }
          },
          onDone: () {},
          onError: (error) {
            print("Error: $error");
          });

      await BeaconsPlugin.runInBackground(true);

      await startMonitoring();
    } on PlatformException catch (e) {
      print(e.toString());
    }
kmigntw commented 3 years ago

Any advice or answer?

PetrosPoll commented 3 years ago

Hello! For me isn't working. I got the example that has this package on pub.dev. Your code working ? Can you send me fll of code to try it if working for me correctly ?

Thank you!

cloudbreakstudios commented 3 years ago

Your issue maybe related to this (i believe this package uses this beacon library!).

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

Android 8.0+ Scan Limitations Android versions 8+ restrict background processes to run for at most 15 minutes after an app was last in the foreground. Beyond this, the JobScheduler must be used to execute periodic tasks like beacon scanning, which are limited to running once every 15 minutes. This means that any betweenScanPeriod configured for < 15 minutes will be extended on Android 8 devices to only happen once every 15 minutes. Further, because Android may stagger these jobs, the actual time between scans may be 10-25 minutes on Android 8.

This does not mean that detections will take that long, as low-power scans as described in the fast detections section described above will still be active.

If you use case needs frequent scanning in the background, you can configure the library to use a Foreground Service.

I have also noticed that this package has hardcoded two properties in BeaconHelper.kt which would be good if they were configurable.

beaconManager?.backgroundBetweenScanPeriod = 0 beaconManager?.backgroundScanPeriod = 1100

For my use case I need to be able to monitor for beacons (a quick scan every 30 seconds) for nearby iBeacons. The beacons transmit for 1 minute. I need to do this whilst the app is both in the background and foreground. The foreground part works fine, but like above i think the background implementation may not be 100% Has anyone achieve similar with this package?