【发布时间】:2016-01-30 14:05:58
【问题描述】:
我买了几个 iBeacons,然后按照教程尝试发现它们 (http://ibeaconmodules.us/blogs/news/14279747-tutorial-ibeacon-app-development-with-corelocation-on-apple-ios-7-8)。
它不起作用。
locationManager:(CLLocationManager *)manager didRangeBeacons 将始终返回一个空数组。
如果我稍微调整一下代码,根据这个问题的回答(locationManager:didEnterRegion not called when a beacon is detected),我会继续得到 CLRegionStateOutside
需要注意的一点是,我在下载第 3 方应用 Light Blue 时发现了 Beacon 的 UDID。并且 Light Blue 应用程序可以正确识别我所有的信标。
要注意的另一件事是,我不知道应该在标识符字段中输入什么,所以我只是键入了一个粘贴在物理信标之外的字符串。
这是代码:
- (void) locationManager:(CLLocationManager *)manager didStartMonitoringForRegion:(CLRegion *)region
{
NSLog(@"did start monitoring");
[self.locationManager requestStateForRegion:self.beaconRegion];
}
-(void)locationManager:(CLLocationManager *)manager didDetermineState:(CLRegionState)state forRegion:(CLRegion *)region
{
if (state == CLRegionStateInside)
{
//Start Ranging
NSLog(@"inside, start ranging");
[manager startRangingBeaconsInRegion:self.beaconRegion];
}
else
{
NSLog(@"outside, stop ranging");
//Stop Ranging here
}
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
NSUUID *beaconUUID = [[NSUUID alloc] initWithUUIDString:@"9B2D1BB8-25AA-8EE5-2513-7C140B6B1801"];
NSString *regionIdentifier = @"MiniBeacon_04193";
CLBeaconRegion *beaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:beaconUUID
major:0 minor:0 identifier:regionIdentifier];
self.beaconRegion = beaconRegion;
self.beaconRegion.notifyOnEntry=YES;
self.beaconRegion.notifyOnExit=YES;
self.beaconRegion.notifyEntryStateOnDisplay=YES;
switch ([CLLocationManager authorizationStatus]) {
case kCLAuthorizationStatusAuthorizedAlways:
NSLog(@"Authorized Always");
break;
case kCLAuthorizationStatusAuthorizedWhenInUse:
NSLog(@"Authorized when in use");
break;
case kCLAuthorizationStatusDenied:
NSLog(@"Denied");
break;
case kCLAuthorizationStatusNotDetermined:
NSLog(@"Not determined");
break;
case kCLAuthorizationStatusRestricted:
NSLog(@"Restricted");
break;
default:
break;
}
self.locationManager = [[CLLocationManager alloc] init];
if([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
[self.locationManager requestAlwaysAuthorization];
}
self.locationManager.delegate = self;
self.locationManager.pausesLocationUpdatesAutomatically = NO;
[self.locationManager startMonitoringForRegion:beaconRegion];
return YES;
}
-(void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region {
[manager startRangingBeaconsInRegion:(CLBeaconRegion*)region];
[self.locationManager startUpdatingLocation];
NSLog(@"You entered the region.");
[self sendLocalNotificationWithMessage:@"You entered the region."];
}
-(void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region {
[manager stopRangingBeaconsInRegion:(CLBeaconRegion*)region];
[self.locationManager stopUpdatingLocation];
NSLog(@"You exited the region.");
[self sendLocalNotificationWithMessage:@"You exited the region."];
}
-(void)sendLocalNotificationWithMessage:(NSString*)message {
UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.alertBody = message;
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
}
-(void)locationManager:(CLLocationManager *)manager didRangeBeacons:(NSArray *)beacons inRegion:(CLBeaconRegion *)region {
NSString *message = @"";
IMViewController *viewController = (IMViewController*)self.window.rootViewController;
viewController.beacons = beacons;
[viewController.tableView reloadData];
if(beacons.count > 0) {
CLBeacon *nearestBeacon = beacons.firstObject;
if(nearestBeacon.proximity == self.lastProximity ||
nearestBeacon.proximity == CLProximityUnknown) {
return;
}
self.lastProximity = nearestBeacon.proximity;
switch(nearestBeacon.proximity) {
case CLProximityFar:
message = @"You are far away from the beacon";
break;
case CLProximityNear:
message = @"You are near the beacon";
break;
case CLProximityImmediate:
message = @"You are in the immediate proximity of the beacon";
break;
case CLProximityUnknown:
return;
}
} else {
message = @"No beacons are nearby";
}
NSLog(@"%@", message);
[self sendLocalNotificationWithMessage:message];
}
【问题讨论】:
标签: objective-c ios9 ibeacon