测距信标只适用于应用程序运行?

我很难得到这个工作,当应用程序没有运行。 我有locationManager:didRangeBeacons:inRegion:实现,当应用程序在前台或后台运行时调用它,但是当我退出应用程序并locking屏幕时,它似乎没有任何操作。 位置服务图标消失,我不知道我进入了一个灯塔范围。 LocalNotification是否仍然有效?

我有位置更新和使用在背景模式(XCode 5)中select的蓝牙LE配件,我不认为我需要它们。

任何帮助不胜感激。

 -(void)watchForEvents { // this is called from application:didFinishLaunchingWithOptions id class = NSClassFromString(@"CLBeaconRegion"); if (!class) { return; } CLBeaconRegion * rflBeacon = [[CLBeaconRegion alloc] initWithProximityUUID:kBeaconUUID identifier:kBeaconString]; rflBeacon.notifyOnEntry = YES; rflBeacon.notifyOnExit = NO; self.locationManager = [[CLLocationManager alloc] init]; self.locationManager.delegate = self; [self.locationManager startRangingBeaconsInRegion:rflBeacon]; [self.locationManager startMonitoringForRegion:rflBeacon]; } -(void)locationManager:(CLLocationManager *)manager didRangeBeacons:(NSArray *)beacons inRegion:(CLBeaconRegion *)region { if (beacons.count == 0 || eventRanged) { // breakpoint set here for testing return; } eventRanged = YES; if (backgroundMode) { // this is set in the EnterBackground/Foreground delegate calls UILocalNotification *notification = [[UILocalNotification alloc] init]; notification.alertBody = [NSString stringWithFormat:@"Welcome to the %@ event.",region.identifier]; notification.soundName = UILocalNotificationDefaultSoundName; [[UIApplication sharedApplication] presentLocalNotificationNow:notification]; } // normal processing here... } 

监控可以启动未运行的应用程序。 测距不能。

监控启动你的应用程序的关键是在你的CLBeaconRegion上设置这个logging不好的标志:r egion.notifyEntryStateOnDisplay = YES; 即使完全重新启动手机,这也可以启动您的应用程序在区域转换。 但是有一些警告:

  1. 您的应用只能在几秒钟内启动到后台。 (尝试添加NSLog语句到applicationDidEnterBackground和你的AppDelegate中的其他方法,看看发生了什么。)
  2. iOS可以采取自己的甜蜜时间来决定你进入一个CLBeaconRegion 。 我已经看到这花了四分钟。

就范围而言,即使您无法唤醒您的应用程序,也可以让您的应用程序同时进行监视和测距。 如果监控唤醒您的应用程序并将其放入后台几秒钟,则会立即启动各种callback。 这使您有机会在应用程序仍在运行时执行任何快速测距操作。

编辑:进一步调查certificatenotifyEntryStateOnDisplay没有影响后台监控,所以上述应该工作,不pipe你是否有这个标志。 请参阅关于您可能遇到的延迟的详细说明和讨论

以下是您需要遵循的后台进程范围:

  1. 对于任何CLBeaconRegion始终保持监视,在后台或前台,并保持notifyEntryStateOnDisplay = YES
  2. notifyEntryStateOnDisplay调用locationManager:didDetermineState:forRegion:在后台,所以实现这个委托调用…

…喜欢这个:

 - (void)locationManager:(CLLocationManager *)manager didDetermineState:(CLRegionState)state forRegion:(CLRegion *)region{ if (state == CLRegionStateInside) { //Start Ranging [manager startRangingBeaconsInRegion:region]; } else{ //Stop Ranging [manager stopRangingBeaconsInRegion:region]; } } 

我希望这有帮助。

iOS 9的代码通过使用位置更新在后台范围信标:

  1. 打开项目设置 – >function – >背景模式 – >切换Location UpdatesUses Bluetooth LE accessories ON

  2. 创build一个CLLocationManager ,请求Always监视授权(不要忘记添加Application does not run in background NO和应用程序的info.plist中的NSLocationAlwaysUsageDescription )并设置以下属性:

     locationManager!.delegate = self locationManager!.pausesLocationUpdatesAutomatically = false locationManager!.allowsBackgroundLocationUpdates = true; 
  3. 开始测量信标和监控区域:

     locationManager!.startMonitoringForRegion(yourBeaconRegion) locationManager!.startRangingBeaconsInRegion(yourBeaconRegion) locationManager!.startUpdatingLocation() // Optionally for notifications UIApplication.sharedApplication().registerUserNotificationSettings( UIUserNotificationSettings(forTypes: .Alert, categories: nil)) 
  4. 实现CLLocationManagerDelegate并在didEnterRegion发送startRangingBeaconsInRegion()startUpdatingLocation()消息(可选地发送通知),并在stopRangingBeaconsInRegion()设置stopRangingBeaconsInRegion()stopUpdatingLocation()

请注意,此解决scheme的工作原理,但由于电池消耗和客户隐私,不推荐使用苹果电脑!

更多这里: https : //community.estimote.com/hc/en-us/articles/203914068-Is-it-possible-to-use-beacon-ranging-in-the-background-

你在这里做两个独立的操作 – “测距”信标和监测一个地区。 您可以在后台监视某个区域,但不能监视范围信标。

因此,您的locationManager:didRangeBeacons:inRegion:将不会在后台调用。 相反,您对startMonitoringForRegion的调用将导致调用以下一个/一些方法:

 – locationManager:didEnterRegion: – locationManager:didExitRegion: – locationManager:didDetermineState:forRegion: 

这些将在后台被调用。 你可以在这一点触发一个本地通知,就像你原来的代码一样。

如果您只是想在进入灯塔区域时收到通知,那么您的应用现在应该会醒来。 我知道的唯一的背景限制实际上托pipeiOS设备上的iBeacon。 在这种情况下,应用程序将需要在前台实际打开。 对于这种情况,你最好只做直接的CoreBluetooth CBPeripheralManager实现。 这样你就能在后台有一些广告能力。

你可以在背景中做范围。 但是,有一个问题。 用户必须将应用程序放到前台,这是您开始测量的时间。 然后,即使用户locking了屏幕,并且应用程序在后台进入,它将继续执行范围,并永远每1秒调用一次didRangeBeaconscallback。

要打开背景位置更新,请select您的项目,转到信息并添加一个名为“所需的背景模式”的行。 然后添加相关项目:位置更新,通过核心蓝牙通信/共享数据等。

现在,当您进入该地区时,一旦您收到回拨,您的应用程序将在后台唤醒并通知用户。 用户打开应用程序将应用程序放在前台。 此时,您可以通过调用startRangingBeaconsInRegion来启动测距过程。 另外,调用startUpdatingLocation方法将更新后台的位置。