didRangeBeaconsInRegion() call multiple times afte

2019-08-28 06:50发布

问题:

I am developing Android app which scan near Beacons. Very first time open Android app and scan near beacons and didRangeBeaconsInRegion() method call every 2 seconds.

But when I close app then again starts then didRangeBeaconsInRegion() method called 2 times in 2 seconds

3rd time didRangeBeaconsInRegion() method called 3 times in 2 seconds

and so on....

Code snippet

@Override
public void didRangeBeaconsInRegion(final Collection<Beacon> beacons, final Region region) {
    if (beacons != null) {
        Log.i("iBeacons", "beacons.size() : " + beacons.size());
    }
}

Logs:

2018-12-29 21:39:40.328 26528-27120/com.test.demo I/iBeacons: beacons.size() : 0
2018-12-29 21:39:40.328 26528-27120/com.test.demo I/iBeacons: beacons.size() : 0
2018-12-29 21:39:40.328 26528-27120/com.test.demo I/iBeacons: beacons.size() : 0

2018-12-29 21:39:42.259 26528-27124/com.test.demo I/iBeacons: beacons.size() : 1
2018-12-29 21:39:42.260 26528-27124/com.test.demo I/iBeacons: beacons.size() : 1
2018-12-29 21:39:42.260 26528-27124/com.test.demo I/iBeacons: beacons.size() : 1

2018-12-29 21:39:44.229 26528-27132/com.test.demo I/iBeacons: beacons.size() : 1
2018-12-29 21:39:44.229 26528-27132/com.test.demo I/iBeacons: beacons.size() : 1
2018-12-29 21:39:44.229 26528-27132/com.test.demo I/iBeacons: beacons.size() : 1

2018-12-29 21:39:46.241 26528-27136/com.test.demo I/iBeacons: beacons.size() : 1
2018-12-29 21:39:46.241 26528-27136/com.test.demo I/iBeacons: beacons.size() : 1
2018-12-29 21:39:46.241 26528-27136/com.test.demo I/iBeacons: beacons.size() : 1

2018-12-29 21:39:48.251 26528-27138/com.test.demo I/iBeacons: beacons.size() : 1
2018-12-29 21:39:48.251 26528-27138/com.test.demo I/iBeacons: beacons.size() : 1
2018-12-29 21:39:48.251 26528-27138/com.test.demo I/iBeacons: beacons.size() : 1

2018-12-29 21:39:50.249 26528-27139/com.test.demo I/iBeacons: beacons.size() : 0
2018-12-29 21:39:50.249 26528-27139/com.test.demo I/iBeacons: beacons.size() : 0
2018-12-29 21:39:50.249 26528-27139/com.test.demo I/iBeacons: beacons.size() : 0

2018-12-29 21:39:52.241 26528-27141/com.test.demo I/iBeacons: beacons.size() : 0
2018-12-29 21:39:52.241 26528-27141/com.test.demo I/iBeacons: beacons.size() : 0
2018-12-29 21:39:52.241 26528-27141/com.test.demo I/iBeacons: beacons.size() : 0

Update 2:

Here the code for callbacks

mBeaconManager.addRangeNotifier(this);

Above callback is called only one time form application once user allow all necessary permission for beacon scan operation

Problem: It is working fine in first time but issue with 2nd time, 3rd time, ... restarts. I captured above logs when application was restarts 3rd time.

Note: It is also working fine when I clear the application form background.

回答1:

mBeaconManager.removeRangeNotifier(this);