我使用以下代码从特定信标获取消息,但它在 uuid 中始终显示 null。当我运行此代码时,“viewdidLoad 方法工作正常,但当涉及到 didRangebeacons 时,它仅显示最后一个 uuid 信标。实际上,我想检查信标是否可用于特定 uuid 并接收该信号并继续检查数组中的 uuid 和当它接收到另一个信号时,它也应该显示该信标信号。
- (void)viewDidLoad
{
[super viewDidLoad];
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
NSLog(@"Before assigning UUID");
NSArray *uuidArr = [NSArray arrayWithObjects: @"9146947B-441D-4116-B60E-4DD4659825AB", @"9146947B-441D-4116-B60E-4DD46598257C", @"9146947B-441D-4116-B60E-4DD46598257B", nil];
for(int i=0; i<[uuidArr count]; i++)
{
NSString *uuidTemp = [uuidArr objectAtIndex:i];
NSUUID *uuid = [[NSUUID alloc] initWithUUIDString:uuidTemp];
self.myBeaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:uuid
identifier:@"com.gimbal.iosexample"];
[self.locationManager startMonitoringForRegion:self.myBeaconRegion];
[self.locationManager startRangingBeaconsInRegion:self.myBeaconRegion];
if (![CLLocationManager isMonitoringAvailableForClass:[CLBeaconRegion class]])
{
NSString *msg = [NSString stringWithFormat:@"Monitoring not available for %@", uuidTemp];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Monitoring not available" message:msg delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles: nil];
[alert show]; return;
}
}
}
-(void)locationManager:(CLLocationManager*)manager didRangeBeacons:(NSArray*)beacons inRegion:(CLBeaconRegion*)region
{
NSLog(@"iBeacons found");
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Successfully found" message:nil delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles: nil];
[alert show];
self.statusLabel.text = @"iBeacon found!";
CLBeacon *foundBeacon = [beacons firstObject];
NSString *uuid = foundBeacon.proximityUUID.UUIDString;
NSString *major = [NSString stringWithFormat:@"%@", foundBeacon.major];
NSString *minor = [NSString stringWithFormat:@"%@", foundBeacon.minor];
NSLog(@"UUID: %@", uuid);
}
@end
谁能帮我做到这一点。
提前致谢。
最佳答案
您正在 for 循环中创建不同的信标区域,但您的区域标识符相同,因此第二个区域定义将替换第一个,然后第二个区域将被第三个替换。您需要使用唯一的区域标识符 - 类似于
CLBeaconRegion *beaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:uuid
identifier:[NSString stringWithFormat:@"com.gimbal.iosexample.%d",i]];
关于ios - didRangeBeacons 没有在 ios 中获取 uuid,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24155263/