Ranging Beacons仅适用于app运行?

时间:2013-10-02 16:18:57

标签: ios ios7 ibeacon

当应用程序未运行时,我无法使用此功能。我已经实现locationManager:didRangeBeacons:inRegion:,当应用程序在前台或后台运行时会调用它,但是当我退出应用程序并锁定屏幕时它似乎没有做任何事情。位置服务图标消失了,我永远不知道我进入了信标范围。 LocalNotification是否仍然可以使用?

我在背景模式(XCode 5)中选择了位置更新和使用蓝牙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...
}

6 个答案:

答案 0 :(得分:11)

监控可以启动未运行的应用。范围不能。

<德尔> 监控启动应用程序的关键是在CLBeaconRegion上设置这个记录不佳的标记:r egion.notifyEntryStateOnDisplay = YES; 即使在完全重启手机后,这也可以在区域转换时启动您的应用。但有一些警告:

  1. 您的应用只会在几秒钟内启动到后台。 (尝试将NSLog语句添加到applicationDidEnterBackground以及AppDelegate中的其他方法以查看正在发生的事情。)
  2. iOS可以自己花时间来决定您输入CLBeaconRegion。我看到它需要长达四分钟。
  3. 就测距而言,即使您无法唤醒应用程序,也可以让您的应用同时进行监控和测距。如果监控唤醒您的应用并将其置于后台几秒钟,则会立即启动各种回调。这使您有机会在应用程序仍在运行时执行任何快速测距操作。

    编辑:进一步调查证明notifyEntryStateOnDisplay对后台监控没有影响,因此无论您是否拥有此标志,上述情况都应该有效。请参阅this detailed explanation and discussion of delays you may experience

答案 1 :(得分:7)

以下是您需要遵循的背景范围:

  1. 对于任何CLBeaconRegion始终保持监控,在后台或前台,并保持notifyEntryStateOnDisplay = YES
  2. notifyEntryStateOnDisplay在后​​台调用locationManager:didDetermineState:forRegion:,因此请执行此委托调用...
  3. ...像这样:

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

    我希望这会有所帮助。

答案 2 :(得分:7)

使用位置更新在iOS 9中编制后台范围信标的代码:

  1. 打开项目设置 - &gt;能力 - &gt;背景模式 - &gt;将Location UpdatesUses Bluetooth LE accessories切换为ON

  2. 创建CLLocationManager,请求Always监控授权(不要忘记将Application does not run in background添加到NONSLocationAlwaysUsageDescription应用的info.plist)并设置以下属性:

    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()条消息(也可选择发送通知)并设置{{1} }和stopRangingBeaconsInRegion()

  5. 中的stopUpdatingLocation()

    请注意此解决方案有效,但由于电池消耗和客户隐私,Apple不建议使用此解决方案!

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

答案 3 :(得分:2)

你在这里做两个独立的操作 - '测距'信标和监控区域。您可以监视背景中的某个区域,但不能监视范围信标。

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

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

这些将在后台调用。您可以在此时触发本地通知,就像在原始代码中一样。

答案 4 :(得分:1)

如果您只是想在进入信标区域时收到通知,您的应用程序当前应该会被唤醒。我知道的唯一背景限制涉及在iOS设备上实际托管iBeacon。在这种情况下,应用程序需要在前台进行物理打开。对于这种情况,你最好只做直接的CoreBluetooth CBPeripheralManager实现。这样你就可以在后台拥有一些广告能力。

答案 5 :(得分:-2)

你可以在后台进行测距。然而,有一个问题。用户必须将应用程序带到前台,即开始测距时。然后,即使用户锁定屏幕并且应用程序进入后台,它也将继续范围并永远每1秒调用didRangeBeacons回调。

要启用后台位置更新,请选择您的项目,转到信息并添加一个名为“所需背景模式”的行。然后添加相关项目:位置更新,通过corebluetooth通信/共享数据等

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