![](/img/trans.png)
[英]How can I customise proximity zones for ibeacons using CoreLocation
[英]How do I use CoreLocation to get multiple iBeacons?
我正在嘗試使用CoreLocation來獲取我的iOS應用程序指定的多個iBeacon,其想法是,如果它們在范圍內,它將在找到它們時通知我每個iBeacon。
問題在於,在didEnterRegion和didExitRegion方法中,我必須提供一個CLBeaconRegion對象。 每個iBeacon都有一個,如果我只使用一個iBeacon,我可以只使用一個,但是既然有多個,我需要知道如何從這些方法中找到每個CLBeaconRegion。
也許我誤會了它的工作方式; 如果是這樣,請告訴我。
- (void)getForUUUIDs:(CDVInvokedUrlCommand *)command
{
//Get an array of UUID's to filter by
self->locationUUIDs = [self getArgsObject:command.arguments];
self->locationManager = [[CLLocationManager alloc] init];
self->locationManager.delegate = self;
scanCallback = command.callbackId;
for(NSInteger i = 0; i < [locationUUIDs count]; i++) {
NSString *identifier = [NSString stringWithFormat:@"BLERegion %d",i];
CLBeaconRegion *thisRegion = [[CLBeaconRegion alloc] initWithProximityUUID:[[locationUUIDs allKeys] objectAtIndex:i] identifier:identifier];
[self->locationManager startMonitoringForRegion:thisRegion];
}
}
- (void)locationManager:(CLLocationManager*)manager didEnterRegion:(CLRegion*)region
{
[self->locationManager startRangingBeaconsInRegion:????];
}
-(void)locationManager:(CLLocationManager*)manager didExitRegion:(CLRegion*)region
{
[self->locationManager stopRangingBeaconsInRegion:????];
}
在觸發監視器進入/退出事件的同一區域進行測距非常簡單:
- (void)locationManager:(CLLocationManager*)manager didEnterRegion:(CLRegion*)region
{
[self->locationManager startRangingBeaconsInRegion:(CLBeaconRegion *)region];
}
這將在與用於開始監視的區域完全相同的范圍內開始。 請注意,有一個區域參數傳遞給回調。 該Region參數將包含您之前設置的UUID。
另一點:雖然進入區域時開始測距並在離開區域時停止測距沒有錯,但實際上並不需要這樣做。 只需在開始監視的同時開始測距。 因為在看不見iBeacon時測距不會做任何事情,所以最終結果幾乎是相同的。 唯一的區別是,如果您提前進行設置,則可能會更快地獲得第一個遠程回調。 電池或系統資源沒有多余的消耗。
提前設置它的另一個好處是,您不必強制轉換CLRegion對象-您可以從原始對象開始。 而且您不必實現監視回調方法,因此您的代碼更簡單。 像這樣:
- (void)getForUUUIDs:(CDVInvokedUrlCommand *)command
{
//Get an array of UUID's to filter by
self->locationUUIDs = [self getArgsObject:command.arguments];
self->locationManager = [[CLLocationManager alloc] init];
self->locationManager.delegate = self;
scanCallback = command.callbackId;
for(NSInteger i = 0; i < [locationUUIDs count]; i++) {
NSString *identifier = [NSString stringWithFormat:@"BLERegion %d",i];
CLBeaconRegion *thisRegion = [[CLBeaconRegion alloc] initWithProximityUUID:[[locationUUIDs allKeys] objectAtIndex:i] identifier:identifier];
[self->locationManager startMonitoringForRegion:thisRegion];
[self->locationManager startRangingBeaconsInRegion:thisRegion];
}
}
您的區域由uuid指定
self.beaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:uuid
identifier:identifier];
您所有的信標都必須共享該uuid。 因此,在對信標進行范圍調整時,可以通過該方法(CLLocationManagerDelegate)來獲取它們。
-(void)locationManager:(CLLocationManager*)manager didRangeBeacons:(NSArray*)beacons inRegion:(CLBeaconRegion*)region
{
for (CLBeacon *beacon in beacons) {
NSLog(@"Major : %@", beacon.major);
NSLog(@"Minor : %@", beacon.minor);
}
}
這里的major和minor屬性可以區分您的信標。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.