CBCentralManager状态为Off,即使蓝牙处于开启状态

时间:2018-02-14 18:08:45

标签: ios objective-c bluetooth

我正在尝试使用以下代码检查蓝牙是否打开/关闭。但即使蓝牙已经打开,它也会返回CBManagerStatePoweredOff。我在iPhone6s和iOS版本11.2.5(15D60)中检查过它。如果我在设置上手动重新启动蓝牙,它将返回CBManagerStatePoweredOn。

- (void)detectBluetooth
{
  if(!bluetoothManager)
  {
    // Put on main queue so we can call UIAlertView from delegate callbacks.
    //    NSDictionary *options = @{CBCentralManagerOptionShowPowerAlertKey: @NO};
    //    bluetoothManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil options:options];
    bluetoothManager = [[CBCentralManager alloc] initWithDelegate:self queue:dispatch_get_main_queue()];
    [bluetoothManager scanForPeripheralsWithServices:nil options:nil];
  }

  [self centralManagerDidUpdateState:bluetoothManager]; // Show initial state
}

- (void)centralManagerDidUpdateState:(CBCentralManager *)central
{
  NSString *stateString = nil;
  switch(central.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 goToSearchDevices];
      break;
    default: stateString = @"State unknown, update imminent."; break;
  }
}

0 个答案:

没有答案