2014-01-15 24 views
4

我已經完成了兩篇教程,並且正在閱讀基本的C語言。在過去一週左右的時間裏,通過學習最好的學習並編寫了幾個輕型應用程序。我正在儘快編寫一些將使用ibeacon的應用程序。當我瀏覽一些樣本並閱讀參考指南時,我發現可以通過爲每個UUID運行startMonitoringForRegion來掃描多個區域。好的,所以我想我可以爲每個UUID運行它,但這不起作用。我確定我正在做一些基本完全錯誤的事情......下面的代碼是一個徹頭徹尾的破解 - 一旦我獲得了語義,我將通過API調用從數據庫中提取UUID並通過它們循環來激活監控。下面的代碼在上一個循環中僅顯示四個UUID中的兩個。如何在Objective-C中使用startMonitoringForRegion掃描多個區域

在標題:

@property (strong, nonatomic) CLBeaconRegion *myBeaconRegion; 
@property (strong, nonatomic) CLBeaconRegion *myBeaconRegion2; 
@property (strong, nonatomic) CLBeaconRegion *myBeaconRegion3; 
@property (strong, nonatomic) CLBeaconRegion *myBeaconRegion4; 

主:

NSUUID *uuid = [[NSUUID alloc] initWithUUIDString:@"86E4BDEA-C6FF-442C-95CB-E6E557A23CF2"]; 
self.myBeaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:uuid identifier:@"com.appcoda.testregion"]; 

NSUUID *uuid2 = [[NSUUID alloc] initWithUUIDString:@"C9AFF296-A722-4F2D-8669-47B7CCC79A14"]; 
self.myBeaconRegion2 = [[CLBeaconRegion alloc] initWithProximityUUID:uuid2 identifier:@"com.appcoda.testregion"]; 

NSUUID *uuid3 = [[NSUUID alloc] initWithUUIDString:@"1DBDDC7C-49BB-48BF-A2F6-A4825BD514EA"]; 
self.myBeaconRegion3 = [[CLBeaconRegion alloc] initWithProximityUUID:uuid3 identifier:@"com.appcoda.testregion"]; 

NSUUID *uuid4 = [[NSUUID alloc] initWithUUIDString:@"8D942B9E-0197-4C81-8722-92144599E9F7"]; 
self.myBeaconRegion4 = [[CLBeaconRegion alloc] initWithProximityUUID:uuid4 identifier:@"com.appcoda.testregion"]; 

[self.locationManager startMonitoringForRegion:self.myBeaconRegion]; 
[self.locationManager startMonitoringForRegion:self.myBeaconRegion2]; 
[self.locationManager startMonitoringForRegion:self.myBeaconRegion3]; 
[self.locationManager startMonitoringForRegion:self.myBeaconRegion4]; 

NSSet *setOfRegions = [self.locationManager monitoredRegions]; 
    for (CLRegion *region in setOfRegions) { 
     NSLog (@"region info: %@", region); 
    } 

回答

10

我認爲這個問題是您所在地區的標識符。每個信標區域identifier必須是唯一,否則CLLocationManager將它們視爲相同區域。

嘗試設置一個唯一的標識符爲每個區域:

NSUUID *uuid = [[NSUUID alloc] initWithUUIDString:@"86E4BDEA-C6FF-442C-95CB-E6E557A23CF2"]; 
self.myBeaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:uuid identifier:@"com.appcoda.testregion"]; 

NSUUID *uuid2 = [[NSUUID alloc] initWithUUIDString:@"C9AFF296-A722-4F2D-8669-47B7CCC79A14"]; 
self.myBeaconRegion2 = [[CLBeaconRegion alloc] initWithProximityUUID:uuid2 identifier:@"com.appcoda.testregion2"]; 

NSUUID *uuid3 = [[NSUUID alloc] initWithUUIDString:@"1DBDDC7C-49BB-48BF-A2F6-A4825BD514EA"]; 
self.myBeaconRegion3 = [[CLBeaconRegion alloc] initWithProximityUUID:uuid3 identifier:@"com.appcoda.testregion3"]; 

NSUUID *uuid4 = [[NSUUID alloc] initWithUUIDString:@"8D942B9E-0197-4C81-8722-92144599E9F7"]; 
self.myBeaconRegion4 = [[CLBeaconRegion alloc] initWithProximityUUID:uuid4 identifier:@"com.appcoda.testregion4"]; 

你應該看到從NSLog聲明中列出的每個區域。無需使用dispatch_async

+0

這是正確的。 – davidgyoung

+0

太棒了,作品!非常感謝! – user3196820

+0

我不能在OS 7.1.1上使用我的iphone5s。但它曾用於一個燈塔區域。任何人都可以通過這種方式工作? – Forrest

0

爲startMonitoringForRegion頭文件指出, 「這是異步完成的,可能不會立即反映在monitoredRegions」。

您可以通過添加時間延遲到您的循環驗證這一點:

double delayInSeconds = 5.0; 
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC)); 
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){ 
    NSSet *setOfRegions = [self.locationManager monitoredRegions]; 
    for (CLRegion *region in setOfRegions) { 
     NSLog (@"region info: %@", region); 
    } 
}); 
+0

沒有運氣。所以我添加了這段代碼,它確實延遲了。我也增加了延遲,並有幾乎相同的結果。幾個奇怪的事情,但。一次UUID不是我在代碼中的一個......我開始從下到上註釋掉UUID,看看它只是最後一個顯示......這就是它的樣子。我可以不正確地添加到startMonitoringForRegion嗎? – user3196820

+0

很確定您已經正確添加了startMonitoringForRegion,但時間延遲確實存在。您可以添加一個UIButton,通過監視區域循環並在點擊時打印出UUID - 您會注意到每次都會打印不同的UUID。最終你應該看到你所有添加的UUID。 – kenshin03

0

如果您需要使用多個信標進行監控,那麼您可以使用信標主要和次要值進行區分。閱讀教程here以更好地瞭解ibeacon。

-(void)setBeaconTranmitter:(NSInteger)major minorValue:(NSInteger)minor { 
 
    // We need to set beacon regions here. 
 
    NSUUID * uid = [[NSUUID alloc] initWithUUIDString:uuid]; //uuid value is static common string for all beacons. 
 
    self.beaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:uid major:major minor:minor identifier:beaconsId];//beaconsId is a common identifier for all beacons. 
 
    
 
// Call your Transmitter function here 
 
    [self configureTransmitter]; 
 
}
我與主要和次要的差值配置3個標區

以上方式。我放置了三個按鈕,並調用IBAction使用標籤和調用函數發送不同的主值和次值。我在三個不同的iPhone中安裝了相同的應用程序,並在每部手機中啓用了每個不同的按鈕,並在另一部手機中安裝了接收器進行演示。像魅力一樣工作!但從一個地區移動到另一個地區時需要時間來檢測地區。

+1

這是一個邊緣鏈接專用答案。你應該擴大你的答案,在這裏包含儘可能多的信息,並使用鏈接僅供參考。 – gunr2171

+0

當然,我將在未來的迴應中包含這一點。謝謝 – Vijay