CLBeaconRegion,如何关闭警告:打开蓝牙以允许*连接到附件 [英] CLBeaconRegion, how to turn off warning: Turn On Bluetooth to Allow * to Connect to Accessories

查看:346
本文介绍了CLBeaconRegion,如何关闭警告:打开蓝牙以允许*连接到附件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

We have a project that is using CoreLocation regions to monitor iBeacon region entering/exiting in the app background. CLBeaconRegion (CLRegion), CLBeacon, etc. CLLocationManager returns callbacks when a CLBeacon (iBeacon) region is entered. It is a light wrapper around a bluetoothManager underneath.

// 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,但蓝牙已关闭,因此无法执行此操作。

The problem we're having is that when a user does not have bluetooth turned on, the Iphone issues a system level warning on a regular basis to 'Turn On Bluetooth to Allow "APP_NAME" to Connect ot Accessories'. This happens a lot, I've got it 4 times this morning already as the app runs in the background. CLLocationManager may be attempting to monitor those CLBeaconRegion, but bluetooth is off so it can't do this.

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

Another post, mentions that CBCentralManager has a property, CBCentralManagerOptionShowPowerAlertKey, that allows disabling this warning.

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

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

Unfortunately I've found no way to access underlying bluetooth, or any CBCentralManager reference to use this.

有没有办法为CLBeaconRegion监控禁用此警告?

Is there any way to disable this warning for CLBeaconRegion monitoring?

推荐答案

我找到了使用CoreBluetooth和 CBCentralManager 来检测,停止和启动蓝牙使用的解决方案。下面是大部分代码,加上初始检查以确定它是否在启动之前打开。它通过保证在蓝牙关闭时不使用信标来抑制错误提示。如果禁用,则停止信标。因此警告就消失了。我们无法以编程方式实际启用/禁用蓝牙。

I worked out a solution that uses CoreBluetooth and CBCentralManager to detect, stop, and start bluetooth usage. Below is the majority of the code, plus an initial check to see if it is on before starting. It works to suppress the error prompt by guaranteeing beacons are not being used when bluetooth is off. If it is disabled, beacons are stopped. The warning thus goes away. We're unable to actually enable/disable bluetooth programmatically unfortunately.

// 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];
    }
}

这篇关于CLBeaconRegion,如何关闭警告:打开蓝牙以允许*连接到附件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆