iBeacon:didRangeBeacons没有被调用

时间:2015-04-05 14:34:15

标签: ios core-bluetooth

我正在尝试将iBeacon支持添加到我的应用中。我没有任何物理iBeacons,因此我在另一部iPhone上使用Radius Networks的Locate应用程序。有了这个应用程序,我能够看到其他手机信标。然而,在我自己的应用程序中,didRangeBeacons永远不会被调用。我确保将CoreLocation和CoreBluetooth框架添加到我的应用程序中。有什么想法吗?

以下是我的代码:

- (instancetype)init
{
    self = [super init];
    if (self)
    {
        self.locationManager = [[CLLocationManager alloc] init];
        self.locationManager.delegate = self;

        self.discoveryUUID = [[NSUUID alloc] initWithUUIDString:@"E2C56DB5-DFFB-48D2-B060-D0F5A71096E0"];
        self.region = [[CLBeaconRegion alloc] initWithProximityUUID:self.discoveryUUID identifier:[self.discoveryUUID UUIDString]];
        [self.locationManager startRangingBeaconsInRegion:self.region];

        self.timer = [[NSTimer alloc] initWithFireDate:[NSDate dateWithTimeIntervalSinceNow:LocatorTimeInSec] interval:0 target:self selector:@selector(timerElapsed:) userInfo:nil repeats:NO];

        [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSDefaultRunLoopMode];
    }

    return self;
}


-(void)locationManager:(CLLocationManager *)manager didRangeBeacons:(NSArray *)beacons inRegion:(CLBeaconRegion *)region
{ ... }

3 个答案:

答案 0 :(得分:2)

好的,我在 app -Info.plist文件中缺少以下内容。我想对于iOS 8你需要它......

<key>NSLocationWhenInUseUsageDescription</key>
<string>This will allow you to...</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>This will allow you to...</string>

我需要添加以下代码:

if([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)])
    {
        [self.locationManager requestAlwaysAuthorization];
    }

答案 1 :(得分:2)

确保在您正在调试的设备上打开蓝牙。

答案 2 :(得分:0)

您需要添加:

self.region.notifyOnEntry = YES;

或者,您也可以在启用显示时添加通知:

self.region.notifyEntryStateOnDisplay = YES;