2

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.

Choo
  • 21
  • 2

1 Answers1

2

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.

davidgyoung
  • 63,876
  • 14
  • 121
  • 204
  • Thanks, will explore foreground service. Followup question: Is it possible for set a time based AlarmManager to start a foreground service scanning for Beacon, if i do not want the icon to appear 24/7? example, if the user know he will typically pass by the location at around 8 to 9 AM everyday, can I have an alarm manager that starts the foreground service at 8, and stops the foreground at 9? – Choo Mar 01 '18 at 15:05
  • For Android 8, AlarmManager does not work reliably anymore, but you can use the JobScheduler to do the same thing. – davidgyoung Mar 01 '18 at 17:37
  • As suggested, I set up a Foreground Service implementing BeaconConsumer. At onStartCommand (which returns START_STICKY), I set up beaconManager with beaconManager.setBackgroundMode(false). However the responsiveness is still the same as before, that is if the screen is on, detection is within seconds; once the screen is off, responsiveness drops. Any idea how I can make the beacon monitoring be as responsive even when screen is off? Thanks – Choo Mar 05 '18 at 07:54
  • See my edit on scanning with the screen off in Android 8.1+ – davidgyoung Mar 05 '18 at 22:29
  • Thanks, @davidgyoung, this explains the behavior I am observing using a background service. – J E Carter II Oct 15 '19 at 16:12