I use the sample code on this page ( http://altbeacon.imtqy.com/android-beacon-library/samples.html ) in the "Running the application in the background" section, and I got a working application.
It detects when the beacon is in range even in the background.
The problem is that I need to know which beacon it is (UUID, Major, Minor) in order to map it to my local database and give a notification with the application still in the background.
The didEnterRegion (Region region) function only has a matchBeacon method, and I tried the following to determine which of the beacons is being viewed, but it throws 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();
Am I missing something obvious or is it impossible in this library?
EDIT:
I updated the sample code to give you guys a broader idea, and I tried to implement the FOliveira proposal without any success.
EDIT2:
Updated code to reflect davidgyoung offer. Not lucky yet. I have Log.d () right in the first line of the didRangeBeaconsInRegion () function, and it is not being called.
I tried adding BeaconManager.getInstanceForApplication (this) .setRangeNotifier (this); before the try / catch block and the result is the same.
Did I use this sentence incorrectly or is there any other way to make this work?