Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get visible beacons from the RegionBootstrap AltBeacon method

I'm using the example code on this page (http://altbeacon.github.io/android-beacon-library/samples.html) in the Starting an App in the Background section and I've got a working app.

It detects whenever a beacon is in range even on background.

The problem is I need to know which beacon is it (UUID, Major, Minor) to then match it against my local database and throw a notification with the app still on background.

The didEnterRegion(Region region) function only has a matchesBeacon method, and I've tried doing the following to identify which of the beacons is being seen but it's throwing a NullPointerException:

public class SightSeeing extends Activity implements BootstrapNotifier, RangeNotifier {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

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

    BeaconManager.getInstanceForApplication(this).getBeaconParsers().add(
            new BeaconParser(). setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24")
    );

    BeaconManager.getInstanceForApplication(this).setRangeNotifier(this);

}

@Override
public void didEnterRegion(Region region) {

    regionBootstrap.disable();

    BeaconManager.getInstanceForApplication(this).setRangeNotifier(this);

    try {
        BeaconManager.getInstanceForApplication(this).startRangingBeaconsInRegion(region);
    }
    catch (RemoteException e) {
        Log.e(TAG, "Can't start ranging");
    }

 }


@Override
public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
    if (beacons.size() > 0) {
        Iterator<Beacon> beaconIterator = beacons.iterator();
        while (beaconIterator.hasNext()) {
            Beacon beacon = beaconIterator.next();
            //check if beacon exists in our DB and throw notification
        }
    }
}

Am I missing something obvious or isn't this possible with this library?

EDIT:

I've updated the code sample to give you guys a broader idea and I've tried implementing the suggestion by FOliveira without any success.

EDIT2:

Updated code to reflect the davidgyoung's suggestion. Still no luck. I have a Log.d() right on the first line of the didRangeBeaconsInRegion() function and it isn't being called.

I've tried adding BeaconManager.getInstanceForApplication(this).setRangeNotifier(this); before the try/catch block and the result is the same.

Did I implement the suggestion wrong or is there any other way to get this working?

like image 507
José Miguel Melo Avatar asked Sep 10 '14 14:09

José Miguel Melo


1 Answers

If you want the app to launch itself on beacon detection, then the RegionBootstrap is the easiest way to go. In order to combine this with Ranging needed to detect individual beacons, then add code in your didEnterRegion method like this:

try {
    BeaconManager.getInstanceForApplication(this).startRangingBeaconsInRegion(region);
} 
catch (RemoteException e) { 
    Log.e(TAG, "Can't start ranging");
}

Then implement a ranging callback like you have.

You also need to remove the code below, which is probably what is causing your NullPointerException, because the :

for(int i=0; i< beaconsList.size(); i++) {
    Beacon b = new Beacon.Builder()
            .setId1(beaconsList.get(i).get("uuid"))
            .setId2(beaconsList.get(i).get("major"))
            .setId3(beaconsList.get(i).get("minor"))
            .build();

    if(region.matchesBeacon(b)) {
        //get info from DB and throw notification
    }
 }

EDIT: I have updated the library's reference application to show how this can be done successfully. See here: https://github.com/AltBeacon/android-beacon-library-reference/blob/master/src/org/altbeacon/beaconreference/BeaconReferenceApplication.java

like image 82
davidgyoung Avatar answered Nov 01 '22 10:11

davidgyoung