@Override
public void onBeaconServiceConnect() {
BeaconManager beaconManager = BeaconManager.getInstanceForApplication(this);
beaconManager.setRangeNotifier(new RangeNotifier() {
@Override
public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
for (Beacon beacon : beacons) {
Beacon.setHardwareEqualityEnforced(true);
Log.i("MainActivity", "I see a beacon that is about "+ beacon.getDistance() +" meters away. ");
}
}
});
try {
beaconManager.startRangingBeaconsInRegion(new Region("myRangingUniqueId", null, null, null));
} catch (RemoteException e) { }
}
I'm trying to loop on my beacons and getting the distance from all my beacons, but I want to keep track of the distance of each beacon, I want to know the maximum distance and the minimum distance between me and the beacons, what I'm trying to say and ask is for example, If the distance between me and beacon1 is less than the distance between me and beacon2 do something, or if the distance between me and beacon2 is bigger than the distance between me and beacon3.
I know I can keep track of each beacon with the mac address, but how can I hold and know the distance between me and all the beacons.
what I want to do is "if the beacon with mac address 1 ".getDistance() > "beacon with mac address 2".getDistance(){ do something }
How can I keep hold of the beacons
I often use a BeaconTracker singleton class in apps where I want to keep track of the last distance seen to each beacon. You can see an example of this here:
https://github.com/davidgyoung/ningo-android/blob/master/app/src/main/java/com/davidgyoungtech/beaconscanner/BeaconTracker.java
You can use this class by calling it from your didRangeBeaconsInRegion(...) callback like this:
You can then access the tracked beacons later to compare distance with: