2017-09-26 44 views
2

当我设置关闭蓝牙,然后我用CBCentralManager获得蓝牙的状态是这样的:iOS11蓝牙有一些奇怪的事情

self.bluetoothManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil]; 

系统会显示这样的警告:system alert

蓝牙的当前状态是CBManagerStatePoweredOff。但是当我在控制中心关闭蓝牙时,即使蓝牙的当前状态仍然是CBManagerStatePoweredOff,此警报也不再显示。

如何在这种情况下提醒用户打开蓝牙?

回答

0

您可以通过执行以下代表方法提醒用户。

//Bluetooth state delegation 
#pragma mark - CBCentralManagerDelegate 

- (void)centralManagerDidUpdateState:(CBCentralManager *)central 
{ 
    NSString *stateString = nil; 
    switch(self.CBManager.state) 
    { 
     case CBManagerStateResetting: 
     stateString = @"The connection with the system service was momentarily lost, update imminent."; 
     break; 
     case CBManagerStateUnsupported: 
     stateString = @"The platform doesn't support Bluetooth Low Energy."; break; 
     case CBManagerStateUnauthorized: stateString = @"The app is not authorized to use Bluetooth Low Energy."; 
     break; 
     case CBManagerStatePoweredOff: 
     stateString = @"Bluetooth is currently powered off."; 
     break; 
     case CBManagerStatePoweredOn: 
     [self.beaconManager startMonitoringForRegion:self.museumsRegion]; 
     [self.beaconManager startRangingBeaconsInRegion: self.museumsRegion]; 
     break; 
     case CBManagerStateUnknown: 
     stateString = @"State unknown, update imminent."; 
     break; 
    } 
    NSLog(@"%@", stateString); 
} 

现在应该自动通知用户。

+0

当您在设置中关闭蓝牙时,将调用此代理方法,系统将自行显示提醒。当您在控制中心关闭蓝牙时,此代理方法也会被调用,但系统不会显示警报。如果用这种方法提醒用户,可能会同时显示两个警报。 –

+0

这也可能是你必须现在放置信息plist NSBluethoothInUse隐私密钥并设置一个字符串值。我以为我在iOS 11中阅读了一些关于此的内容。 –

+0

您没有明白。 –

0

您可以通过在选项字典中使用CBCentralManagerOptionShowPowerAlertKey来禁用系统蓝牙警报。

NSDictionary *options = @{CBCentralManagerOptionShowPowerAlertKey: @NO}; 
self.bluetoothManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil options:options]; 

然后你就可以使用deleget方法centralManagerDidUpdateState:弹出你自定义的警报。