The first thing I would say is that it is the first time I am using the beacons. I'm using the library AltBeacon
Currently I have several things working, for example, when I enter a region or when I leave it .. also get the distance to the beacon correctly. This works perfectly. I also get the UUID, the major and the minor .. but do not know if I'm doing it correctly.
This is my code:
public class Hello extends Activity implements BeaconConsumer {
protected static final String TAG = "readme";
private BeaconManager beaconManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.hola);
beaconManager = BeaconManager.getInstanceForApplication(this);
beaconManager.getBeaconParsers().add(new BeaconParser().
setBeaconLayout("m:0-3=4c000215,i:4-19,i:20-21,i:22-23,p:24-24"));
beaconManager.bind(this);
}
@Override
public void onBeaconServiceConnect() {
try {
beaconManager.startRangingBeaconsInRegion(new Region("myRangingUniqueId", Identifier.parse("MY_UUID"), Identifier.parse("1"), null));
} catch (RemoteException e) {
}
beaconManager.setRangeNotifier(new RangeNotifier() {
@Override
public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
if (beacons.size() > 0) {
Log.i(TAG, "The first beacon I see is about " + beacons.iterator().next().getDistance() + " meters away.");
Log.i(TAG, "Reading…"+"\n"+"proximityUuid:"+" "+ beacons.iterator().next().getId1()+"\n"+
"major:"+" "+beacons.iterator().next().getId2()+"\n"+
"minor:"+" "+beacons.iterator().next().getId3());}
}
});
beaconManager.setMonitorNotifier(new MonitorNotifier() {
@Override
public void didEnterRegion(Region region) {
Log.i(TAG, "I just saw an beacon for the first time!");
}
@Override
public void didExitRegion(Region region) {
Log.i(TAG, "I no longer see an beacon");
}
@Override
public void didDetermineStateForRegion(int state, Region region) {
Log.i(TAG, "I have just switched from seeing/not seeing beacons: " + state);
}
});
try {
beaconManager.startMonitoringBeaconsInRegion(new Region("myMonitoringUniqueId", null, null, null));
} catch (RemoteException e) {
}
}
@Override
protected void onDestroy() {
super.onDestroy();
beaconManager.unbind(this);
}
}
As I said, I get the UUID, the major and minor, but I get using
beaconManager.setRangeNotifier (new RangeNotifier () {...
setRangeNotifier
get the distance to the beacon, so that it is automatically updated every few seconds, for this reason, each time it is updated again get the UUID, the major and the minor ..
Is that the right place for get the UUID, the major and the minor? I tried to get it when I enter a region:
@Override
public void didEnterRegion(Region region) {
Log.i(TAG, "I just saw an beacon for the first time!");
}
…but without success.
I searched a lot of information, but can not find anything specific. My intention is to have a variable that contains the UUID, another that contains the major and the other containing the minor. That's right?
If this is not the right way, what is? It is correct as I do now?
I very much appreciate any help and put a mention of @davidgyoung …I hope not to bother.
Greetings.
Yes, using the RangeNotifier
to read the beacon identifiers is the correct approach.
The reason you cannot get the identifiers in the callback for the MonitorNotifier
is because the callback only passes you the Region object whose region you entered/exited. This region can contain wildcards for some of the identifiers, so it usually won't tell you everything about the beacons that were encountered. For some use cases, this is not necessary.
The bottom line is that if you want to read all the identifiers you are doing the correct thing by using the ranging APIs and the RangeNotifier
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With