14

当我在 iPhone 5 上运行使用 CoreBluetooth 的应用程序时,我不断收到此错误:<CBConcreteCentralManager: 0x2007d590> is not powered on

但是当我调用state程序的唯一一个 CBCentralManager 对象时,它返回 5,即 CBCentralManagerStatePoweredOn。所以它已通电,但我收到此错误。iPhone 的蓝牙功能也已启用。

总的来说,什么时候会发生这种情况?我什至不知道程序运行时发生了什么,因为我收到了看似冲突的消息。

4

1 回答 1

22

您必须先等到 centralManager 从centralManagerDidUpdateState:您的应用程序启动时收到回调。然后每隔一段时间,我建议在进行任何 centralManager 调用之前检查状态。您很可能在中央有机会更新之前调用扫描或检索。确保仅在知道方法已打开后才调用方法。如果将每个调用包装在首先检查状态的 if 语句中,则不会出现错误。

- (void)centralManagerDidUpdateState:(CBCentralManager *)central
{
   if(central.state==CBCentralManagerStatePoweredOn)
   {
      //Now do your scanning and retrievals
   }
}

否则,只需在每次通话之前将您的中心包裹在状态检查中:

if(yourCentral.state==CBCentralManagerStatePoweredOn)
{
//you're good to go on calling centralManager methods
}
于 2013-06-15T00:03:27.563 回答