ios - CLBeaconRegion,如何关闭警告 : Turn On Bluetooth to Allow * to Connect to Accessories

标签 ios core-bluetooth clbeacon clbeaconregion

我们有一个项目正在使用 CoreLocation 区域来监控 iBeacon 区域在应用程序后台的进入/退出。 CLBeaconRegion (CLRegion)、CLBeacon 等 CLLocationManager 在进入 CLBeacon (iBeacon) 区域时返回回调。它是一个围绕下方蓝牙管理器的轻型包装器。

// 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 passively check if Bluetooth is enabled without prompting user to turn Bluetooth on

不幸的是,我找不到访问底层蓝牙或任何 CBCentralManager 引用来使用它的方法。

CLBeaconRegion 监控有什么方法可以关闭这个警告吗?

enter image description here

最佳答案

我制定了一个使用 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];
    }
}

关于ios - CLBeaconRegion,如何关闭警告 : Turn On Bluetooth to Allow * to Connect to Accessories,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28484304/

相关文章:

ios - 使用整数创建 if 语句

ios - 我是否必须等待 Swift 中的 didReadRSSI() 函数?

iphone - 出现键盘时调整 View 大小 (iOS)

Bluetooth Low energy最佳能耗策略

ios - 我们可以将外部摄像头的视频分享到 iOS 设备吗

ios - 在不知道 UUID 的情况下检测 iBeacon。一些应用程序正在这样做

ios - CLBeacon 子类错误

objective-c - OSX/iOS 上 Objective C 中的右移运算符是算术右移还是逻辑右移?

ios - 添加到 iPhone 的主屏幕时,Web 应用感觉响应速度较慢