【问题标题】:iBeacon monitoring not working properlyiBeacon 监控无法正常工作
【发布时间】:2015-05-22 12:49:22
【问题描述】:

当iOS设备范围内有信标时,我会收到通知(CLRegionStateInside)并可以开始测距。这工作正常。但是,当测距开始并且 iOS 设备不再在范围内时,我不会收到通知(状态不会更改为 CLRegionStateOutside)。不在前台或后台。

还有didEnterRegiondidExitRegion 永远不会被调用。当状态为CLRegionStateInside 时,我开始在didDeterminState 范围内。

  • 我确实为我的应用启用了后台刷新设置。
  • 当我第一次启动应用程序时,我确实收到了一条询问位置许可的警报。

所以基本上,我可以开始测距,但我无法停止测距,因为状态不会更改为CLRegionStateOutside

我在 iPhone 4s 上使用 Xcode 6.3.1、iOS 8.3。

这是我的代码:

初始化:

- (id)init{
    self = [super init];
    self.locationManager = [[CLLocationManager alloc] init];
    self.locationManager.delegate = self;
    if ([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
        [self.locationManager requestAlwaysAuthorization]; //or requestWhenInUseAuthorization
    }

    NSUUID *uuid = [[NSUUID alloc] initWithUUIDString:@"B75FA2E9-3D02-480A-B05E-0C79DBB922AD"];
    self.myBeaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:uuid
                                                             identifier:@"TESTREGIO"];

    [self.locationManager startMonitoringForRegion:self.myBeaconRegion];
    [self.locationManager requestStateForRegion:self.myBeaconRegion];
    self.myBeaconRegion.notifyEntryStateOnDisplay = YES;
    self.myBeaconRegion.notifyOnEntry = YES;
    self.myBeaconRegion.notifyOnExit = YES;

    return self;
}

确定:

- (void)locationManager:(CLLocationManager *)manager didDetermineState:(CLRegionState)state forRegion:(CLRegion *)region {
    if (state == CLRegionStateInside)
    {
        [self.locationManager startRangingBeaconsInRegion:self.myBeaconRegion];

    }else{
        [self.locationManager stopRangingBeaconsInRegion:self.myBeaconRegion];
    }
}

DIDENTERREGION 和 DIDEXITREGION:

   - (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion*)region
    {
        NSLog(@"DidenterRegion================================================");
    }

    - (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region {

        NSLog(@"DidexitRegion================================================");
    }

【问题讨论】:

    标签: ios objective-c monitoring ibeacon


    【解决方案1】:

    除非您已经在某个区域内,否则您不会收到 didExitRegion 通知。我建议您单独跟踪您正在寻找的区域的状态。

    我不明白你在 didDetermineState 中做什么。你开始在 init() 中为你的信标测距。您无需重新开始测距。

    【讨论】:

    • 我的错误,开始测距不应该发生在 init() 中,我删除了。当然,如果您首先不在该区域,则无法退出该区域。但就我而言,我在一个地区。我能够进入一个区域并开始测距并从该区域内的信标获取范围更新。因此,进入一个区域有效,但退出该区域无效。
    【解决方案2】:

    一些提示:

    • 您无需请求任何后台权限即可使用。

    • 当设备在前台测距时不再检测到信标后 3 秒调用didExitRegion。在后台,或者在没有测距时在前台,这些呼叫最多可以延迟 15 分钟。这些延迟始终存在于 iPhone 4S 机型上。在较新的型号上,延迟可能存在也可能不存在,具体取决于是否有足够的硬件辅助插槽来检测信标。在此处查看详细信息:http://developer.radiusnetworks.com/2014/03/12/ios7-1-background-detection-times.html

    我怀疑无法获取退出事件实际上是它们花费比预期更长的时间的问题。等待 15 分钟,看看回调是否到来。

    【讨论】:

      【解决方案3】:

      您还应该调用以下函数来开始测距

      [self.locationManager startRangingBeaconsInRegion:self.myBeaconRegion];
      

      【讨论】: