didExitRegion not fired when turn off bluetooth

2019-06-20 14:45发布

问题:

Im building a simple ios app with IBeacon, I'm monitoring a region but I have some issues with enter and exit events.

If I go into a region the callback didEnterRegion is fired, but being within the region, turning off bluetooth doesn't fire didExitRegion callback. Is this the expected behavior?

This is an issue since I have to be able to detect when the user exits the region. Any idea?

Thanks

回答1:

For testing purposes you must turn off your beacons or move out of range to get a didExitRegion event.

Once CoreLocation has determined it is inside a CLBeaconRegion it will only change state to being outside the region after it has a chance to do a Bluetooth scan for 3 seconds during which time no beacons matching the CLBeaconRegion are detected. If Bluetooth is not on, it cannot scan to make this determination.

If you want to force an exit in your app in this case, consider listening for CoreBluetooth lifecycle events. On power off, you can deregister each CLBeaconRegion, fire your didExitRegion logic manually, then deregister them with CoreLocation.