0
votes

I have been tasked with fixing some code that uses altbeacon with iBeacons. The detection of beacons works well in foreground but not in background.

In the Activity that scans in foreground mode, on entering background the onStop is used to unbind from the beaconmanager and then call a method in the beaconBkgService like this:

Activity code:

protected void onStop() {

    super.onStop();

    try {
        // Do we need to unbind????
        iBeaconManager.unbind(this);

        beaconBkgService.startScanning();

      } catch (Exception e) {
        e.printStackTrace();
      }

}

beaconBkgService code:

public class beaconBkgService extends Application implements BootstrapNotifier {

    public void startScanning() {

        try {

            mBeaconManager = BeaconManager.getInstanceForApplication(this);
            mBeaconManager.setBackgroundBetweenScanPeriod(3000L);
            mBeaconManager.setBackgroundScanPeriod(1000L);

            mBeaconManager.setBackgroundMode(true);

            if (mRegionBootstrap == null){
                mAllBeaconsRegion = new ArrayList<Region>();

                for (UserBeacons userBeacon : userBeacons) {

                    mBeaconsRegion = new Region(userBeacon.identifier, userBeacon.UUID, null, null);
                    mAllBeaconsRegion.add(mBeaconsRegion);
                }
                mRegionBootstrap = new RegionBootstrap(this, mAllBeaconsRegion);
             }

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

Should the Activity onStop call the beacon manager unbind() prior to calling beaconBkgService.startScanning() ? I have tried with and without but cannot anything to work.

The desired outcome is that when the app goes into background, scanning should last for 1 sec every 3 secs but the beaconBkgService didEnterRegion does not fire when a beacon is turned on and the app is in background.

And when I get this to work, how will the ExitRegion be effected if the Activity detects the beacon in foreground, then the user switches to background and exits the beacon area? Will the onExitRegion in the beaconBkgService fire? i.e is the state preserved from foreground to background using RegionBootstrap?

1

1 Answers

0
votes

The RegionBootstrap and BootstrapNotifier are not designed to be used with and Android Activity. They should instead be used with an Android Application class or other object that exists for the lifetime of the Android app.

Understand that in the Android lifecycle, Activities are created and destroyed as the user navigates around the app. Any code that tries to do long-term beacon scanning in an Activity simply won't work.

The Application object, by contrast, is created when the user first launches the app (or when the app is auto-launched by first beacon detection) and continues to exist.

The simplest way to fix this code is to rewrite it to use an Application class. You can see an example of how to do this in the sample code for the Android Beacon Library. See the Starting App in the Background section here: http://altbeacon.github.io/android-beacon-library/samples.html

You also probably want to use the code in the Auto Battery Saving Example Code section of that same page.