How to get visible beacons from RegionBootstrap AltBeacon method

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(); //check if beacon exists in our DB and throw notification } } } 

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?

+5
source share
2 answers

If you want the application to start when a beacon is detected, then RegionBootstrap is the easiest way. To combine this with the ranging needed to detect individual beacons, add code to your didEnterRegion method, for example:

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

Then implement a callback range, just like yours.

You also need to remove the code below, which probably causes your NullPointerException , because:

 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 updated the library help 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

+4
source

you can implement the RangeNotifier interface, and you can access all the beacon information recorded in the public void didRangeBeaconsInRegion(Collection<Beacon> Beacons, Region region) method of this interface. I hope my question is correct.

0
source

Source: https://habr.com/ru/post/1202249/


All Articles