iBeacon ranging in the background? - ios

I have started to test out iBeacons using estimotes as beacons.
It's all running pretty good, but i'm struggling with getting the app to run properly in the background.
self.region = [[CLBeaconRegion alloc] initWithProximityUUID:self.uuid identifier: self.deviceID];
self.region.notifyEntryStateOnDisplay = YES;
[self.locationManager startMonitoringForRegion:self.region];
So this is the basic setup and for my test app i want to show a local notification when my phone is in immediate proximity of the beacon. My problem is that it won't work unless i include the line below.
[self.locationManager startUpdatingLocation];
Can anyone explain why that is or if i'm missing something about iBeacons?

You are mistaken. You don't need to call startUpdatingLocation in order to be called in the background.
When you're in the background it takes longer to get notified when you enter a region. If you want ranging calls, you have to issue the startRangingBeaconsInRegion call as well. As the other poster pointed out, you will only get a few seconds of ranging calls from the background when a new beacon is detected. (You get a didEnterRegion, followed by a few ranging calls, and then your app goes back to sleep.)

No, you are not missing anything. In background you app gets a very small amount of time to do ranging. From my personal experience, you get about 3 to 5 ranging callbacks. Thats it.

You do not need to call startUpdatingLocation method.
startMonitoringForRegion method starts monitoring the region only and call the didStartMonitoringForRegion delegate method to let you know when beacon enter/exit the region.
You need to call startRangingBeaconsInRegion method, which calls didRangeBeacons delegate method which gives you array of detected beacons with UUID, major, minor, rssi info of beacons.
For background execution, just use UIBackgroundTaskIdentifier and your code will work in background as well.
- (void)applicationDidEnterBackground:(UIApplication *)application
{
NSLog(#"=== DID ENTER BACKGROUND ===");
UIBackgroundTaskIdentifier bgTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
NSLog(#"End of tolerate time. Application should be suspended now if we do not ask more 'tolerance'");
}];
if (bgTask == UIBackgroundTaskInvalid) {
NSLog(#"This application does not support background mode");
}
else {
//if application supports background mode, we'll see this log.
NSLog(#"Application will continue to run in background");
}
}

Related

didExitRegion: 1.5 to 2 minute delay of called when my application is killed or in background in ios 10

i have to use estimote location beacon,iphone 5s,ios version 10.
didEnterRegion: method also called 30 to 40 seconds delay.
i have to use below code for monitoring ibeacon.
NSUUID *uuid = [[NSUUID alloc] initWithUUIDString:#"CFC52BF4-FD33-4569-B4B5-5E9C220514A2"];
CLBeaconRegion *region = [[CLBeaconRegion alloc] initWithProximityUUID:uuid major:1 identifier:#"Technostacks23"];
region.notifyOnEntry = YES;
region.notifyOnExit = YES;
// launch app when display is turned on and inside region
region.notifyEntryStateOnDisplay = YES;
if ([CLLocationManager isMonitoringAvailableForClass:[CLBeaconRegion class]])
{
[_locationManager startMonitoringForRegion:region];
[_locationManager startRangingBeaconsInRegion:region];
}
[self.locationManager startUpdatingLocation];
#pragma mark - CoreLocation Delegate method
-(void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region {
//local notification fire
}
-(void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region {
//local notification fire
}
please guys tell me how to tackle this scenario.
Thanks
I don't think you can monitor a region at the same time you are ranging. I had this problem on a screen I made where I wanted to show the user if they are in range of the Beacon. I had to stop monitoring until they left the screen, then restart monitoring. If you don't need the events for ranging (how close the user is to the Beacon), I would leave that line off, it isn't necessary to get -didEnter and -didExit calls. You also shouldn't need -startUpdatingLocation either. That is only for active location calls.
Entry events typically fire right away. Exit events do have a 20-30 second delay once you lose signal to the Beacon.
It may be that you are burning up your background run time monitoring, after you run out of time it stops, opening the door for you get monitoring events.
Call the startRangingBeaconsInRegion method in the didEnterRegion delegate method so it wont range for beacons when it is not already in the beacons region this will help in power and battery consumption and i believe it would speed up region detection a little bit.
Edit your advertising interval of the beacon make it 350ms or less.
Check in your iPhone Settings tab for the apps that allow background App Refresh and Location Always and just reverse them to don't allow and make a test see if there would be a difference in detection performance.
But in all cases the 30 - 40 sec delay i find it normal depending on the number of apps that are already processing in the background and running out resources plus it would be even better depending on your app model to start notifying for a region entry after a couple of seconds to make sure also that he is in the region and didn't just pass by so quickly "Im talking for a super market model for instance".
But if this is not your case and you want to detect a region just by passing by i think the fastest you can get will be 15 - 20 sec depending on your device capability, speed, other background processing apps, and the beacons advertising time interval.

app getting killed if using beginBackgroundTaskWithExpirationHandler IOS

I am using beginBackgroundTaskWithExpirationHandler method in the applicationDidEnterBackground delegate method for keeping the NSTimer to keep running. But then the application gets killed after a long time if left in background for a long time (in my case 7-10 mins). I don't want my app to be getting killed and also I want the timer to run if in background. How do I get rid of this issue. Following is the code that I have written in the applicationDidEnterBackground method
- (void)applicationDidEnterBackground:(UIApplication *)application {
if ([application respondsToSelector:#selector(setKeepAliveTimeout:handler:)]) {
[application setKeepAliveTimeout:600 handler:^{
DDLogVerbose(#"KeepAliveHandler");
// Do other keep alive stuff here.
}];
}
/*
* The following code is used to make the app running in background state as certain features
* (eg: NSTimer) doesn not run if its in background or if the phone is locked
*/
UIBackgroundTaskIdentifier locationUpdater =[[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
[[UIApplication sharedApplication] endBackgroundTask:locationUpdater];
} ];
}
The beginBackgroundTaskWithExpirationHandler is only intended to let you complete a finite length task of a few minutes after the app leaves foreground. iOS, quite deliberately, doesn't let your app run in background perpetually except for very narrow situations (e.g. VOIP, audio app, navigation app) or for narrow functional needs (significant change location services, background fetch, etc.). But you cannot just run arbitrary code perpetually in the background.
For a discussion of the options, see the Background Execution section of the App Programming Guide for iOS.

iBeacons scanning in background mode in iOS?

Please help me!
Can you explain how can I continue scan iBeacons after changing app mode to background?
Please write an example code (iOS)
Thanks)
You can't indefinitely scan for beacons while backgrounded. You can typically range for about 5 seconds if you detect that you enter a CLBeaconRegion, and you can configure your location manager to notifyEntryStateOnDisplay (it defaults to NO), so when the screen turns on, such as when the user hits the home button, you will get about 5 seconds up range updates then.
The code needed to monitor for beacons is the same whether you are in the background or the foreground. You set it up like this:
CLBeaconRegion *beaconRegion = [[CLBeaconRegion alloc]
initWithProximityUUID:proximityUUID
identifier:identifier];
[locationManager startMonitoringForRegion:beaconRegion];
And then get callbacks to the locationManager:didEnterRegion: and locationManager:didExitRegion:
You can read more details here:
https://developer.apple.com/library/ios/documentation/userexperience/Conceptual/LocationAwarenessPG/RegionMonitoring/RegionMonitoring.html

iBeacon monitoring implementation ios7.1

I’ve a problem about iBeacon monitoring implementation. I fire a local notification when locationManager:didDetermineState:forRegion: method is called. When application goes in background I don’t get any local notification at all but they came all at once when I active the screen pushing the home button. According to time I leave sleep the device I can get up to tens notification always when I wake it up. How is that possible? Anyone had the same problem?
I use iPhone 5S and 5C with iOS 7.1. The local notification is set in this way:
- (void)locationManager:(CLLocationManager *)manager didDetermineState:(CLRegionState)state forRegion:(CLRegion *)region{
UILocalNotification *localNot = [[UILocalNotification alloc] init];
localNot.alertBody = [NSString stringWithFormat:#"Region state %d determined", state];
localNot.alertAction = #"Go for it!";
localNot.soundName = UILocalNotificationDefaultSoundName;
localNot.fireDate = nil;
[[UIApplication sharedApplication] presentLocalNotificationNow:localNot];
}
I suspect that you are not actually doing any background detections at all, and the reason you see the notifications when you hit the home button is because you have the notifyEntryStateOnDisplay flag set, which causes you to get an extra callback to didDetermineState: forRegion: whenever the screen comes on, for every region you are monitoring with the flag set.
Why do you not get callbacks in the background? You may need to wait up to 15 minutes to detect an iBeacon in the background, even on iOS 7.1. See here.

didRangeBeacons method not getting called when display is Off

i'm working with ibeacons from couple of weeks, i was trying to post some local notifications , when the iphone hits the beaconregion(when proximity near).
It was working fine when the app is in background with locked and with display on, but when my display turns black ,
didRangeBeacons method stopped getting called.
I know by using the
region.notifyEntryStateOnDisplay = true;
we can get notified while the display on.
Is there any way that i can achieve posting notification while the app is background with locked and display off.
Please help me out.
It is totally possible to range beacons in background.
You can go around the limitation by setting this up together with your current location manager or creating a parallel one (which makes no difference, I think). You'll also need to enable background location updates capabilities at your project.
locationManager2 = [[CLLocationManager alloc] init];
locationManager2.delegate = self;
locationManager2.desiredAccuracy = kCLLocationAccuracyKilometer;
[locationManager2 startUpdatingLocation];
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{
//Do nothing here, but enjoy ranging callbacks in background :-)
}
Now, you'll need to have a good explanation when Apple ask you why do you need the background location updates mode in your app. They're very picky with that.
If you have requested both beacon entry notifications and ranging, and you ENTER a new region while the screen is locked, you will get a didEnterRegion message (or possibly the didDetermineState message) followed by about 5 seconds of ranging messages. If the user doesn't wake up the device during those 5 seconds, the ranging messages stop.
Thus you can't really filter based on proximity to a beacon from the background. If you try to wait until you get a range value of near then more than likely you won't get it because the device stops sending your ranging messages before the user gets that close. You then won't get any more notifications about that beacon region until the user either leaves the region again or wakes up the phone and brings your app back to the foreground.
An app I'm working on posts a local notification when it receives a didEnterRegion (or didDetermineState) message. That causes the screen to light up, but doesn't seem to extend the amount of time you get ranging notices.
No, this is by design and reflects iOS's goal of conserving battery. You can only count on didRangeBeacons: while the app is foregrounded.
David Young has a pretty thorough writeup that may help clarify.
I have the same problem which Madhu has. Instead of didUpdateToLocation, I used didUpdateLocations which is worked for me. Now I am able to range beacons while the app is in background or terminated with locked and display off.
On iOS 8.4
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
//Do nothing here
}

Resources