0

我的问题是:如何在手机锁定(未打开显示器)或在后台时从中央启用连续监控?

首先,我正在使用由 Christopher Mann @ https://github.com/csmann/iBeaconDemo提供的 iBeaconDemo ,我不认为编写此演示,但我使用它来了解 iBeacon 和区域监控。这是一个很棒的演示,但我希望它启用了后台监控,以便在您进入 Near 范围时显示本地通知。

我在我的 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}];
}

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

4

1 回答 1

1

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

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

  • 在您的应用中设置 UiBackgroundModes = 位置权限
  • region.notifyEntryStateOnDisplay = YES 确保在屏幕解锁时您将收到 didRangeBeacons(您似乎拥有)
  • locationManager.pausesLocationUpdatesAutomatically = 否;否则,当 iOs 决定用户不再移动时,测距偶尔会被关闭
  • 也使用 startUpdatingLocation,desiredAccuracy = kCLLocationAccuracyThreeKilometers; 你也会得到无用的 GPS 线,但它显然使 iO 不会停止信标测距。

另请参阅http://airfy.svbtle.com/battery-friendly-indoor-positioning-with-ibeacon

于 2014-07-10T09:48:27.580 回答