ibeacon-android

didEnterRegion event fired when out of beacon area


When you are outside the area of ​​the beacon, you sometimes enter didEnterRegion.

Sometimes not entering didEnterRegion when in beacon area. This lasts a long time.

The terminal is Fujitsu arrows M03. Android ™ 6.0

You can get beacon information with didRangeBeaconsInRegion when you are in didEnterRegion.

The activity is used by the foreground service.

BeaconManager is used as a foreground in that service.

Screen is off. However, wakeLock is acquired in the service.

The beacon scan interval is 4 seconds. setForegroundBetweenScanPeriod (4000);

Monitoring is turned off / on at 10-second intervals.

never enter didExitRegion. I think it is necessary to turn on monitoring for more than 10 seconds to enter.

Is it necessary to generate didExitRegion firmly?

Is it better not to turn monitoring ON / OFF every 10 seconds? Is ON always better?

Beacon detection settings

g_beaconManager = BeaconManager.getInstanceForApplication(this);
g_beaconManager.getBeaconParsers().add(new BeaconParser().setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24"));
g_beaconManager.bind(this);
g_beaconManager.setForegroundBetweenScanPeriod(4000);
g_region = new Region("iBeacon", null, null, null);

BeaconManager Services

@Override
public void onBeaconServiceConnect() {

    g_beaconManager.addMonitorNotifier(new MonitorNotifier() {
        public void didEnterRegion(Region region) {
            Log.d("Beacon", "didEnterRegion Success!!");

            if (g_beaconManager.getRangedRegions().size() == 0) {
                try {
                    g_beaconManager.startRangingBeaconsInRegion(g_region);
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
            }
        }

        @Override
        public void didExitRegion(Region region) {
            Log.d("Beacon", "didExitRegion Success!!");

            if (g_beaconManager.getRangedRegions().size() != 0) {
                try {
                    g_beaconManager.stopRangingBeaconsInRegion(g_region);
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
            }
        }

        @Override
        public void didDetermineStateForRegion(int i, Region region) {
            Log.d("Beacon", "didDetermineStateForRegion Success!!");

            if (g_beaconManager.getRangedRegions().size() == 0) {
                try {
                    g_beaconManager.startRangingBeaconsInRegion(g_region);
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
            }
        }
    });

    try {
        g_beaconManager.startMonitoringBeaconsInRegion(g_region);
    } catch (RemoteException e) {
        e.printStackTrace();
    }

    g_beaconManager.addRangeNotifier(new RangeNotifier() {
        @Override
        public void didRangeBeaconsInRegion(Collection<Beacon> Beacons, Region region) {

            Beacon lBeacon_Most_Near_Distance = null;

            Log.d("Beacon" , "addRangeNotifier Success!!");

            for (Beacon beacon : Beacons) {

                if (lBeacon_Most_Near_Distance == null)
                {
                    lBeacon_Most_Near_Distance = beacon;
                }
                else
                {
                    if (lBeacon_Most_Near_Distance.getDistance() > beacon.getDistance())
                    {
                        lBeacon_Most_Near_Distance = beacon;
                    }
                }
            }
        }
    });
}

Monitoring OFF / ON every 10 seconds

try {
    if (g_beaconManager.getRangedRegions().size() != 0) {
        g_beaconManager.stopRangingBeaconsInRegion(g_region);
    }

    if (g_beaconManager.getMonitoredRegions().size() != 0) {
        g_beaconManager.stopMonitoringBeaconsInRegion(g_region);
    } else {
        g_beaconManager.startMonitoringBeaconsInRegion(g_region);
    }
} catch (RemoteException e) {
    e.printStackTrace();
}

Solution

  • The library determines it has exited a region by not seeing any visible beacon packets for 10 seconds. This constant can be overridden to any value you want by calling BeaconManager.setRegionExitPeriod(15000); // change to 15 seconds If you don't let monitoring run for this long, you won't get any region exit events.

    The library is designed for you to turn on monitoring and leave it on for an extended period of time. Turning it off after such a short period is asking for trouble, not just with region exits, but by starting and stopping scanning frequently, which is an intensive operation that can lead the Android OS to block future scans.