I have a program in swift that detect when a beacon is in range and out of range even though the app is in background.
The problem I have is it takes about 30 secs for didExitRegion to get triggered. I know this 30 secs is a non-adjustable value, but for my app, 30 secs is a long way to walk away from the beacon to get notification. What other alternative I have to solve this issue?
Is it possible to monitor for a Major value while the app is in the background, and give it like 10 secs for false positive and if still not seeing the major value, then I know I am out of range?
If answer is yes, any sample to show as how to monitor on the major value?
The alternative is to do beacon ranging, and create your own exit event when you haven't seen any beacons in the region for a smaller time period, say 10 seconds.
However, there is a big limitation to doing this:
In the background on iOS, you can only range for beacons for a limited time period after a region entry event, or after the app gets pushed to the background. By default, this is only 5 seconds, but it can be extended up to 3 minutes programatically. After that three minutes, you won't get any more ranging callbacks, so if you haven't detected a 10-second exit by that time, then you will have to rely on the regular didExitRegion
event.
If you use this technique, you need to realize that iOS still reports the presence of the beacon for awhile after it has not been detected, with a proximity of UNKNOWN.
Here is an example of how to do this in your ranging callback:
var lastBeaconDetectionTime = 0.0
var exitFired = false
func locationManager(manager: CLLocationManager, didRangeBeacons beacons: [CLBeacon], inRegion region: CLBeaconRegion) {
for beacon in beacons {
if beacon.proximity != CLProximity.Unknown {
lastBeaconDetectionTime = NSDate().timeIntervalSince1970
exitFired = false
}
}
if NSDate().timeIntervalSince1970 - lastBeaconDetectionTime > 10.0 && !exitFired {
exitFired = true
// TODO: Add logic for region exit firing after 10 seconds
}
}