我们有一个项目正在使用CoreLocation区域来监控在应用程序后台进入/退出的iBeacon区域。 CLBeaconRegion(CLRegion),CLBeacon等。当输入CLBeacon(iBeacon)区域时,CLLocationManager返回回调。它是一个围绕下面蓝牙管理器的光包装。
// 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,它允许禁用此警告。
不幸的是我发现没有办法访问底层蓝牙或任何CBCentralManager引用来使用它。
有没有办法为CLBeaconRegion监控禁用此警告?
答案 0 :(得分: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];
}
}