Ranging Beacons仅适用于app运行?

Aar*_*her 24 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...
}
Run Code Online (Sandbox Code Playgroud)

dav*_*ung 11

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

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

  1. 您的应用只会在几秒钟后启动到后台.(尝试在AppDelegate中添加NSLog语句applicationDidEnterBackground和其他方法以查看发生了什么.)
  2. iOS可以自己花时间来决定你输入的CLBeaconRegion.我看到它需要长达四分钟.

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

编辑:进一步的调查证明,这notifyEntryStateOnDisplay对后台监控没有影响,所以无论你是否有这个标志,上述都应该有效.请参阅此详细说明和您可能遇到的延迟的讨论


Teo*_*aru 8

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

  1. 打开项目设置- >功能- >背景模式- >切换Location UpdatesUses Bluetooth LE accessoriesON.

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

    locationManager!.delegate = self
    locationManager!.pausesLocationUpdatesAutomatically = false
    locationManager!.allowsBackgroundLocationUpdates = true
    
    Run Code Online (Sandbox Code Playgroud)
  3. 为信标和监控区域开始测距:

    locationManager!.startMonitoringForRegion(yourBeaconRegion)
    locationManager!.startRangingBeaconsInRegion(yourBeaconRegion)
    locationManager!.startUpdatingLocation()
    
    // Optionally for notifications
    UIApplication.sharedApplication().registerUserNotificationSettings(
        UIUserNotificationSettings(forTypes: .Alert, categories: nil))
    
    Run Code Online (Sandbox Code Playgroud)
  4. 落实CLLocationManagerDelegate并在didEnterRegion同时发送startRangingBeaconsInRegion()startUpdatingLocation()消息(可选发送通知为好),并设置stopRangingBeaconsInRegion()stopUpdatingLocation()didExitRegion

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

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


man*_*ath 7

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

  1. 对于任何CLBeaconRegion始终保持监控,在后台或前台并保持notifyEntryStateOnDisplay = YES
  2. notifyEntryStateOnDisplaylocationManager: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];
    }

}
Run Code Online (Sandbox Code Playgroud)

我希望这有帮助.

  • 是的,它可以与任何对象一起使用 - 请参阅此示例https://github.com/manishnath/iBeaconCenter (2认同)

Tom*_*voy 1

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