How to detect Region Enter/Exit for multiple beacons using AltBeacon android-beacon-library?

user993543 picture user993543 · Aug 9, 2014 · Viewed 12.2k times · Source

I am working with iBeacons and using the AltBeacon library.

beaconManager.getBeaconParsers()
             .add(new BeaconParser()
             .setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24"));

I would like my Android app to detect and generate an event when beacons enter and exit visibility. This works just fine fine with a single beacon using the library using methods.

public void **didEnterRegion**(Region region)

and

public void **didExitRegion**(Region region) 

My problem is when multiple beacons are visible at the same time. I am trying to maintain an array with all beacons visible. I want to generate an event each time a beacon enters and exits. The event should identify the beacon that generated the event by it's unique Identifier. My beacons are uniquely identifiable using the beacon.getIdentifiers() or (UUID, Major and Minor)

The problem is that the didExitRegion method does not get executed until all beacons exit the region.

Can anyone think of a simple way for me to achieve my goals using AltBeacon library?

Any suggestions would be greatly appreciated.

Answer

davidgyoung picture davidgyoung · Aug 10, 2014

Two options:

  1. Set up a different region to match only each individual beacon, specifying all their identifiers, and monitor for each. You will get a different entry and exit callback for each region.

    Region region1 = new Region("myIdentifier1", Identifier.parse("2F234454-CF6D-4A0F-ADF2-F4911BA9FFA6"), Identifier.parse("1"), Identifier.parse("1"));        
    Region region2 = new Region("myIdentifier2", Identifier.parse("2F234454-CF6D-4A0F-ADF2-F4911BA9FFA6"), Identifier.parse("1"), Identifier.parse("2"));   
    
    beaconManager.startMonitoringBeaconsInRegion(region1);
    beaconManager.startMonitoringBeaconsInRegion(region2);    
    
  2. Enable ranging, and put code in the didRangeBeaconsInRegion callback to track individual beacons. You can use a java.util.HashMap to keep track of all beacons that are visible (with a timestamp for the latest time each was seen), and then if you haven't seen a beacon in, say, five seconds, you can remove the beacon from the HashMap and execute your exit logic for that beacon.

Option 1 is great for a small number of beacons where you know their identifiers up front. Option 2 is more involved, but better for a large number of beacons or if you do not know their identifiers in advance.