androidibeaconandroid-ibeaconaltbeacon

Starting the App when an iBeacon is detected


I am experimenting the Android Beacon Library to monitoring iBeacon in background with this code:

public class IBeaconBootstrap extends Application implements BootstrapNotifier {

private RegionBootstrap regionBootstrap;

@Override
public void onCreate() {

   super.onCreate();

   Log.d("IBeaconBootstrap", "App started up");

   // wake up the app when any beacon is seen (you can specify specific id
   // filers in the parameters below)

   Region region = new Region("MyRegion", null, null, null);
   regionBootstrap = new RegionBootstrap(this, region);

   // This is for Apple compatible iBeacons
   BeaconManager.getInstanceForApplication(this).getBeaconParsers().add(new     BeaconParser().setBeaconLayout("m:0-3=4c000215,i:4-19,i:20-21,i:22-23,p:24-24"));
}

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

   Log.d("Boostrap didDetermineStateForRegion", "Region " + region.toString());
}

@Override
public void didEnterRegion(Region region) {

   Log.d("Boostrap didEnterRegion", "Got a didEnterRegion call");

   intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
   this.startActivity(intent);
}

@Override
public void didExitRegion(Region region) {

   Log.d("Boostrap didExitRegion", "Got a didExitRegion call");
}
}

The app goes foreground when detects an iBeacon if it is running but nothing happens if the app is not running. Is this the expected behavior or the app is supposed to be started if it is not running?


Solution

  • You probably need to clarify what you mean by "the app is not running". Do you mean:

    1. The app has been installed but never launched
    2. The app has been launched once but since rebooted
    3. The app has been killed from the task switcher

    Using the code above, here is the expected behavior in each case:

    1. The app will not be running and cannot auto-launch the Activity.
    2. The app will start scanning for beacons periodically after boot and will launch the Activity when one is detected.
    3. The app will not be running and cannot auto-launch until charger connect/disconnect or reboot. After that time, behavior is as in (2). More details on this case are available here.

    It is important to note that when no Activity is visible, the library will only do a scan tyo look for beacons every 5 minutes, so detection can take that long. This interval is fully configurable.

    The restrictions on case (3) are imposed by Android OS. An event must occur allowing restart of an app after it is killed by a user.