测距信标只适用于应用程序运行?
我很难得到这个工作,当应用程序没有运行。 我有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;
- 您的应用只能在几秒钟内启动到后台。 (尝试添加
NSLog
语句到applicationDidEnterBackground
和你的AppDelegate中的其他方法,看看发生了什么。) - iOS可以采取自己的甜蜜时间来决定你进入一个
CLBeaconRegion
。 我已经看到这花了四分钟。
就范围而言,即使您无法唤醒您的应用程序,也可以让您的应用程序同时进行监视和测距。 如果监控唤醒您的应用程序并将其放入后台几秒钟,则会立即启动各种callback。 这使您有机会在应用程序仍在运行时执行任何快速测距操作。
编辑:进一步调查certificatenotifyEntryStateOnDisplay
没有影响后台监控,所以上述应该工作,不pipe你是否有这个标志。 请参阅关于您可能遇到的延迟的详细说明和讨论
以下是您需要遵循的后台进程范围:
- 对于任何
CLBeaconRegion
始终保持监视,在后台或前台,并保持notifyEntryStateOnDisplay = YES
-
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的代码通过使用位置更新在后台范围信标:
-
打开项目设置 – >function – >背景模式 – >切换
Location Updates
并Uses Bluetooth LE accessories
ON
。 -
创build一个
CLLocationManager
,请求Always
监视授权(不要忘记添加Application does not run in background
NO
和应用程序的info.plist
中的NSLocationAlwaysUsageDescription
)并设置以下属性:locationManager!.delegate = self locationManager!.pausesLocationUpdatesAutomatically = false locationManager!.allowsBackgroundLocationUpdates = true;
-
开始测量信标和监控区域:
locationManager!.startMonitoringForRegion(yourBeaconRegion) locationManager!.startRangingBeaconsInRegion(yourBeaconRegion) locationManager!.startUpdatingLocation() // Optionally for notifications UIApplication.sharedApplication().registerUserNotificationSettings( UIUserNotificationSettings(forTypes: .Alert, categories: nil))
-
实现
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秒调用一次didRangeBeacons
callback。
要打开背景位置更新,请select您的项目,转到信息并添加一个名为“所需的背景模式”的行。 然后添加相关项目:位置更新,通过核心蓝牙通信/共享数据等。
现在,当您进入该地区时,一旦您收到回拨,您的应用程序将在后台唤醒并通知用户。 用户打开应用程序将应用程序放在前台。 此时,您可以通过调用startRangingBeaconsInRegion
来启动测距过程。 另外,调用startUpdatingLocation
方法将更新后台的位置。
- iBeacon基础和实施
- 如何发送数据到iPhone变成了一个iBeacon?
- iOS – iBeacon – 在iOS 7.1更新后,在后台广告(广播)作为信标
- iBeacons:如果app在后台,locationManager:didEnterRegion:仅在locking屏幕显示时被调用
- Qualcomm Gimbal Geofence Communication
- iBeacon:didEnterRegion和didDetermineState(CLRegionStateInside)之间有什么区别
- CoreLocation线程崩溃崩溃:com.apple.CoreLocation.ConnectionClient.0x16fcb870.events
- iBeacon应用程序 – 同时接收/发送
- 在iOS的背景中的烽火台