3

Hi i have created app using altbeacon reference app. And i want to call didEnterRegion using bootstrap notifier when app sees beacon in background. But i dont want it to scan background every 5 minutes, i want my app react to new beacon immediately. Is there some way to do this ?

My Code :

private static final String TAG = ".Application";
private final Identifier uuid = Identifier.parse("A1B2C3D4-AAAA-48D2-B060-D0C0D0C0D0C0");
private RegionBootstrap regionBootstrap;

@Override
public void onCreate() {
    super.onCreate();

    Log.d(TAG, "App has started");

    Region region = new Region(TAG, uuid, null, null);
    BeaconManager.debug = true;
    BeaconManager.getInstanceForApplication(this).getBeaconParsers().add(
            new BeaconParser().setBeaconLayout("m:0-3=4c000215,i:4-19,i:20-21,i:22-23,p:24-24"));
    regionBootstrap = new RegionBootstrap(this, region);
}

@Override
public void didEnterRegion(Region region)
{
    Log.i(TAG, "BACKGROUND ACTIVATED");
}

@Override
public void didExitRegion(Region region) {

}

@Override
public void didDetermineStateForRegion(int i, Region region) {

}

}

Juraj
  • 33
  • 5

1 Answers1

2

You can increase the frequency of background scans with the following code:

beaconManager.setBackgroundBetweenScanPeriod(0l);
beaconManager.setBackgroundScasnPeriod(1100l);

This will make the background detection times as fast as in the foreground. But be forewarned, this will make your app use lots of battery power. You can tweak the between scan period to your tolerance for battery drain. As you noted, the default is 5 minutes (5*3600l).

Android L has new scanning APIs which promise to help improve this tradeoff between detection timers and battery usage. But for4.3 and 4.4 apps, you need to make a judgment call.

davidgyoung
  • 63,876
  • 14
  • 121
  • 204
  • And what about function regionBootstrap ? Does this feature work ? Because in API docs it says: "Class allowing a user to set up background launching of an app when a user enters a beacon Region.". Does this work in background mode ? Beacause i only get didEnterRegion call when i start app. – Juraj Sep 23 '14 at 12:15
  • Yes, the RegionBootstrap will launch the app into the background and `didEnterRegion` gets called when a beacon matching the specified region is detected. The specifics of how long this will take depends on the state of your app. See here: http://altbeacon.github.io/android-beacon-library/resume-after-terminate.html – davidgyoung Sep 23 '14 at 13:52