2014-02-10 12 views
3

我測試iBeacon顯示的區域,我使用didDetermineState:方法和CLRegionStateCLRegionStateUnknown當我在該區域內,任何人可以告訴我爲什麼?CLRegionState是未知的。當我「米

我的藍牙是在和探測信標時,我不等他們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; 
    } 
} 
+1

嗨@jhondoe:你已經克服了這個問題,我得到同樣的問題時,我正在jaalee信標。 。 – sabir

回答

1

我已重新啓動我的兩個設備並重新安裝了應用程序,仍然遇到同樣的問題,並且它沒有觸發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 [email protected]"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]; 

    } 
    } 
} 
+0

我在該代碼中沒有看到任何可以解釋您的症狀的代碼。你使用的是什麼樣的iBeacon?你可以嘗試一個不同的,看看你是否得到相同的結果?如果您有第二個使用BLE的iOS設備,則可以在此處使用免費的定位for iBeacon應用程序:https://itunes.apple.com/us/app/locate-for-ibeacon/id738709014?mt=8或者如果您的mac有BLE,可以在這裏使用$ 9.99 MacBeacon:http://www.radiusnetworks.com/macbeacon-app.html – davidgyoung

+0

奇怪!我已經將設備(iPad 3)作爲接收器和(iPhone 4s)作爲發送者進行了交換,並且正在按照需要工作,不是什麼故障和哪個設備有問題? – Jhondoe

+0

的確很奇怪。我會採取任何無法接收的設備,並嘗試使用不同的發射器。對於它的價值,我已經成功地在這兩種角色中使用了iPhone 4s和iPad 3設備,所以我懷疑是否存在與這些設備類型相關的任何問題。 – davidgyoung

1

你說你是「區域內」,但你怎麼知道的iBeacon是否真的被檢測到?藍牙是否開啓並正常工作?用戶在提示時是否允許位置訪問?

我會啓用iBeacon測距和記錄檢測到的任何事情,以驗證您的設備真的看到了iBeacon。我懷疑你會發現你沒有發現任何東西,或者iBeacon.proximity字段由於校準不好而返回CLProximityUnknown

didDetermineState即使在啓動監控時沒有檢測到信標(或者當您在區域上設置了notifyEntryStateOnDisplay=YES時喚醒屏幕),也會獲得額外的呼叫。這是我認爲你可能得到CLRegionStateUnknown價值的唯一情況。我從未見過這個值返回,所以我很好奇它發生了什麼情況。找到我上面的問題的答案可以解決這個問題。請回報!

+0

那段代碼對我來說很合適。這可能聽起來很愚蠢,但我會重新啓動手機,然後再試一次,以確保它不處於不良狀態。如果仍然存在問題,請在代碼中設置CLBeaconRegion並調用'[locationManager startMonitoringForRegion:region]' – davidgyoung

0

我以前有同樣的問題,得到的回答是,我只是忘記了打電話:

[self.locationManager startMonitoringForRegion:beaconRegion]; 

起初我錯誤地認爲,如果你開始使用範圍:

[self.locationManager startRangingBeaconsInRegion:beaconRegion]; 

...並獲取數據的rssi,準確性和UUID在locationManager:didRangeBeacons:inRegion:方法,CLLocationManager應該能夠找出手機在該地區,但顯然不是。因此,要求狀態時,有:

[self.locationManager requestStateForRegion:beaconRegion]; 

我在locationManager:didDetermineState:forRegion:得到CLRegionStateUnknown該區域像你這樣。

1

這個職位是根據在iOS 8,這可能在未來改變寫着:

我在這裏張貼一個答案的情況下,別人是有同樣的問題,因爲我。在意識到這是因爲我的手機處於飛行模式之前,我總共花了近4個小時。

TL;博士

如果飛行模式,區域通知不來通過

+0

經過1.5天的努力奮鬥後,即使我在這裏看到你的答案,但我仍然沒有注意到問題出自飛機模式。 iBeacon的工作範圍很完善,但不是監測。可惜沒有任何錯誤或任何錯誤 – bpolat

+0

@bpolat - 我做了同樣的事情!在我發現問題之前,我檢查了我的代碼很多次。我很高興別人覺得這很有幫助! – Logan

相關問題