-->

Detect beacon exited a range

2019-09-08 15:20发布

问题:

I am using Android with the AltBeacon library. I am starting to develop a BLE app which should tell me when a beacon is entering and when it is exiting a range inside a region so I can start an action.

I could detect when a beacon is entering a region and determine his range. Therefore I used the didRangeBeaconsInRegion Method.

My problem is now how to detect that a beacon has left/exited that range in the region? I couldn't find anything in the lib to do so.

How can I achieve this?

回答1:

The Android Beacon Library offers to sets of APIs for looking for beacons:

  1. Ranging APIs. These give you a callback to didRangeBeaconsInRegion approximately every second that beacons of interest are in range, and provides a list of all these beacons so you can inspect their identifiers.

  2. Monitoring APIs. These provide callbacks only when beacons of interest appear didEnterRegion or disappear didExitRegion.

Your question suggests that you have currently implemented the Ranging APIs, and need to implement the Monitoring APIs to know when all beacons of interest disappear.

You can see an example of this in the Monitoring Example Code section at the top of this page.

EDIT: Note that you can monitor regions based on individual beacons and dynamically change which are being monitored. So in the ranging callback you could do something like:

Region singleBeaconRegion = new Region(beacon.toString(), beacon.getIdentifiers());
beaconManager.startMonitoringBeaconsInRegion(singleBeaconRegion);

This will start monitoring for a single beacon matching the passed identifiers. You will get a callback to didExitRegion when it is no longer visible.

If you use this technique, you should also stop monitoring the region on exit, so your monitored region list does not grow large over time:

public void didExitRegion(Region region) {
    // perform logic based on region here
    beaconManager.stopMonitoringBeaconsInRegion(region);
}