Advanced : Android adjusting BLE scanning period

Custom monitoring/ranging scan and between scan periods

Since Ubudu Android-SDK v1.8.2 it is possible to adjust scan and between scan periods of the ranging and monitoring tasks. It allows developers to adjust the frequency of BLE scanning and also the period of each scan. There are a total of four methods in UbuduBeaconManager class that handle these settings. Each method has two long type arguments standing for foreground and background period value in milliseconds.

Scan period

Scan period is a period of time of a single BLE scan. Longer scanning period enhances the reception of weak signals coming from distant beacons and the signals of the beacons with low advertising frequency. There are two methods in UbuduBeaconManager class for customizing the scan period:

public void setMonitoringScanPeriods(long foregroundPeriodMillis,long backgroundPeriodMillis);

public void setRangingScanPeriods(long foregroundPeriodMillis,long backgroundPeriodMillis);

Between scan period

Between scan period is a period of time to wait between starting next BLE scan. Setting the longer between scan period is useful to save battery life when we do not need to refresh detected beacons data very often, e.g. in monitoring state. There are two methods in UbuduBeaconManager class for customizing the between scan period:

public void setMonitoringBetweenScanPeriods(long foregroundPeriodMillis,long backgroundPeriodMillis);

public void setRangingBetweenScanPeriods(long foregroundPeriodMillis,long backgroundPeriodMillis);

Difference between monitoring state and ranging state

setMonitoringScanPeriods and setMonitoringBetweenScanPeriods set the scan and between scan periods in foreground and background for monitoring state. Monitoring state is on as long as the SDK does not detect any beacon matching the UUID of the application namespace. In practice one should set the scanning in this state to be less frequent to save battery life.

setRangingScanPeriods and setRangingBetweenScanPeriods set the scan and between scan periods in foreground and background for ranging state. Ranging state is turned on straight after SDK detects the presence of any first beacon matching the UUID of the application namespace. In the ranging state the scanning should be much more frequent because usually SDK should always provide precise real time data of the beacon. In the ranging state SDK gathers RSSI measurements of the beacon to calculate distance and proximity.

Sample code

An example android code in which custom scan and between scan periods are set during the SDK initialization is presented below.

(...)

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    initUbuduSDK();
}

private UbuduSDK mUbuduSdk;
private UbuduBeaconManager mBeaconManager;

private void initUbuduSDK() {
    mUbuduSdk = UbuduSDK.getSharedInstance(getApplicationContext());
    mUbuduSdk.setNamespace(namespace);

    mBeaconManager = mUbuduSdk.getBeaconManager();
    mBeaconManager.setAreaDelegate(getAreaDelegate());

    mBeaconManager.setRangingBetweenScanPeriods(1100, 5000);
    mBeaconManager.setMonitoringBetweenScanPeriods(10000, 20000);

    mBeaconManager.setRangingScanPeriods(1500, 1500);
    mBeaconManager.setMonitoringScanPeriods(4000, 4000);
}

(...)