On Android, you can manage the bluetooth scanning process through Connecthings' SDK.

For the alert process, when your application is open and in the foreground, high scanning frequency is recommended to ensure quick beacon detection. However, for notification process, when the application is running in the background, the scanning process may cause high battery-consumption, so you may decide to implement one of two configurations:

  • Either detect nearby beacons as quickly as possible to generate a notification
  • Or limit battery impact as much as possible

Up until version 2.4 of our SDK, developers only had control over the bluetooth scanning frequency when the application is running in the background, to optimize battery impact during the beacon scanning process.

Version 2.4 now features a BeaconScanningPeriodManager class, which allows developers to implement different algorithms to manage scanning periods.

The SDK now comes with two BeaconScanningPeriodManager implementation classes:

  • the BeaconScanningPeriodManagerDefault
  • the BeaconScanningPeriodManagerOptimizer

The default implementation

This is the class implemented by default for the BeaconScanningPeriodManager (available in the previous version of our SDK).

When the application is running in the foreground, the SDK does an active bluetooth scan with a high scanning period. When the application is running in the background, the SDK does a smart bluetooth scan with a lower scanning period.

To optimize battery consumption when the application is running in the background, developers have to play with the background scanning parameters:

  • The waiting period: the length of time the SDK waits before launching a bluetooth scan
  • The scanning period: the duration of the beacon scanning period by the SDK

To optimize beacon detection & the notification process, we recommend setting the waiting period to 15-30 seconds and the scanning period to 10-30 seconds.

With this algorithm, and when the application is running in the background, the SDK is actively scanning about 50% of the time. Constant scanning is not appropriate as it will drain users' battery.

The final waiting period and scanning period that you choose depend on your app and the type of interactions that will occur with the beacons.

Initialization

The BeaconScanningPeriodManagerDefault comes with two constructors:

  • BeaconScanningPeriodManagerDefault(Context context, long backgroundBetweenScanPeriod, long backgroundScanPeriod) where:

    • context: is the application context
    • backgroundBetweenScanPeriod: is the time lapse between two scanning periods when the application is running in the background
    • backgroundScanPeriod: is the scanning period when the application is in the background
    • For example:
        new BeaconScanningPeriodManagerDefault(applicationContext, 30 * 1000,  20 *1000); 
      
  • BeaconScanningPeriodManagerDefault(Context context) where the others parameters are initialized as follows:

    • 30 seconds for the backgroundBetweenScanPeriod parameter
    • 10 seconds for the backgroundScanPeriod parameter

The "optimized" implementation

The BeaconScanningPeriodManagerOptimizer class provides additional parameters to optimize the beacon scanning configuration. It contains three settings:

Setting #1:
When the application is running in the background, the SDK actively starts scanning for nearby beacons when the phone's screen is activated, for a limited period of time only (unless a beacon is detected), which is configurable by the developer.

Setting #2:
When the application is running in the background, and one or more beacons are detected, the SDK launches a "super-active" scan until no more beacons are detected (developers can choose the scanning periods for both the super-active scanning period and the lower-intensity one that follows).

Setting #3:
When the application is running in the background, and the phone's screen is off, the SDK waits a longer period of time (configurable) before launching a new scan for nearby beacons.

As a result, an application that uses the BeaconScanningPeriodManagerOptimizer will:

  • rapidly detect beacons every time the phone's screen is activated
  • rapidly detect exits from beacon regions if the phone is already inside an area with beacons
  • optimize scanning (and the phone's battery) when the phone's screen is inactive.

The following diagram gives an overview of the AdtagBeaconScanningPeriodManagerOptimizer lifecycle when an application is running in the background.

Initialization

The BeaconScanningPeriodManagerOptimizer comes with two constructors:

Constructor #1: BeaconScanningPeriodManagerOptimizer(Context context,int maxScanningTime, ScanPeriod activeScanPeriod, ScanPeriod unactiveScanPeriod) where:

  • context: is the application context
    • maxScanningTime: is the maximum scanning duration after a device's screen is turned on or off, if no beacons are detected nearby
    • activeScanPeriod: is the parameters to manage the "active" scanning process with:
      • the corresponding "active" scanning period parameter
      • the corresponding "active" waiting period parameter
    • inactiveScanPeriod: parameters to manage the "inactive" scanning process with:
      • the corresponding "inactive" scanning period parameter
      • the corresponding "inactive" waiting period paramete

For example, it is possible to configure the BeaconScanningPeriodManagerOptimizer class as follows:

  • A 2-minute maximum scanning time, if no beacons are detected after the device's screen is turned on or off
  • The following "active" scanning parameters:
    • a 1100-millisecond scanning period
    • a null waiting period
  • The following "inactive" scanning parameters:
    • a 20-second scanning period
    • a 10-minute waiting period
new AdtagBeaconScanningPeriodManagerOptimizer(this, 2 * 60 * 1000, new ScanPeriod(1100, 0L), new ScanPeriod(20 * 1000, 60 * 10 * 1000)) 

Constructor #2: BeaconScanningPeriodManagerOptimizer(Context context) which initializes all parameters as follows:

  • A 20-second maximum scanning time, if no beacons are detected after the device's screen is turned on or off
  • The following "active" scanning parameters: * a 1100-millisecond scanning period * a null waiting period
  • The following "inactive" scanning parameters: * a 15-second scanning period * a 20-minute waiting period

Which ScanningPeriodManager should you use?

The BeaconScanningPeriodManagerDefault class is great to display notifications even if the phone is not active.

The BeaconScanningPeriodManagerOptimizer class is great for an application that interacts with beacons inside a same zone, like an airport, a museum, a stadium, a park...

The BeaconScanningPeriodManagerOptimizer class allows you to be more efficient in terms of battery consumption.

Initializing the BeaconScanningPeriodManager class and your SDK simultaneously

You just need to initialize the SDK with theBeaconScanningPeriodManager class of your choice in your Application class. For example, you can initialize the SDK with a BeaconScanningPeriodManagerOptimizer class with the following parameters:

AdtagBeaconManager.initInstance(this, UUID_CT_DEV, new AdtagBeaconScanningPeriodManagerOptimizer(this, 2 * 60 * 1000, new ScanPeriod(1100, 0L), new ScanPeriod(20 * 1000, 60 * 5 * 1000 + 100)));

Note:

By default, the SDK is initialized with the BeaconScanningPeriodManagerDefault class.