Search code examples
androidkotlinbeaconaltbeacon

altbeacon [2.19]+ how to off autobind in startRangingBeacons(region) and stopRangingBeacons(region)


I've been using this library since the "2.18" version.

I had to make the beacon scan work in the background, and I used the foreground service to do so. But the battery problem was serious. Although the scan cycle was set, beacons were often missed. So I turned it on and on clearly using the functions of "startRangingBeaconsInRegion" and "stopRangingBeaconsInRegion".

And only now have I started migration to the latest version,and that latest version makes me difficult now. This is because of the "autobind" function used in the start and stop. It kills and creates foreground services. The alarm keeps ringing whenever the "startRangingBeacons(region)" function is used after using the "stopRangingBeacons(region)" function.

Is there a way to turn on/off only scan? or Is there a way to turn off the autobind?

Please Tell me.

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


Solution

  • The AndroidBeaconLibrary allows for BLE beacon scanning in the background including a number of techniques, including the optional use of an Android "foreground service" to allow extended indefinite scanning in the background. The library provides a built in foreground service to make this easy to do. When configured, the foreground service works this way:

    1. Whenever you start ranging or monitoring at least one region, the foreground service will start, and a notification will be displayed on the phone indicating the scanning service is running.
    2. Whenever you stop ranging and monitoring all regions, the foreground service will stop, and the notification will disappear.

    The OP asks about a situation where the app wants to repeatedly start and stop ranging beacons, but doesn't want the foreground service notification to blink on and off. Library versions prior to 2.19 made this possible with manual service binding, but manual service binding is deprecated in 2.19+ because it is a common source of errors, crashes and developer frustration.

    To achieve the same effect with autobind ranging on library version 2.19+, you have several options:

    1. Set beaconManager.setIntentScanningStrategyEnabled(true) which will use background intent-based scans instead of a foreground service. See here With this option, a foreground service notification is never shown by the library (if you want one, you can build your own foreground service). You may find, however, that this burns too much battery as it will scan continually.

    2. Don't ever call stopRangingBeacons(region) at all. Instead, change the between scan period to something very large like this: beaconManager.setBackgroundBetweenScanPeriod(360000000L) /* 10 hours*/ followed by beaconManager.updateScanPeriods(). Then when you want to resume scanning, set this back to its normal value.

    3. Build your own foreground service so you have full control over the appearance and disappearance of the notification. Share the same notification with the library's foreground service, so even if the library's foreground service stops, your custom foreground service keeps showing the notification.