Fab
Fab

Reputation: 1564

Starting App in background when iBeacon detected

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:

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?

Upvotes: 2

Views: 1856

Answers (1)

davidgyoung
davidgyoung

Reputation: 64961

Understand that out-of-the-box, the library only works with beacons supporting the open AltBeacon standard. This is necessary in order to keep all intellectual property out of the open source library. If you wish to make the library work with proprietary beacons, you must add a line to your onCreate method to add a different BeaconParser instance.

BeaconManager.getInstanceForApplication().getBeaconParsers()
  .add(new BeaconParser().setBeaconLayout("put-your-parser-expression-here"));

You will need to replace the string "put-your-parser-expression-here" with one that will detect the proprietary beacon you are using. In order to find parser expressions for various proprietary beacons, try doing a Google search for "getBeaconParser" (include the quotes in the search.)

Upvotes: 2

Related Questions