0
votes

We are trying to use Altbeacon library to satisfy the next study case: We want to put several IBeacons in a room or corridor with a distance of no more than 3 meters between each of them, and we want to get the current closest Ibeacon based on the user phone which scans for the beacons. We first tried to build regions with only one beacon each, wondering that a region were a closed set, meaning that when you enter in a region, you couldn’t be in other region at the same time, and when you leave a region, you enter in the next closest one and so. But that’s not the approach that the library implements. We want to know if there’s any way in Altbeacon library to apply our approach or if some kind of patch has to be made to satisfy the study case that I present to you.

1
I don't know Altbeacon library, but if it is like other beacon libraries, you should range beacons, not monitor regions, in order to find the nearest beacon.Jerome Diaz
With this library you can only range beacons inside a especified region where you can find the closest one inside of the region (supposing that there're two or more). We want to make the application to enter in only the closest region, and when entering in one, we wont leave it until there's other region closer to us.Jorge Revuelta
"Regions" only exist on Android at the level of the code provided by your app, including this library. As far as Android is concerned the BLE packet contents are arbitrary data and your options are open. However if you want to be compatible with iOS, you need to use as few regions as possible, as there are tight limits on how much you can look for at once.Chris Stratton

1 Answers

1
votes

The easiest way to accomplish this goal is to range for all beacons using a single region, and start ranging:

@Override
public void onBeaconServiceConnect() {
    try {
         // Set up a region that matches all of your beacons.  You may want to replace the first
         // null with a UUID that all your beacons share.

        Region allBeaconsRegion = new Region("all beacons", null, null, null);
        beaconManager.startRangingBeaconsInRegion(mAllBeaconsRegion);
        beaconManager.setRangeNotifier(this);    

    } catch (RemoteException e) {
        Log.e(TAG, "Cannot connect to beacon service");
    }
}

Note, if you are using a custom Application class with the RegionBootstrap, you can put the above code above inside the didEnterRegion method instead of inside the onBeaconServiceConnect method.

Once you start ranging, you will get a callback once per second with a list of all visible beacons. You can add code to determine which one is closest:

@Override
public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region arg1) {
    Beacon closestBeacon = null;

    for (Beacon beacon : beacons) {
            if (closestBeacon == null) {
                closestBeacon = beacon;
            }
            else {
                if (closestBeacon.getDistance() > beacon.getDistance()) {
                    closestBeacon = beacon;
                }
            }
    }

    // Do Something with closestBeacon here        
}

Keep in mind that the closest beacon may change back and forth due to radio noise, so you probably need to add extra logic to protect against the closest beacon flipping back and forth too often.