Looping on beacons

2019-08-20 08:31发布

@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

1条回答
Luminary・发光体
2楼-- · 2019-08-20 09:13

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:

BeaconTracker.getInstance(context).updateTrackedBeacons(beacons);

You can then access the tracked beacons later to compare distance with:

List<TrackedBeacon> trackedBeacons = BeaconTracker.getInstance(context).getTrackedBeacons();
查看更多
登录 后发表回答