iBeacon background scanning

Sven Haiges picture Sven Haiges · Dec 9, 2013 · Viewed 7.4k times · Source

I've written my own little BLE scanning service that is triggered via an alarm every 3-5 seconds. It scans for 1.1 seconds to get teh beacons around it and then transforms the RSSI signal into a rough proximity.

I am now considering the Radius Networks Android iBeacon service, but I am wondering how I could realize the same background scanning.

e.g.: I want the beacons scannign to start annd run in the background and receive Intents into a broadcast receiver to decide what I do with the beacons scanned.

Are there soem examples and is there an estimate how much battery this consumes?

Answer

davidgyoung picture davidgyoung · Dec 10, 2013

Radius Networks' Android iBeacon Library does exactly that. All you have to do to run it in the background is bind the IBeaconManager to something with a long lifecycle. This can be a custom android.app.Application object, or a Service of your own. Since you have already written your own service for your app, you could easily bind the IBeaconManager to that service, and it would remain active in the background as long as the service runs. You could also use your service to send out broadcast intents if you wish, but for most use cases this is probably not necessary.

As for battery usage, the library's reference application has an example of how to set the background mode on the library so scans happen less often, saving battery. The pertinent code in that reference app is here:

    @Override 
    protected void onPause() {
            super.onPause();
            if (iBeaconManager.isBound(this)) iBeaconManager.setBackgroundMode(this, true);                    
    }
    @Override 
    protected void onResume() {
            super.onResume();
            if (iBeaconManager.isBound(this)) iBeaconManager.setBackgroundMode(this, false);                    
    } 

Setting the background mode to true reduces the Bluetooth scans to happen only once very 5 minutes -- something similar to what iOS does. Based on tests on a Nexus 4, this reduces the phone's overall battery consumption from 95mA to 55mA (both of which numbers include the overall operating system drain.)

Full Disclosure: I work for Radius Networks and am the primary author of the Android iBeaconLibrary.