didRangeBeacons没有在ios中获得uuid

时间:2014-06-11 05:31:03

标签: ios objective-c xcode

我使用以下代码从特定信标获取消息,但它始终在uuid中显示null。当我运行此代码时," viewdidLoad方法工作正常但是当涉及到didRangebeacons时,它只显示最后的uuid信标。实际上我想检查是否可用于特定uuid的信标并接收该信号并继续检查阵列中的uuids,当它接收到另一个信号时,它也应该显示该信标信号。

    - (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

任何人都可以帮助我这样做。

提前致谢。

1 个答案:

答案 0 :(得分:1)

您正在for循环中创建不同的信标区域,但您的区域标识符是相同的,因此第二个区域定义将替换第一个,然后第二个将被第三个替换。您需要使用唯一的区域标识符 - 类似于

CLBeaconRegion *beaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:uuid
                                                             identifier:[NSString stringWithFormat:@"com.gimbal.iosexample.%d",i]];