蓝牙中央区域监控背景和/或锁定(无显示)

时间:2013-11-11 21:41:23

标签: ios7 core-location core-bluetooth ibeacon cbcentralmanager

我的问题是:如何在手机锁定(没有显示屏开启)或背景设置时从中央启用连续监听?

首先,我正在使用iBeaconDemo由Christopher Mann @ https://github.com/csmann/iBeaconDemo提供,我不赞成编写这个演示,但我用它来理解iBeacons和Region Monitoring。这是一个很棒的演示,但我希望它能够启用后台监控,以便在您进入近距离时显示本地通知。

我在我的Capabilities(info.plist)文件中启用了蓝牙中心,蓝牙 - 外围设备和位置,并将.notifyOnDisplay,notifyOnEntry和notifyOnExit设置为YES,并且发现我在应用程序位于前台时获得了结果或在锁定屏幕上(在手机锁定时按下主屏幕按钮)但在后台(手机未锁定)或手机锁定(无显示)时无法进行监听。

这是我的appDelegate的didEnterBackground方法(注意:这是一个不同的本地通知,每次都能正常工作但与我进入该区域时想要显示的通知不一样:

    - (void)applicationDidEnterBackground:(UIApplication *)application {
    NSLog(@"Application entered background state.");
    [[UIApplication sharedApplication] cancelAllLocalNotifications];

    // create local notification and present it immediately
    UILocalNotification *notification = [self createLocalNotification:@"Bring app back to foreground"];
    [[UIApplication sharedApplication] presentLocalNotificationNow:notification];
}

以下是我想在后台发布的通知,一旦我进入该区域,我只需要弹出1个通知(为了测试目的,每个放1个):

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

    // identify closest beacon in range
    if ([beacons count] > 0) {
        CLBeacon *closestBeacon = beacons[0];
        if (closestBeacon.proximity == CLProximityImmediate) {
            /**
             Provide proximity based information to user.  You may choose to do this repeatedly
             or only once depending on the use case.  Optionally use major, minor values here to provide beacon-specific content
             */

            [self fireUpdateNotificationForStatus:[NSString stringWithFormat:@"Range: %li \n Beacon Name: %@ \n You are in the immediate vicinity of the Beacon.", (long)closestBeacon.rssi, ((CLBeaconRegion*)region).identifier]];

            [[UIApplication sharedApplication] presentLocalNotificationNow:[self createLocalNotification:@"Immediate LN"]];


        } else if (closestBeacon.proximity == CLProximityNear) {
            // detect other nearby beacons
            // optionally hide previously displayed proximity based information
            [self fireUpdateNotificationForStatus:[NSString stringWithFormat: @"Range: %li \n Beacon Name: %@ \n There are Beacons nearby.", (long)closestBeacon.rssi, ((CLBeaconRegion*)region).identifier]];

            [[UIApplication sharedApplication] presentLocalNotificationNow:[self createLocalNotification:@"Near LN"]];

        } else if (closestBeacon.proximity == CLProximityFar) {

            [self fireUpdateNotificationForStatus:[NSString stringWithFormat:@"Range: %li \n Beacon Name: %@ \n You are far away from the %@th beacon in store %@!", (long)closestBeacon.rssi, ((CLBeaconRegion*)region).identifier, closestBeacon.minor, closestBeacon.major]];

            [[UIApplication sharedApplication] presentLocalNotificationNow:[self createLocalNotification:@"Far LN"]];

        }

    } else {
        // no beacons in range - signal may have been lost
        // optionally hide previously displayed proximity based information
        [self fireUpdateNotificationForStatus:@"There are currently no Beacons within range."];
    }
}

最后,这是fireUpdateNotificationsForStatus方法,这可能有用:

    - (void)fireUpdateNotificationForStatus:(NSString*)status {
    // fire notification to update displayed status
    [[NSNotificationCenter defaultCenter] postNotificationName:kLocationUpdateNotification
                                                        object:Nil
                                                      userInfo:@{@"status" : status}];
}

如果还有其他任何有用的帮助,请随时告诉我。谢谢你的时间!

1 个答案:

答案 0 :(得分:1)

假设用户已收到通知(您使用日志记录而不是通知对此进行了测试,对吗?)并且您的问题是后台处理:

iOs和后台扫描有点古怪,你可以做一些事情:

  • 在您的应用中设置了UiBackgroundModes =位置权限
  • region.notifyEntryStateOnDisplay = YES确保您在屏幕解锁时收到didRangeBeacons(您好像有)
  • locationManager.pausesLocationUpdatesAutomatically = NO;否则当iOs决定用户不再移动时偶尔会关闭测距
  • 也使用startUpdatingLocation,expectAccuracy = kCLLocationAccuracyThreeKilometers;你也会得到无用的GPS线,但它显然让iOs不能停止灯塔测距。

另见http://airfy.svbtle.com/battery-friendly-indoor-positioning-with-ibeacon