【问题标题】:iBeacon ranging in the background?iBeacon 在后台测距?
【发布时间】:2014-01-27 19:31:07
【问题描述】:

我已经开始使用 estimotes 作为信标来测试 iBeacon。

一切都运行得很好,但我正在努力让应用程序在后台正常运行。

self.region = [[CLBeaconRegion alloc] initWithProximityUUID:self.uuid identifier: self.deviceID];
self.region.notifyEntryStateOnDisplay = YES;
[self.locationManager startMonitoringForRegion:self.region];

所以这是基本设置,对于我的测试应用程序,我想在我的手机靠近信标时显示本地通知。我的问题是除非我包含下面的行,否则它将无法工作。

[self.locationManager startUpdatingLocation];

谁能解释为什么会这样,或者我是否遗漏了有关 iBeacons 的某些内容?

【问题讨论】:

  • 嗨,您在 startUpdatingLocation 上的问题得到答案了吗?

标签: ios core-location ibeacon


【解决方案1】:

不,你没有错过任何东西。在后台,您的应用程序只有很少的时间来进行测距。根据我的个人经验,您会收到大约 3 到 5 次范围回调。就是这样。

【讨论】:

  • 这并不是我问题的真正答案。为什么我需要最后一行代码来让应用在后台运行时显示通知?我在想我只需要 [self.locationManager startMonitoringForRegion:self.region];
【解决方案2】:

你错了。您无需调用 startUpdatingLocation 即可在后台调用。

当您在后台时,当您进入某个区域时需要更长的时间才能收到通知。如果你想要测距调用,你也必须发出 startRangingBeaconsInRegion 调用。正如另一张海报指出的那样,当检测到新的信标时,您只会从后台收到几秒钟的测距呼叫。 (您会得到一个 didEnterRegion,然后是几个测距调用,然后您的应用会重新进入睡眠状态。)

【讨论】:

  • 好的。谢谢。我试图弄清楚是否可以通过走廊跟踪用户的移动。据我现在的理解,如果不使用 [locationManager startUpdatingLocation] 是不可能的,因为该应用程序需要在其他的前台运行才能使测距工作。对吗?
  • 是的,您需要在后台运行才能获得信标测距呼叫超过几秒钟。我不认为 startUpdatingLocation 会让你/让你处于前台。
  • 哎呀,我之前的评论应该是“是的,你需要在前台运行......”
【解决方案3】:

你不需要调用startUpdatingLocation方法。

startMonitoringForRegion 方法仅开始监视区域并调用didStartMonitoringForRegion 委托方法让您知道信标何时进入/退出该区域。

您需要调用startRangingBeaconsInRegion 方法,该方法调用didRangeBeacons 委托方法,该方法为您提供检测到的带有UUID、主要、次要、rssi 信标信息的信标数组。

对于后台执行,只需使用UIBackgroundTaskIdentifier,您的代码也将在后台运行。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-28
    • 1970-01-01
    相关资源
    最近更新 更多