iBeacon ranging in the background?

ぃ、小莉子 提交于 2019-12-01 23:32:49

问题


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?


回答1:


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.)




回答2:


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.




回答3:


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");
    }
}


来源:https://stackoverflow.com/questions/21016992/ibeacon-ranging-in-the-background

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!