AltBeacon / android-beacon-library

Allows Android apps to interact with BLE beacons
Apache License 2.0
2.83k stars 834 forks source link

Improve default background mode with autobind #1051

Closed davidgyoung closed 2 years ago

davidgyoung commented 2 years ago

The Problem

The library uses a BackgroundPowerSaver class to track the activity lifecycle to determine if the app is in the foreground or background and apply configured scan periods depending on the background mode. As of version 2.19, direct use of this class is deprecated and instead one is automatically created whenever startMonitoring or startRangingBeacons is first called.

The problem with the above is that we don't know the actual foreground/background state of the app when the user makes these calls. Android provides no APIs to determine the current foreground/background status of the app. The preferred approach to doing this is to start tracking activities with the Application.onCreate() method. But this library cannot guarantee that the first call to start monitoring or ranging (which start the internal BackgroundPowerSaver) are made from Application.onCreate(). They often will not be. So the initial background/foreground mode of the library is often wrong.

Here is how it works as of 2.19:

The Solution

While we cannot exactly determine the foreground/background state on the first call to startMonitoring/startRangingBeacons, we can infer if it is in the background from a few things:

Impact

Based on the above, the library will always start out in the proper mode if ranging/montioring is first started from:

The only times that it may be set wrong will be when it is started from a backgrounded component outside the Application#onCreate call stack. This will often happen I the library starts looking for beacons based on some background event (e.g. a timer, or a signal from a server.). Further, this will only be wrong is if the screen is on at the same time as this happens (e.g. the user is using a different app, has the lock screen or springboard up.)

In the case where the library starts out (incorrectly) in foreground mode, the library will automatically switch to background mode as soon as the screen goes off. As a result, the amount of time in the wrong mode will usually be limited and not have significant battery impact.