2013-12-12 19 views
2

我正在研究一個類來處理我所有的iBeacon測試。它的目的是開始尋找地區,範圍的信標,確定他們,然後發送通知。代碼如下。iBeacon測距和接近度是越野車

我遇到的問題是應用程序運行速度非常慢,我知道iBeacons有延遲問題,有時只是停止工作(不會識別關閉的燈塔)。我的代碼很混亂,我知道,在我清理它之前嘗試對邏輯進行排序。我想知道我是否在這裏錯過了一個邏輯缺陷(我的意思是,我不知道我介紹過哪些邏輯缺陷!)。

#import "dcBeaconManager.h" 

@implementation dcBeaconManager 

@synthesize currentBeaconState; 

bool testRanging = false; 
int firstRegionEntered = 0; 
int beaconsRangedCount = 0; 

- (void)initBeaconManager { 
    NSLog(@"initBeaconManager called"); 
    self.locationManager = [[CLLocationManager alloc] init]; 
    self.locationManager.delegate = self; 
    NSUUID *uuid = [[NSUUID alloc]initWithUUIDString:@"B9407F30-F5F8-466E-AFF9-25556B57FE6D"]; 
    self.beaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:uuid identifier:@"digiConsRegion"]; 
    [self.locationManager startMonitoringForRegion:self.beaconRegion]; 
    [self.locationManager requestStateForRegion:self.beaconRegion]; 

    currentBeaconState = @"initial"; 
} 

- (void)locationManager:(CLLocationManager *)manager didStartMonitoringForRegion:(CLRegion *)region { 
    NSLog(@"Started looking for regions"); 
    [self.locationManager requestStateForRegion:self.beaconRegion]; 
} 

- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region { 
    NSLog(@"Region discovered"); 
    if (firstRegionEntered == 0) { 
     NSLog(@"First time in region"); 
     UILocalNotification *notification = [[UILocalNotification alloc] init]; 
     notification.alertBody = @"Welcome to Digial Conversations, we are upstairs."; 
     notification.soundName = UILocalNotificationDefaultSoundName; 
     [[UIApplication sharedApplication] presentLocalNotificationNow:notification]; 
     firstRegionEntered = 1; 
    } 
    [self.locationManager startRangingBeaconsInRegion:self.beaconRegion]; 
} 

- (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region { 
    [self.locationManager stopRangingBeaconsInRegion:self.beaconRegion]; 

    UILocalNotification *notification = [[UILocalNotification alloc] init]; 
    notification.alertBody = @"We hope you enjoyed the event, thank you for coming."; 
    notification.soundName = UILocalNotificationDefaultSoundName; 
    [[UIApplication sharedApplication] presentLocalNotificationNow:notification]; 
} 

- (void)locationManager:(CLLocationManager *)manager didRangeBeacons:(NSArray *)beacons inRegion:(CLBeaconRegion *)region { 
    CLBeacon *beacon = [[CLBeacon alloc] init]; 
    beacon = [beacons lastObject]; 
    NSNumber *currentBeaconMajor = beacon.major; //it's major (group) number 
    NSNumber *currentBeaconMinor = beacon.minor; //it's minor (individual) number 

    if (([currentBeaconMinor floatValue] == 59204) && ([currentBeaconMajor floatValue] == 33995) && (beacon.proximity == CLProximityNear)) { 
     if (beaconsRangedCount == 0) { 
      currentBeaconState = @"Mint"; 
      beaconsRangedCount ++; 
     } 
     if ([currentBeaconState isEqualToString:@"Blue"] || [currentBeaconState isEqualToString:@"Purple"]) { 
      [[NSNotificationCenter defaultCenter] postNotificationName:@"didLocateMint" object:nil]; 
     } 

    } else if (([currentBeaconMinor floatValue] == 7451) && ([currentBeaconMajor floatValue] == 63627) && (beacon.proximity == CLProximityNear)) { 
     if (beaconsRangedCount == 0) { 
      currentBeaconState = @"Blue"; 
      beaconsRangedCount ++; 
     } 
     if ([currentBeaconState isEqualToString:@"Mint"] || [currentBeaconState isEqualToString:@"Purple"]) { 
      [[NSNotificationCenter defaultCenter] postNotificationName:@"didLocateBlue" object:nil]; 
     } 

    } else if (([currentBeaconMinor floatValue] == 51657) && ([currentBeaconMajor floatValue] == 26976) && (beacon.proximity == CLProximityNear)) { 
     if (beaconsRangedCount == 0) { 
      currentBeaconState = @"Purple"; 
      beaconsRangedCount ++; 
     } 
     if ([currentBeaconState isEqualToString:@"Mint"] || [currentBeaconState isEqualToString:@"Blue"]) { 
      [[NSNotificationCenter defaultCenter] postNotificationName:@"didLocatePurple" object:nil]; 
     } 
    } else { 
     [[NSNotificationCenter defaultCenter] postNotificationName:@"didLeaveNearRegion" object:nil]; 
    } 
} 

@end 
+0

信標只發送每隔幾秒鐘,不是?你是否說明了這一點。它不像他們處於連續發送模式... –

+0

我在這幾天工作Estimote iBeacon,我注意到一些延遲和一些問題,以確定出口和在發送通知信標範圍的入口..有時不工作或最近工作。我已將廣告間隔更改爲50毫秒,但沒有任何更改。 – Firegab

回答

9

您的意思是didEnterRegion和didExitRegion回調被延遲?

如果您的應用程序正在前臺運行,您應該在一秒鐘內輸入區域通知,並在幾秒鐘內退出區域通知。如果您的應用在後臺,最多可能需要15分鐘才能獲得區域內或區域外的通知。

有關此計時的詳細信息,請參閱here

這些延遲問題不是信標特定的。他們必須處理CoreLocation API在iOS中的實現方式。

+0

優秀寫作David,感謝您分享該鏈接! –