CBCentralManager 的状态何时会开启,但仍会显示“未开启"?警告? [英] When would CBCentralManager's state ever be powered on but still give me a "not powered on" warning?

查看:13
本文介绍了CBCentralManager 的状态何时会开启,但仍会显示“未开启"?警告?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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.

推荐答案

当你的应用程序启动时,你必须首先等待 centralManager 从 centralManagerDidUpdateState: 获得回调.然后每隔一次,我建议在执行任何 centralManager 调用之前检查状态.您很可能在中央有机会更新之前调用扫描或检索.确保只在知道方法已开启后才调用方法.如果将每个调用都包含在首先检查状态的 if 语句中,则不会出现错误.

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
}

这篇关于CBCentralManager 的状态何时会开启,但仍会显示“未开启"?警告?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆