【发布时间】:2014-05-28 23:06:34
【问题描述】:
我有三个全新的 Kontakt.io 信标,并使用this tutorial 和它的source 尝试用我的 iPhone 4s 识别它们。我所做的只是更改 UUID(所有 Kontakt.io 信标从一开始就具有相同的 UUID)并添加一些代码:
此行应该强制应用检查它是否已经在该区域中
[self.locationManager requestStateForRegion:self.myBeaconRegion];
这些行应该检查系统是否以正确的方式设置:
NSArray *locationServicesAuthStatuses = @[@"Not determined",@"Restricted",@"Denied",@"Authorized"];
NSArray *backgroundRefreshAuthStatuses = @[@"Restricted",@"Denied",@"Available"];
BOOL monitoringAvailable = [CLLocationManager isMonitoringAvailableForClass:[self.myBeaconRegion class]];
NSLog(@"Monitoring available: %@", [NSNumber numberWithBool:monitoringAvailable]);
int lsAuth = (int)[CLLocationManager authorizationStatus];
NSLog(@"Location services authorization status: %@", [locationServicesAuthStatuses objectAtIndex:lsAuth]);
int brAuth = (int)[[UIApplication sharedApplication] backgroundRefreshStatus];
NSLog(@"Background refresh authorization status: %@", [backgroundRefreshAuthStatuses objectAtIndex:brAuth]);
当我启动应用程序时,我的手机旁边有 iBeacons,didEnterRegion 不会被触发。此外,当我取出电池并将它们放回原处时(模拟超出范围并重新进入),没有任何反应。当我使用真正的 iBeacons 时,其他 Github 示例代码对我也不起作用,只有当我使用另一部 iPhone 传输信号时。
这是 ViewController.h:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// Initialize location manager and set ourselves as the delegate
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
// Create a NSUUID with the same UUID as the broadcasting beacon
NSUUID *uuid = [[NSUUID alloc] initWithUUIDString:@"A77A1B68-49A7-4DBF-914C-760D07FBB87B"];
// Setup a new region with that UUID and same identifier as the broadcasting beacon
self.myBeaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:uuid
identifier:@"com.appcoda.testregion"];
// Tell location manager to start monitoring for the beacon region
[self.locationManager startMonitoringForRegion:self.myBeaconRegion];
// Force app to check if it's already IN the region
[self.locationManager requestStateForRegion:self.myBeaconRegion];
// Check if beacon monitoring is available for this device
if (![CLLocationManager isMonitoringAvailableForClass:[CLBeaconRegion class]]) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Monitoring not available" message:nil delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles: nil]; [alert show]; return;
}
NSArray *locationServicesAuthStatuses = @[@"Not determined",@"Restricted",@"Denied",@"Authorized"];
NSArray *backgroundRefreshAuthStatuses = @[@"Restricted",@"Denied",@"Available"];
BOOL monitoringAvailable = [CLLocationManager isMonitoringAvailableForClass:[self.myBeaconRegion class]];
NSLog(@"Monitoring available: %@", [NSNumber numberWithBool:monitoringAvailable]);
int lsAuth = (int)[CLLocationManager authorizationStatus];
NSLog(@"Location services authorization status: %@", [locationServicesAuthStatuses objectAtIndex:lsAuth]);
int brAuth = (int)[[UIApplication sharedApplication] backgroundRefreshStatus];
NSLog(@"Background refresh authorization status: %@", [backgroundRefreshAuthStatuses objectAtIndex:brAuth]);
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)locationManager:(CLLocationManager*)manager didEnterRegion:(CLRegion *)region
{
// We entered a region, now start looking for our target beacons!
self.statusLabel.text = @"Finding beacons.";
[self.locationManager startRangingBeaconsInRegion:self.myBeaconRegion];
}
-(void)locationManager:(CLLocationManager*)manager didExitRegion:(CLRegion *)region
{
// Exited the region
self.statusLabel.text = @"None found.";
[self.locationManager stopRangingBeaconsInRegion:self.myBeaconRegion];
}
-(void)locationManager:(CLLocationManager*)manager
didRangeBeacons:(NSArray*)beacons
inRegion:(CLBeaconRegion*)region
{
// Beacon found!
self.statusLabel.text = @"Beacon found!";
CLBeacon *foundBeacon = [beacons firstObject];
// You can retrieve the beacon data from its properties
//NSString *uuid = foundBeacon.proximityUUID.UUIDString;
//NSString *major = [NSString stringWithFormat:@"%@", foundBeacon.major];
//NSString *minor = [NSString stringWithFormat:@"%@", foundBeacon.minor];
}
这是 NSLog 输出:
2014-05-29 00:49:19.951 BeaconReceiver[2803:60b] Monitoring available: 1
2014-05-29 00:49:19.967 BeaconReceiver[2803:60b] Location services authorization status: Authorized
2014-05-29 00:49:20.109 BeaconReceiver[2803:60b] Background refresh authorization status: Available
非常感谢您的帮助!!
【问题讨论】:
标签: objective-c ios7 ibeacon