屏幕关闭时的 iBeacon 事件

iBeacon events while screen is off

我正在尝试触发基于 iBeacons 的事件

当应用程序 运行 在前台、后台但未挂起(屏幕通过电源按钮关闭)时,它工作正常

我在锁定屏幕上可以看到 NSLog 消息,但在设备屏幕关闭时看不到。

有办法吗?

AppDelegate.m:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    NSLog(@"applicationDidFinishLaunching");

    _locationManager = [[CLLocationManager alloc] init];
    _locationManager.delegate = self;

    [_locationManager requestAlwaysAuthorization];

    CLBeaconRegion *region;

    region = [[CLBeaconRegion alloc] initWithProximityUUID:[[NSUUID alloc] initWithUUIDString:@"EBEFD083-70A2-47C8-9837-E7B5634DF524"] major: 9 minor: 103 identifier: @"region1"];
    region.notifyEntryStateOnDisplay = YES;
    region.notifyOnEntry = YES;
    region.notifyOnExit = YES;
    [_locationManager startMonitoringForRegion:region];
    [_locationManager startRangingBeaconsInRegion:region];

    return YES;
}

- (void)locationManager:(CLLocationManager *)manager didDetermineState:(CLRegionState)state forRegion:(CLRegion *)region
{
    if(state == CLRegionStateInside) {
        NSLog(@"locationManager didDetermineState INSIDE for %@", region.identifier);
    }
    else if(state == CLRegionStateOutside) {
        NSLog(@"locationManager didDetermineState OUTSIDE for %@", region.identifier);
    }
    else {
        NSLog(@"locationManager didDetermineState OTHER for %@", region.identifier);
    }
}

- (void)locationManager:(CLLocationManager *)manager didRangeBeacons:(NSArray *)beacons inRegion:(CLBeaconRegion *)region
{
    if ( beacons.count > 0 )
    {
        NSLog(@"locationManager didRangeBeacons: %@",beacons.description);
    }
}

Info.plist(仅相关部分):

        <key>NSLocationAlwaysUsageDescription</key>
        <string>app location requested</string>
        <key>UIBackgroundModes</key>
        <array>
            <string>location</string>
            <string>voip</string>
            <string>bluetooth-peripheral</string>
            <string>bluetooth-central</string>
            <string>external-accessory</string>
        </array>

LocationManager 的 pausesLocationUpdatesAutomatically 属性 设置为 "NO",并将此 属性 设置为没有位置服务永远不会关闭。但您必须小心,因为将此 属性 设置为 NO 会显着增加设备的功耗。

虽然信标监控(didEnterRegion:didExitRegion:)在后台工作,但信标测距(didRangeBeacons:inRegion:)仅在应用程序位于前台时才工作,并在后台限时播放。这些后台限制包括应用程序因事件(例如由于您的设置而出现锁定屏幕 region.notifyEntryStateOnDisplay = YES;)被唤醒进入后台后五秒

您可以采取一些技巧来获得额外的背景测距时间。阅读此处:

http://developer.radiusnetworks.com/2014/11/13/extending-background-ranging-on-ios.html