我已经完成了两个教程并且正在阅读基本的C.通过做最好的学习并且在过去一周内写了一些轻量级的应用程序。我正在加快编写一些将使用ibeacon的应用程序。当我浏览一些示例并阅读参考指南时,我看到可以通过为每个UUID运行startMonitoringForRegion来扫描多个区域。好的,所以我想我可以为每个UUID运行它,但那不起作用。我确定我正在做一些基本上完全错误的事情......下面的代码是一个彻底的黑客攻击 - 一旦我得到语义,我将从带有API调用的数据库中提取UUID,然后循环遍历它们以激活监视。下面的代码导致最后一个循环只显示四个UUID中的两个。
标题中的:
@property (strong, nonatomic) CLBeaconRegion *myBeaconRegion;
@property (strong, nonatomic) CLBeaconRegion *myBeaconRegion2;
@property (strong, nonatomic) CLBeaconRegion *myBeaconRegion3;
@property (strong, nonatomic) CLBeaconRegion *myBeaconRegion4;
在main中:
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);
}
答案 0 :(得分:11)
我认为问题是您的区域标识符。每个信标区域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
。
答案 1 :(得分:0)
startMonitoringForRegion的头文件状态"这是异步完成的,可能不会立即反映在monitoredRegions"。
您可以通过为for循环添加时间延迟来验证这一点:
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);
}
});
答案 2 :(得分:0)
如果您需要使用多个信标进行监控,那么您可以使用信标Major和Minor值进行区分。阅读教程here 以更好地了解ibeacons。
-(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];
}

上面我配置了三个信标区域,主要和次要的价值差异。我放置了三个按钮并调用IBAction,使用标签和调用函数发送不同的Major和Minor值。我在三个不同的iphone中安装了相同的应用程序,并在每部手机中启用了每个不同的按钮,并在另一部手机中安装了接收器以进像魅力一样工作!但是从一个地区移动到另一个地区时需要时间来检测区域。