无法在iOS9中检测到iBeacons。继续获得CLRegionStateOutside

时间:2015-10-30 07:50:26

标签: objective-c ios9 ibeacon

我买了几个iBeacons,然后我按照教程尝试发现它们(http://ibeaconmodules.us/blogs/news/14279747-tutorial-ibeacon-app-development-with-corelocation-on-apple-ios-7-8)。

它不起作用。

locationManager:(CLLocationManager *)管理器didRangeBeacons将始终返回一个空数组。

如果我略微调整代码,根据这个问题的回答(locationManager:didEnterRegion not called when a beacon is detected),我会继续得到CLRegionStateOutside

需要注意的一件事是我找到了我的灯塔的UDID我下载第三方应用程序Light Blue。 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];
    }

1 个答案:

答案 0 :(得分:3)

问题是代码没有寻找信标的正确ProximityUUID。源自Light Blue应用程序的代码中的9B2D1BB8-25AA-8EE5-2513-7C140B6B1801 UUID不是检测iBeacons所需的ProximityUUID。

了解有很多种UUID。 Light Blue应用程序将检测蓝牙服务UUID,但不会检测到iBeacon ProximityUUID。虽然表面上看起来一样,但它们具有不同的价值和含义。您使用Light Blue扫描的蓝牙服务UUID可能源自信标,但它仍然无法在显示的代码中工作,因为它不是正确的ProximityUUID。

你如何找出你的灯塔的ProximityUUID?一些选择:

  1. 询问制造商,或使用制造商的配置实用程序。

  2. 使用dedicated beacon scanner for AndroidOSX或Linux,无论ProximityUUID如何,都可以看到任何信标。不幸的是,如果您事先了解ProximityUUID,iOS只会让您看到信标。

  3. 知道ProximityUUID后,只需替换值而不是显示的{{1}}。