0
votes

I'm having a strange behavior when trying to detect regions with Android Beacon Library.

I defined 2 regions to monitor for beacons with specific ids.

Everytime I am in no region and enter one, didEnterRegion callback is triggered twice, one time for the expected region, and another time for a 'null' region. If I enter a second region, it's triggered just once for the expected region.

Is this behavior normal? How can I make it not trigger for this 'null' region?

Here is a log that I printed to better show what's happening:

10-10 18:07:15.683: Got a didEnterRegion call, id1: 11111111-1111-1111-1111-111111111111

10-10 18:07:15.693: Got a didEnterRegion call, id1: null id2: null id3: null

10-10 18:07:22.946: Got a didEnterRegion call, id1: 00000000-0000-0000-0000-000000000000

10-10 18:07:41.880: got a didExitRegion 11111111-1111-1111-1111-111111111111

10-10 18:07:57.913: got a didExitRegion 00000000-0000-0000-0000-000000000000

10-10 18:07:57.914: got a didExitRegion null

And here is part of my code:

public class BeaconReferenceApplication extends Application implements BootstrapNotifier {
    private RegionBootstrap regionBootstrap;

    public void onCreate() {
        super.onCreate();

        // UPDATE: Trying to clear old regions.
        BeaconManager beaconManager = BeaconManager.getInstanceForApplication(this);
        for (Region region: beaconManager.getMonitoredRegions()) {
            Log.i(TAG, "Clearing old monitored region" + region);
            try {
                beaconManager.stopMonitoringBeaconsInRegion(region);
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }

        ArrayList<Identifier> ids1 = new ArrayList<>(1);
        ids1.add(Identifier.parse("11111111-1111-1111-1111-111111111111"));
        Region region1 = new Region("region1", ids1);

        ArrayList<Identifier> ids0 = new ArrayList<>(1);
        ids0.add(Identifier.parse("00000000-0000-0000-0000-000000000000"));
        Region region0 = new Region("region0", ids0);


        ArrayList<Region> regions = new ArrayList<>(2);
        regions.add(region0);
        regions.add(region1);
        regionBootstrap = new RegionBootstrap(this, regions);
    }
    @Override
    public void didEnterRegion(Region region) {
        Log.i(TAG, "Got a didEnterRegion call, " + region);
    }
    @Override
    public void didExitRegion(Region region) {
        Log.i(TAG, "got a didExitRegion" + region.getId1());
    }
}
1

1 Answers

1
votes

I suspect what is happening is that the "null" region was persisted by an older version of your code. This would have happened when an older code version started monitoring for that region.

Understand that whenever you start monitoring for a region, the Android Beacon Library will persist those regions to non-volatile storage on your Android device, so when it restarts, it remembers the last region state and it knows whether to trigger a new region entry event.

The consequence of this is that if you ever want to clear out those old regions, you have to do so programatically. You can fix this with the following code:

    // Stop monitoring all currently monitored regions
    for (Region region: mBeaconManager.getMonitoredRegions()) {
        mBeaconManager.stopMonitoringBeaconsInRegion(region);
    }        '