在后台关闭 iBeacons 监控并关闭应用程序

Close iBeacons monitoring on background and with app closed

我正在使用 IBeacon 构建一个简单的 ios 应用程序,我正在使用 startMonitoringForRegion 来检测信标。那是 AppDelegte.m 代码:

    NSUUID *beaconUUID = [[NSUUID alloc] initWithUUIDString:@"0040C159-12F6-4FC3-9189-87C069FFE5CF"];
    NSString *regionIdentifier = @"iBeacons region 1";
    self.beaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID: beaconUUID identifier: regionIdentifier ];
    self.beaconRegion.notifyEntryStateOnDisplay = YES;
    self.locationManager = [[CLLocationManager alloc]init];
    self.locationManager.delegate = self;
    self.locationManager.pausesLocationUpdatesAutomatically = NO;
    [self.locationManager startMonitoringForRegion:self.beaconRegion];
    [self.locationManager startRangingBeaconsInRegion:beaconRegion];

然后在 didDetermineState 回调:

- (void)locationManager:(CLLocationManager *)manager didDetermineState:(CLRegionState)state forRegion:(CLRegion *)region{

    if (state == CLRegionStateInside) {
        //Start Ranging
        [manager startRangingBeaconsInRegion:(CLBeaconRegion*) region];
        NSLog(@"didDetermineState -->enter region");
    }
    else{
        //Stop Ranging
        [manager stopRangingBeaconsInRegion:(CLBeaconRegion*) region];
        NSLog(@"didDetermineState -->exit region");
    }

}

当 iBeacon 彼此远离时,这在前台、后台或应用程序关闭时工作正常。但是当 iBeacon 彼此靠近时,我发现了一些问题。

在执行了几次测试后,我得出结论,RangingBeaconsInRegion 无法在后台或应用程序关闭时工作,只有监控工作。

当您进入或退出信标区域时,应用程序将在后台启动约5秒,并回调didDetermineState:forRegion:方法。您可以在这五秒钟内在后台进行测距,之后 iOS 将再次暂停您的应用程序。

当 iBeacon 彼此相距很远时,这是完美的。当 iBeacon 距离太近,回调 didDetermineState: not called for the exit of the iBeacon region before you enter the next iBeacon region.

有什么办法可以强制手动退出区域吗?或者其他一些方法来解决这个问题?

谢谢!

这是一个真正的问题。当信标具有重叠传输并且您的区域包含所有这些时,您将不会收到 entry/exit 个事件,因为一个消失另一个出现。

有多种方法可以解决此问题,但其中 none 是一个完美的解决方案。您可以:

  • 在后台将测距延长最多 3 分钟。
  • 构建您的信标标识符和区域,以便您获得尽可能多的 exit/entry 事件。

您可以在此处阅读第一种技术的更详细讨论:http://developer.radiusnetworks.com/2014/11/13/extending-background-ranging-on-ios.html