Responsiveness of Background Beacon Monitoring (An

2020-04-14 07:22发布

问题:

Background:

I am developing an Android app that monitors for specific beacons in the background, and upon entry detection, will perform custom coding. I am currently exploring this using Android Beacon Library, and is able to detect the beacon responsively most of the time when the screen is On (within seconds).

However, when the screen is off, the responsiveness drops and may take minutes to half and hour to detect.

I am wondering if there are ways to improve responsiveness as my usage scenario requires a responsive background detection without having to turn on the phone (looking at a reliable < 1 minute response time).

I am using Android 8 to test at the moment, but will need to support Android 6+ eventually.

Specific questions:

1) Using Android Beacon Library, I am experimenting with the reference app with the line "new BackgroundPowerSaver(this)". Does removing this line improve responsiveness (in Android 5-7, Android 8) at a cost of more power usage?

2) When I tried to remove the "new BackgroundPowerSaver(this)" line, the app stop working (unable to monitor, nor do ranging). I have added Bluetooth and fine-locations permissions in my manifest. How do I make this work without the backgroundpowersaver?

3) Besides Android Beacon Library, I have yet to explore other SDKs such as those from Kontakt and Estimote. Are these SDKs able to support a more responsive background monitoring compared to Android Beacon Library?

4) Besides Android Beacon Library, if I use Android bluetooth APIs directly, possible to get achieve the responsiveness required in background? Any reference applications?

Thanks.

回答1:

BackgroundPowerSaver automatically switches between constant scanning (foreground) and duty cycle scanning (background). If you remove it, you will end up with constant scanning all the time which will use much more power on Android 4.3-7.x.

Android 8, however has new restrictions that block doing this. The OS limits background apps to running no longer than 10 minutes at a time in the background. This OS restriction exists regardless of which scanning SDK or built-in API you use. The job scheduler on Android 8 allows scheduled scan jobs to be run at most every 15 minutes (which sometimes take longer to run due to OS delays), so that is the limiting factor in how often scans can be run.

I wrote a blog post about this here

If you want to achieve constant background scanning, set up a foreground service with your app. A foreground service shows an icon to users at the top of the screen to let them know it is running, then allows apps to keep running in the background indefinitely, even on Android 8.

EDIT: on Android 8.1+ if the screen is off, scans will be blocked unless you have a filter attached to them. Android Beacon Library 2.13 and higher automatically set up a filter to ensure these scans are not blocked in this case. Make sure you have that version if testing on Android 8.1+ with the screen off.