Search code examples
androidbeacon

altBeacon: didEnterRegion


I'm writing an Android App custom Class that utilises AltBeacon's Android-Beacon-Library. But when first start, If I'm in the beacon region, the didEnterRegion() won't work. I want to create an event right away. I can't find any other solution. Plz, Any help appreciated! This is my code ▼

beaconManager.addMonitorNotifier(new MonitorNotifier() {
            @Override
            public void didEnterRegion(Region region) {
                //Log.i(TAG, "I just saw an beacon for the first time!");
                sendData(dataStart);
                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        tv_beacon_.setText("work");
                    }
                });
            }

            @Override
            public void didExitRegion(Region region) {
                //Log.i(TAG, "I no longer see an beacon");
                sendData(dataEnd);
                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        tv_beacon_.setText("not work");
                    }
                });
            }

            @Override
            public void didDetermineStateForRegion(int state, Region region) {
                Log.i(TAG, "I have just switched from seeing/not seeing beacons: "+state);

            }

Solution

  • This behavior is expected, because the didEnterRegion callback only fires if you are not already in the region. You can learn more about it here: https://altbeacon.github.io/android-beacon-library/detection-trouble.html

    What you can do though, is call

    beaconManager.setRegionStatePeristenceEnabled(false)
    

    This will make the didEnterRegion callback fire after an app re-start, even if phone is already in the beacon region.

    If you do this, bare in mind that the callback might fire multiple times when the app in background is temporarily killed by the Android OS.