locationManager:在检测到信标时未调用didEnterRegion

在使用信标(iOS设备)进行测试时,我发现侦听器信标会发出一些意外行为。 locationManager:即使信标进入某个区域,也不会调用didEnterRegion方法。 但是locationManager:didRangeBeacons:inRegion:被正确调用,并在那里显示检测到的信标。 有没有人经历过这样的事情。

检查您的方法是否以下列方式实现。 在viewDidLoad ,最后开始moniotoring

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

监控启动后,请求您定义的区域的状态

 - (void) locationManager:(CLLocationManager *)manager didStartMonitoringForRegion:(CLRegion *)region { [self.locationManager requestStateForRegion:self.beaconRegion]; } 

确定状态后,开始测距信标

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

并根据您的需求实施以下方法……

 - (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region { self.statusLbl.text=@"Entered region"; } -(void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region { self.statusLbl.text=@"Exited region"; } -(void)locationManager:(CLLocationManager *)manager didRangeBeacons:(NSArray *)beacons inRegion:(CLBeaconRegion *)region { if(beacons.count>0) {} } 

希望这能解决你的问题。

 before starting coding in project , you must follow given setup guidlines --> 1. in project info or info.plist --> Custom IOS Target Properties --> . add "Required background modes" . in this add two items --> ."App shares data using CoreBluetooth" ."App registers for location updates" 2. in project Capability --> There is Background Modes . check "Loaction update" . check "Acts as a Bluetooth LE accessory" . check "uses bluetooth LE accessories" 

(并按照Davidgyoung先生的指示行事。相信我,一定会有用。)

如果没有关于测试开始条件的更多细节,我很难说我是否已经看到完全相同的东西。 但是,是的,在某些特定情况下,我已经看到了locationManager:didRangeBeacons:即使没有调用locationManager,也会调用inRegion:didEnterRegion。

如果您使用相同的区域同时开始测距和监控,并且iOS认为您已经在受监控的区域中,那么您可能无法调用locationManager:didEnterRegion。

要真正测试是否有问题,您需要设置一个测试用例,其中:

  1. 确保您不在该地区。
  2. 让iOS运行几分钟
  3. 开始监控该区域
  4. 让iOS继续运行几分钟
  5. 输入地区。
  6. 看看你是否接到了对locationManager的调用:didEnterRegion

如果你在完成上述操作后仍然没有接到电话,那么肯定是错的。

您还需要知道您正在监视某个区域 – 而不是特定的信标。

因此,如果您有3个共享相同proximityUUID信标,并且您的区域仅定义为proximityUUID (没有主要值和次要值),则只会在以下两种情况下收到通知:

  1. 没有来自该地区的信标在范围内,并且第一个信标/信标被发现( didEnterRegion:

  2. 来自该地区的一个或多个信标在范围内,它们都在视线之外约30秒( didExitRegion: