Beacon scanning in background - Android O
Asked Answered
M

1

6

Google added restrictions of not broadcasting the Bluetooth switch states to the app when the app in the background. This effectively blocks the optimized Bluetooth beacon scanning in the background. How to get around this issue other than periodic job scheduling?

Any help is appreciated.

Meroblastic answered 16/7, 2018 at 23:40 Comment(0)
H
13

You have several options for background BLE Beacon Scanning on Android 8+

  1. A regular background service. You can use these just like on Android 4.3-6.x, but you are generally limited to 10 minutes of running time in the background, after which time Android will kill your app and it won't be able to scan anymore.

  2. Use a foreground service. These work much like Android background services except they display an even-present notification with an icon of your choice to indicate that your app is running in the background. With a foreground service, you can effectively scan for beacons in the background with no restrictions just like on Android 4.3-6.x.

  3. Use Intent-based scans. If you simply need to know when a beacon appears or disappears, you can set up an Intent-based scan for BLE devices with a bluetooth packet filter that filters on the presence of the byte pattern of the beacon, or the absence of the byte pattern of the beacon. When the beacon appears or disappears, Android will send an Intent to a BroadcastReceiver in your app that will wake it up in the background and let it run for about 10 minutes before killing it. During this time you can keep scanning for beacons.

  4. Use the job scheduler (also known as WorkManager). You can schedule a job to run at most every ~15 minutes in the background to do scanning. A job is generally limited to 10 minutes of running time in the background. Since start times vary by +/- 10 minutes, you'll have gaps of up to 0-15 minutes where you won't be scanning.

  5. Play games with (3) and (4) to bend the rules. While this goes against the spirit of Android's restrictions, you can play games with the job scheduler by starting an immediate job, cancelling it before 10 minutes is up, then restarting it. You can do similar things with an intent based scan by simply triggering it over and over. Be forewarned, however, doing these things will drain the users' batteries, perhaps leading them to uninstall your app. This rule bending may be blocked in future Android releases.

You can read my blog post about the merits of these techniques here. The open source Android Beacon Library uses techniques 3 and 4 on Android 8+ devices by default, and also supports configuring a foreground service if you wish to choose option 2.

Hylton answered 17/7, 2018 at 2:25 Comment(6)
The option number 4 looks good as long as the app doesn't need to maintain any service or scheduling. I checked your blog page in this regard as well. Tried a bit with the intent based scan. Even this is not working after 10 minutes of the app on the foreground.Meroblastic
The library's implementation of job scheduler simply schedules a periodic job to run every 15 minutes in the background and scan for beacons. There is no other service. Between these 15 minutes scans it uses option 3, so if a beacon shows up all of a sudden you don't have to wait for the next 15 minute cycle for detection.Hylton
Ok - Got it. Thanks.Meroblastic
Though I am looking to avoid this periodic scheduling. Doing this without having the knowledge of whether the Bluetooth is on or off can be a overkill, unfortunately.Meroblastic
If you immediately exit a job scheduled once every 15 minute job if Bluetooth is off, then system resource usage will be minimal. It may seem like a lot of repeated busy work, but computers don't really mind. :)Hylton
#3 the intent based sounds best and easiest but it is not reliable as system will often not call BroadcastReceiver at all (for example when users swipes-up in app selector to close the app).Mairamaire

© 2022 - 2024 — McMap. All rights reserved.