【问题标题】:CLRegionState is UnKnown When i"m in the region当我在该地区时,CLRegionState 是未知的
【发布时间】:2014-03-07 10:07:04
【问题描述】:

我正在测试 iBeacon,我使用了 didDetermineState: 方法,当我在该区域内时,CLRegionStateCLRegionStateUnknown,谁能告诉我为什么?

我的蓝牙已打开并在我为 CLBeaconStateUnknown 定位信标时检测到信标,发送方和接收方在范围内(1 米),这是我的实现

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

    switch (state) {
    case CLRegionStateInside:
        currentBeconRegion = (CLBeaconRegion *)region;
        [self.locationManager startRangingBeaconsInRegion:currentBeconRegion];
        NSLog(@"INSIDE the Region");//not logging
        break;
    case CLRegionStateOutside:
        NSLog(@"OUTSIDE the Region");
        break;
    case CLRegionStateUnknown:
    default:
        NSLog(@"Region state UNKNOWN!!!"); //Logging on console
        statusLabel.text = @"Region state UNKNOWN!!!";
    //[self.locationManager stopRangingBeaconsInRegion:beaconRegion];           
    [self.locationManager startRangingBeaconsInRegion:beaconRegion];//Suceessfully ranging the beacon

        break;
    }
}

【问题讨论】:

  • 嗨@jhondoe:你有没有克服这个问题,我在研究jaalee beacons时遇到了同样的问题..

标签: ios core-location bluetooth-lowenergy ibeacon


【解决方案1】:

我之前也遇到过同样的问题,答案是我只是忘记打电话了:

[self.locationManager startMonitoringForRegion:beaconRegion];

一开始我错误地认为如果你开始测距:

[self.locationManager startRangingBeaconsInRegion:beaconRegion];

...并在locationManager:didRangeBeacons:inRegion: 方法中获取rssi、准确性和UUID 的数据,CLLocationManager 应该能够确定手机在那个区域,但显然不是。因此,当询问状态时,使用:

[self.locationManager requestStateForRegion:beaconRegion];

像你一样,我在locationManager:didDetermineState:forRegion: 中获得了该地区的CLRegionStateUnknown

【讨论】:

    【解决方案2】:

    这篇文章是基于 iOS 8 编写的,将来可能会改变:

    我在这里发布答案,以防其他人遇到与我相同的问题。我总共花了将近 4 个小时才意识到这是因为我的手机处于飞行模式。

    tl;博士

    如果开启飞行模式,则不会收到区域通知

    【讨论】:

    • 在解决问题 1.5 天后,即使我在这里阅读了您的答案,但我仍然没有注意到问题来自飞行模式。 iBeacon 测距完美运行,但无法监控。遗憾的是没有错误或任何错误
    • @bpolat -- 我也做了同样的事情!在发现问题之前,我检查了我的代码很多次。我很高兴其他人发现这很有帮助!
    【解决方案3】:

    我已经重新启动了我的两个设备并重新安装了应用程序,但仍然遇到同样的问题,而且它没有触发 didEnterRegion:didExitRegion:

    - (void)viewDidLoad
     {
    
     [super viewDidLoad];    
    //set peripheralManager delegate
    self.peripheralManager = [[CBPeripheralManager alloc] initWithDelegate:self
                                                                     queue:nil
                                                                   options:nil];
    
    
    //Set location managaer delegate
    self.locationManager = [[CLLocationManager alloc] init];
    self.locationManager.delegate = self;
    
    
    uuid = [[NSUUID alloc] initWithUUIDString:@"E01D235B-A5DB-4717-8D2F-28D5B48931F1"];
    
    // Setup a new region with that UUID and same identifier as the broadcasting beacon
    self.beaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:uuid identifier:@"testRegion"];
    self.beaconRegion.notifyEntryStateOnDisplay = YES;
    self.beaconRegion.notifyOnEntry = YES;
    self.beaconRegion.notifyOnExit = YES;
    
    }
    
    
    #pragma mark -
    #pragma mark - Peripheral Manager Delegates
    
    -(void)peripheralManagerDidUpdateState:(CBPeripheralManager *)peripheral
    {
    switch (peripheral.state) {
        case CBPeripheralManagerStatePoweredOn:
            [self.locationManager startMonitoringForRegion:self.beaconRegion];
            break;
        case CBPeripheralManagerStatePoweredOff:
             statusLabel.text = @"Bluetooth is Off";
            [self clearFileds];
            [self.locationManager stopMonitoringForRegion:self.beaconRegion];
            break;
        case CBPeripheralManagerStateUnknown:
        default:
            statusLabel.text = @"Bluetooth is Unsupported";
            [self clearFileds];
            [self.locationManager stopMonitoringForRegion:self.beaconRegion];
            break;
       }
    
     }
    
    #pragma mark -
    #pragma mark - Location Manager Delecate Methods
    
    -(void)locationManager:(CLLocationManager *)manager didStartMonitoringForRegion:(CLRegion *)region
    {
    
    [self.locationManager requestStateForRegion:self.beaconRegion];
    
    }
    
     -(void)locationManager:(CLLocationManager *)manager didDetermineState:(CLRegionState)state forRegion:(CLRegion *)region
    {
    
    switch (state) {
        case CLRegionStateInside:
            currentBeconRegion = (CLBeaconRegion *)region;
            [self.locationManager startRangingBeaconsInRegion:currentBeconRegion];
            NSLog(@"INSIDE the Region");
            break;
        case CLRegionStateOutside:
            NSLog(@"OUTSIDE the Region");
            break;
        case CLRegionStateUnknown:
        default:
            NSLog(@"Region state UNKNOWN!!!");
            statusLabel.text = @"Region state UNKNOWN!!!";
           [self.locationManager stopRangingBeaconsInRegion:self.beaconRegion];
            break;
        }
    }
    -(void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region
    {
    //check if we're ranging the correct beacon
    if (![self.beaconRegion isEqual:region]) { return;}
    
    NSLog(@"didEnterRegion");
    if([region isKindOfClass:[CLBeaconRegion class]])
    {
        currentBeconRegion = (CLBeaconRegion *)region;
        if ([beaconRegion.identifier isEqualToString:@"testRegion"])
        {
    
            //send local notificaation
            UILocalNotification *notice = [[UILocalNotification alloc] init];
            notice.alertBody = @"Becoan Found!!!";
            notice.alertAction =@"View";
            [[UIApplication sharedApplication] presentLocalNotificationNow:notice];
    
            //srart raning beacon region
            [self.locationManager startRangingBeaconsInRegion:currentBeconRegion];
    
    
            }
        } 
    
     }
     -(void)locationManager:(CLLocationManager *)manager didRangeBeacons:(NSArray *)beacons inRegion:(CLBeaconRegion *)region
    {
    
    if ([beacons count] > 0) {
    
        int major = [beacons.firstObject major].intValue;
        int minor = [beacons.firstObject minor].intValue;
    
        if (major != self.foundBeacon.major.intValue || minor !=     self.foundBeacon.minor.intValue) {
            NSLog(@"Found Beacon is: %@", [beacons firstObject]);
        }
    
        self.foundBeacon = [[CLBeacon alloc] init];
        self.foundBeacon = [beacons firstObject];
        //Set the ui
        [self setFileds];
    
     }
    else //[beacon count]  0
    {
        statusLabel.text = @"No becaons found";
        [self clearsFileds];
       // [self.locationManager stopMonitoringForRegion:beaconRegion];
        }
    
    }
    -(void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region
    {
    NSLog(@"didExitRegion");
    if([region isKindOfClass:[CLBeaconRegion class]])
    {
        currentBeconRegion = (CLBeaconRegion *)region;
        if ([beaconRegion.identifier isEqualToString:@"testRegion"])
        {
            [self.locationManager stopMonitoringForRegion:currentBeconRegion];
    
         }
      }
    }
    

    【讨论】:

    • 我在该代码中看不到任何可以解释您的症状的内容。您使用的是哪种 iBeacon?你可以尝试不同的,看看你是否得到相同的结果?如果您有第二台带有 BLE 的 iOS 设备,您可以在此处使用免费的 Locate for iBeacon 应用程序:itunes.apple.com/us/app/locate-for-ibeacon/id738709014?mt=8 或者如果您的 Mac 有 BLE,您可以在此处使用 9.99 美元的 MacBeacon:radiusnetworks.com/macbeacon-app.html
    • 奇怪!我已经将设备(iPad 3)作为接收器,将(iPhone 4s)作为发送器,并且按预期工作,不知道是什么故障,哪个设备有故障?
    • 确实很奇怪。我会使用任何接收困难的设备并尝试使用不同的发射器。值得一提的是,我在这两个角色中都成功使用了 iPhone 4s 和 iPad 3 设备,所以我怀疑这些设备类型是否存在任何问题。
    • 为什么在问题仍然存在时标记为已解决?
    • 我使用了您的代码,它对我有很大帮助。但是在 didRangeBeacons 中,我的信标计数为 0,如果我重新启动 ibeacon 设备,则它显示信标计数 >0。我不明白怎么了?
    【解决方案4】:

    您说您在“区域内”,但您怎么知道 iBeacon 真的被检测到了?蓝牙是否已打开并正常工作?用户是否在出现提示时允许访问位置信息?

    我会启用 iBeacon 测距并记录检测到的任何内容,以验证您的设备是否确实看到了 iBeacon。我怀疑您会发现自己没有检测到任何东西,或者由于校准错误,iBeacon.proximity 字段可能会返回 CLProximityUnknown

    didDetermineState 会收到额外的呼叫,即使在您启动监控时(或者如果您在您所在地区设置了notifyEntryStateOnDisplay=YES 时唤醒屏幕)没有检测到信标。这是我认为您可能会获得CLRegionStateUnknown 值的唯一情况。我从来没有见过这个值返回,所以我很好奇它是在什么条件下发生的。找到我上面问题的答案可能会解决这个问题。请回来报告!

    【讨论】:

    • 该代码对我来说看起来不错。这听起来可能很愚蠢,但我会重新启动您的手机并重试以确保它没有处于不良状态。如果您仍然有问题,请将您的代码发布到您设置 CLBeaconRegion 的位置并致电[locationManager startMonitoringForRegion:region]
    猜你喜欢
    • 1970-01-01
    • 2018-05-29
    • 2021-12-22
    • 2012-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-28
    • 1970-01-01
    相关资源
    最近更新 更多