I have an application that monitors for iBeacons. When the application terminates from a suspended state and then enters a beacon region that it is monitoring for, it can sometimes take a very long time (sometimes up to 1 minute) for the application to wake up (call didEnterRegion or didExitRegion). Is there anything I can do about this? Here is the code I am using when the application enters the background
- (void)extendBackgroundRunningTime {
if (_backgroundTask != UIBackgroundTaskInvalid) {
// if we are in here, that means the background task is already running.
// don't restart it.
return;
}
// Attempt to extend background time by initializing a placeholder background thread
__block Boolean self_terminate = YES;
_backgroundTask = [[UIApplication sharedApplication] beginBackgroundTaskWithName:kBISBeaconManagerKeyBackgroundTask expirationHandler:^{
// Background task expired, completion
if (self_terminate) {
NSLog(@"Application Terminated");
[[UIApplication sharedApplication] endBackgroundTask:_backgroundTask];
_backgroundTask = UIBackgroundTaskInvalid;
}
}];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// Background task started
});
}
A few thoughts:
The code shown in the question extends background beacon ranging time. It does not do anything to affect beacon monitoring (monitoring APIs are what provide didEnterRegion and didExitRegion callbacks).
Monitoring response times can vary quite a bit. Best case scenario, didEnterRegion callbacks will fire within a second or two of the beacon being in range. The didExitRegion event is always delayed by an additional 30 seconds (that's how long iOS waits before deciding a beacon is no longer visible.)
The best case scenario times are not met if all 30 Bluetooth hardware acceleration slots are taken. These slots are across all apps on the phone. If your app tries to monitor a beacon region when all slots are taken, detection times fall back to software backup cycles and can take 15 minutes.
See here for more info: http://developer.radiusnetworks.com/2014/03/12/ios7-1-background-detection-times.html
http://developer.radiusnetworks.com/2015/04/21/max-beacon-regions-ios.html