I am experimenting the Android Beacon Library and I am able to make it working for monitoring and ranging with Apple compatible beacons adding a custom Parser (see Is this the correct layout to detect iBeacons with AltBeacon's Android Beacon Library?)
Now I am trying to write an App which starts in background using the sample code shown here:
http://altbeacon.github.io/android-beacon-library/samples.html
This is my 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);
}
@Override
public void didDetermineStateForRegion(int state, Region region) {
// Don't care
// MonitorNotifier.INSIDE
Log.d("Boostrap didDetermineStateForRegion", "Region " + region.toString());
}
@Override
public void didEnterRegion(Region region) {
Log.d("Boostrap didEnterRegion", "Got a didEnterRegion call");
// This call to disable will make it so the activity below only gets
// launched the first time a beacon is seen (until the next time the app
// is launched)
// if you want the Activity to launch every single time beacons come
// into view, remove this call.
regionBootstrap.disable();
Intent intent = new Intent(this, MainActivity.class);
// IMPORTANT: in the AndroidManifest.xml definition of this activity,
// you must set android:launchMode="singleInstance" or you will get two
// instances
// created when a user launches the activity manually and it gets
// launched from here.
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
this.startActivity(intent);
}
@Override
public void didExitRegion(Region region) {
Log.d("Boostrap didExitRegion", "Got a didExitRegion call");
}
}
Unfortunately, it doesn't work. These functions are never called:
- public void didDetermineStateForRegion(int arg0, Region arg1)
- public void didEnterRegion(Region arg0)
- public void didExitRegion(Region arg0)
I expected that at least didDetermineStateForRegion were called creating the RegionBootstrap.
Questions:
0) What am I missing?
1) Does this feature also work with Apple compatible iBeacons?
2) Do I have to add a custom Parser? Where/How?
Thank you in advance.
UPDATE 0:
Following the davidgyoung directions I eventually got it working, changing the onCreate function as follows:
@Override
public void 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);
BeaconManager.getInstanceForApplication(this).getBeaconParsers().add(new BeaconParser().setBeaconLayout("m:0-3=4c000215,i:4-19,i:20-21,i:22-23,p:24-24"));
super.onCreate();
}
I have two more questions:
0) The app checks every 3-5 minutes for the presence of iBeacons, is there any way to statically or dynamically change this interval?
1) Apparently, 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?