2014-01-20 17 views
6

我從Roximity和我收集到的幾個信標中,所有的Roximity信標都有相同的UUID。我知道我可以使用[locationManager: didRangeBeacons: inRegion:]得到主要和次要的價值觀,但如果我設置[locationManager didEnterRegion]發送推送通知,和我的用戶走過不同的Roximity盞明燈,是與別人的應用程序相關聯的,我怎麼能確定這[locationManager didEnterRegion]可以[LocationManager didEnterRegion]獲取燈塔的主要和次要?

回答

12

你基本上有兩種選擇。

  1. 定義您正在監控的區域,以便它們包含您的特定主要和次要編號。主要的限制是僅適用於iOS,您可以同時監控20個大區,這意味着你只能爲20個不同的iBeacons做到這一點:

    CLBeaconRegion *region1 = [[CLBeaconRegion alloc] initWithProximityUUID:[[NSUUID alloc] initWithUUIDString:@"8deefbb9-f738-4297-8040-96668bb44281"] major:1201 minor:3211 identifier:@"beacon1"];  
    [_locationManager startRangingBeaconsInRegion:region1];  
    CLBeaconRegion *region1 = [[CLBeaconRegion alloc] initWithProximityUUID:[[NSUUID alloc] initWithUUIDString:@"8deefbb9-f738-4297-8040-96668bb44281"] major:1798 minor:2122 identifier:@"beacon2"];  
    [_locationManager startRangingBeaconsInRegion:region2]; 
    ... 
    
  2. 監控僅基於UUID的區域,也別在這同一地區測距同時。您將看到您看到的每個特定iBeacon的Ranging回調。 (即使在後臺,進入該區域後也會持續約5秒鐘。)在Ranging回調中,您檢查您看到的信標的主要/次要號碼,並將它們與您擁有的信標進行比較。只有當你看到一場比賽時,你纔會執行特定的動作。如果您持續添加信標,在您的應用中更新此列表可能會很困難,因此您可能需要使用諸如ProximityKit之類的網絡服務,以便您將您的iBeacon標識符列表存儲在雲中。

    -(void)locationManager:(CLLocationManager *)manager didRangeBeacons:(NSArray *)beacons inRegion:(CLBeaconRegion *)region { 
        for (CLBeacon *beacon in beacons) { 
        if ([self isMyBeaconWithMajor: beacon.major minor: beacon.minor]) { 
         // Yes, this is my beacon! Do something special here 
        } 
        } 
    } 
    
    -(BOOL)isMyBeaconWithMajor: (NSNumber *)major minor: (NSNumber *)minor { 
        // TODO: write code here that returns YES if the major and minor belong to you 
    } 
    

另一種可能性,最終(當然外面你是問了一下)是使用標有定製的UUID,這讓事情變得更容易。充分披露:我是一家公司,銷售具有可定製標識符的iBeacons。

+0

謝謝你的偉大的答案。我昨天試圖做選項#2,但沒有工作。再看看它。 – Chris

+0

爲每個信標使用唯一的UUID不會限制您一次只能監控多達20個? – random

+0

是的,但這並不完全是我提出的。如果您定義了自己獨特的ProximityUUID,併爲所有自己的iBeacons使用同一個ProximityUUID,那麼當您看到ProximityUUID iBeacons屬於您時,您將非常確定。 – davidgyoung

-1

可確定的主要和次要使用此代碼:

- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region{ 
    CLBeaconRegion *r = (CLBeaconRegion *) region; 
    NSLog(@"UUID %@, major %@, minor %@", r.proximityUUID, r.major, r.minor); 
    [self myNotification:@"You are in the region"]; 
    [self.locationManager startRangingBeaconsInRegion:r]; 
} 
+4

這個委託方法返回被監視的區域。基本上,它說:「嘿,你告訴我監視的那個區域剛剛進入」它並沒有告訴你任何關於該地區廣告的廣告。 因此,返回的主要和次要價值將是您自己的,而不是觸發通知的燈塔區域。 – joshblour