3

Is background monitoring of Eddystone beacon using altbeacon library on android platform possible? How can I achieve it?

Following is the code by which I can detect beacons with a specified UUID when the app is launched, but I want to achieve the same when the app is not running.

public class MainActivity extends ActionBarActivity implements BeaconConsumer,MonitorNotifier
{

private BeaconManager beaconManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}


@Override
protected void onResume() {
    super.onResume();
    beaconManager = BeaconManager.getInstanceForApplication(this.getApplicationContext());
    beaconManager.getBeaconParsers().add(new BeaconParser().setBeaconLayout("s:0-1=feaa,m:2-2=00,p:3-3:-41,i:4-13,i:14-19"));
    beaconManager.bind(this);
}

@Override
public void onBeaconServiceConnect() {

    Identifier myBeaconNamespaceId = Identifier.parse("0xe2bfcc3cc2370789caef");
    Region region = new Region("my-beacon-region", myBeaconNamespaceId, null, null);
    beaconManager.setMonitorNotifier(this);
    try {
        beaconManager.startMonitoringBeaconsInRegion(region);
    } catch (RemoteException e) {
        e.printStackTrace();
    }
}

@Override
public void didEnterRegion(Region region) {

        Log.d("radbeacon", "Beacon detected with namespace id " + region.getId1() +" and instance id: " + region.getId2());
}

@Override
public void didExitRegion(Region region) {

    Log.d("radbeacon", "Beacon out of region with namespace id " + region.getId1() +" and instance id: " + region.getId2());
}

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


}
Parag Kadam
  • 3,620
  • 5
  • 25
  • 51

2 Answers2

2

Yes, it is possible to detect Eddystone beacons in the background with the Android Beacon Library. You do so in the same manner as with AltBeacon or iBeacon. Details are described in the Starting App in the Background section of the samples.

EDIT: As fof Library version 2.7, support for hardware accelerated discovery of Eddystone frames has been added, meaning that on Android 5+ devices you can get background detections within about 5 seconds.

The basic idea is you need to create a central android Application class for your app, and create a RegionBootstrap object in the onCreate method of that class. It is important to remember that you must register this Application class in your manifest. The sample code linked above shows you how to do this.

So you'd end up with something like below:

public class MyApplication extends Application implements BootstrapNotifier {
    private static final String TAG = "MyApplication";
    private RegionBootstrap regionBootstrap;
    private BackgroundPowerSaver backgroundPowerSaver;
    private BeaconManager mBeaconManager;

    public void onCreate() {
        super.onCreate();
        mBeaconManager = org.altbeacon.beacon.BeaconManager.getInstanceForApplication(this);
        mBeaconManager.getBeaconParsers().clear();
        mBeaconManager.getBeaconParsers().add(new BeaconParser().
            setBeaconLayout(BeaconParser.EDDYSTONE_UID_LAYOUT));
        Identifier myBeaconNamespaceId = Identifier.parse("0xe2bfcc3cc2370789caef");
        Region region = new Region("my-beacon-region", myBeaconNamespaceId, null, null);

        regionBootstrap = new RegionBootstrap(this, region);

        backgroundPowerSaver = new BackgroundPowerSaver(this);
    }

    @Override
    public void didEnterRegion(Region region) {

            Log.d("radbeacon", "Beacon detected with namespace id " + region.getId1() +" and instance id: " + region.getId2());
    }

    @Override
    public void didExitRegion(Region region) {

        Log.d("radbeacon", "Beacon out of region with namespace id " + region.getId1() +" and instance id: " + region.getId2());
    }

    @Override
    public void didDetermineStateForRegion(int i, Region region) {
          //Ignore
    }
...
}
davidgyoung
  • 63,876
  • 14
  • 121
  • 204
  • 1
    Thanks for the answer david :) I have used this code as instructed, but not sure if it is able to monitor the beacon in the background , but when I launch the app it will immediately detect the beacon. What's happening here? How often does the app monitor the beacon in the background? – Parag Kadam Nov 10 '15 at 06:22
  • By default on Android 4.x it scans for beacons once every 5 minutes. On Android 5+ it does a low power background scan constantly. – davidgyoung Nov 10 '15 at 11:51
  • 1
    I am running it no android 5.0.2 ,but using the above code it never detects the beacon in background...the beacon gets detected only when the app is launched. Does it require a root access or am i missing something? – Parag Kadam Nov 10 '15 at 13:23
  • I have noticed a few things here - 1)The above code does not work with android 6.0(Nexus 5) ,the beacon is not even detected when the app is in the foreground let alone background. 2) The beacon is detected on android 5.0.2(xiaomi mi4i) only when the app is launched. 3) The beacon is detected both in the foreground and background on android 5.1.1(Oneplus one).Is there a explanation as to why this is happening? – Parag Kadam Nov 10 '15 at 13:58
  • I suspect the problem with the Nexus 5 and Android 6.0 may have to do with the need to turn on location services. Would you mind opening a new question about this? I will answer there because I think it requires a longer discussion. – davidgyoung Nov 12 '15 at 23:11
  • Hi david here's the link to the question - http://stackoverflow.com/questions/33690470/enable-to-monitor-beacon-on-android-6-0-using-nexus-5-device – Parag Kadam Nov 13 '15 at 10:22
  • Thanks. Take a look at my answer, particularly the part about Location Services. – davidgyoung Nov 13 '15 at 12:01
0

Another very important thing to consider is adding the Application class name to the AndroidManifest.xml, just add android:name=".yourApplication" to the <application> tag.

Is there an example available doing both monitoring and ranging in an Application class?

MattAllegro
  • 6,455
  • 5
  • 45
  • 52
Jonas
  • 1
  • 1
  • check this link -> https://altbeacon.github.io/android-beacon-library/samples.html for ranging example. – Parag Kadam Nov 11 '15 at 10:24
  • There is no sample doing both monitoring and ranging, but it is easy to do. Just make the `Application` class implement `RangeNotifier`. Then in `didEnterRegion` method, set `rangeNotifier=self;` and call `beaconManager.startRangingBeaconsInRegion(region)`. If you have trouble, please post a new question and I will answer. – davidgyoung Nov 12 '15 at 23:18