4

我们有一个项目正在使用 CoreLocation 区域来监控 iBeacon 区域在应用程序后台进入/退出。CLBeaconRegion (CLRegion)、CLBeacon 等。CLLocationManager 在进入 CLBeacon (iBeacon) 区域时返回回调。它是一个轻量级的封装在下面的 bluetoothManager 周围。

// various CLLocation delegate callback examples
- (void) locationManager:(CLLocationManager *)manager didRangeBeacons:(NSArray *)beacons inRegion:(CLBeaconRegion *)region;
- (void) locationManager:(CLLocationManager *)manager didDetermineState:(CLRegionState)state forRegion:(CLRegion *)region;

我们遇到的问题是,当用户没有打开蓝牙时,Iphone 会定期发出系统级警告“打开蓝牙以允许“APP_NAME”连接其他配件”。这种情况经常发生,今天早上我已经得到了 4 次,因为应用程序在后台运行。CLLocationManager 可能正在尝试监视那些 CLBeaconRegion,但蓝牙已关闭,因此无法执行此操作。

另一篇文章提到 CBCentralManager 有一个属性 CBCentralManagerOptionShowPowerAlertKey,它允许禁用此警告。

iOS CoreBluetooth 被动检查蓝牙是否启用而不提示用户打开蓝牙

不幸的是,我发现无法访问底层蓝牙或任何 CBCentralManager 引用来使用它。

有什么方法可以禁用此警告以进行 CLBeaconRegion 监控?

在此处输入图像描述

4

1 回答 1

3

我制定了一个使用 CoreBluetooth 并CBCentralManager检测、停止和启动蓝牙使用的解决方案。以下是大部分代码,以及在开始之前进行的初始检查以查看它是否已打开。它通过保证蓝牙关闭时不使用信标来抑制错误提示。如果它被禁用,则停止信标。警告就这样消失了。不幸的是,我们无法以编程方式实际启用/禁用蓝牙。

// initialize in viewdidLoad, etc
_bluetoothManager =  [[CBCentralManager alloc] initWithDelegate:self queue:nil options:@{CBCentralManagerOptionShowPowerAlertKey:@NO}];


// bluetooth manager state change
- (void)centralManagerDidUpdateState:(CBCentralManager *)central
{
    NSString *stateString = nil;
    switch(central.state)
    {
        case CBCentralManagerStateResetting: stateString = @"The connection with the system service was momentarily lost, update imminent."; break;
        case CBCentralManagerStateUnsupported: stateString = @"The platform doesn't support Bluetooth Low Energy."; break;
        case CBCentralManagerStateUnauthorized: stateString = @"The app is not authorized to use Bluetooth Low Energy."; break;
        case CBCentralManagerStatePoweredOff: stateString = @"Bluetooth is currently powered off."; break;
        case CBCentralManagerStatePoweredOn: stateString = @"Bluetooth is currently powered on and available to use."; break;
        default: stateString = @"State unknown, update imminent."; break;
    }

    if(_bluetoothState != CBCentralManagerStateUnknown && _bluetoothState != CBCentralManagerStatePoweredOn && central.state == CBCentralManagerStatePoweredOn){
         NSLog(@"BEACON_MANAGER: Bluetooth just enabled. Attempting to start beacon monitoring.");
        _forceRestartLM = YES; // make sure we force restart LMs on next update, since they're stopped currently and regions may not be updated to trigger it
        [self startBeaconMonitoring];
    }

    if(_bluetoothState != CBCentralManagerStateUnknown && _bluetoothState == CBCentralManagerStatePoweredOn && central.state != CBCentralManagerStatePoweredOn)    {
        NSLog(@"BEACON_MANAGER: Bluetooth just disabled. Attempting to stop beacon monitoring.");
        [self stopBeaconMonitoring];
    }
}
于 2015-02-16T19:40:12.477 回答