When would CBCentralManager's state ever be powered on but still give me a “not powered on” warning?

ぐ巨炮叔叔 提交于 2019-12-09 02:27:46

问题


I keep getting this error when I run my app that uses CoreBluetooth on an iPhone 5: <CBConcreteCentralManager: 0x2007d590> is not powered on

But when I call state on my program's one and only CBCentralManager object, it returns 5, which is CBCentralManagerStatePoweredOn. So it's powered on, yet I get this error. The iPhone's Bluetooth is also enabled.

Just in general, when would this ever happen? I don't even know what is going on when the program runs because I'm getting what looks like conflicting messages.


回答1:


You have to initially wait until the centralManager gets the callback from centralManagerDidUpdateState: when you're app boots up. Then every other time, I recommend checking the state prior to doing any centralManager calls. You're most likely calling scan or retrieve before the central has had a chance to update. Ensure you only call methods after you know it's on. You will not get the error if you wrap each call in if statements that check the state first.

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

Otherwise just wrap your central inside a state check before each call:

if(yourCentral.state==CBCentralManagerStatePoweredOn)
{
//you're good to go on calling centralManager methods
}


来源:https://stackoverflow.com/questions/17118534/when-would-cbcentralmanagers-state-ever-be-powered-on-but-still-give-me-a-not

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!