iOS 核心蓝牙:获取 API 误用警告
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23338767/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
iOS Core Bluetooth : Getting API MISUSE Warning
提问by Yogesh Kulkarni
I am writing a test app in iOS 7 with the Core Bluetooth API. When I am testing the application I found that I am getting the following warning message:
我正在使用 Core Bluetooth API 在 iOS 7 中编写一个测试应用程序。当我测试应用程序时,我发现我收到以下警告消息:
TestBluetooth[626:60b] CoreBluetooth[API MISUSE] can only accept commands while in the powered on state
TestBluetooth[626:60b] CoreBluetooth[API MISUSE] 只能在开机状态下接受命令
Later I debugged app and found that, warning is coming from the following line of code:
后来我调试了应用程序,发现警告来自以下代码行:
[manager scanForPeripheralsWithServices:array options:scanOptions];
So can anyone please tell me why I am getting this message in the console?
那么谁能告诉我为什么我会在控制台中收到此消息?
There are bluetooth 4.0 android devices around me, but this app is not discovering them as peripheral device. So why it is not discovering bluetooth 4.0 LE Android devices as peripherals?
我周围有蓝牙 4.0 安卓设备,但这个应用程序没有发现它们作为外围设备。那么为什么它没有发现蓝牙 4.0 LE Android 设备作为外围设备呢?
回答by Etan
You have to wait until the [-CBCentralManagerDelegate centralManagerDidUpdateState:]
callback has been called. And then, verify that the state is PoweredOn
before you start scanning for peripherals.
您必须等到[-CBCentralManagerDelegate centralManagerDidUpdateState:]
回调被调用。然后,PoweredOn
在开始扫描外围设备之前验证该状态。
回答by oOEric
Please use the following code to solve the warning:
请使用以下代码解决警告:
(You can reference to the code in https://github.com/luoxubin/BlueTooth4.0)
(可参考https://github.com/luoxubin/BlueTooth4.0 中的代码)
if (bluetoothPowerOn) {
[self.centralManager scanForPeripheralsWithServices:[serviceIDs copy] options:@{CBCentralManagerScanOptionAllowDuplicatesKey:@(NO)}];
}
-(void)centralManagerDidUpdateState:(CBCentralManager *)central{
switch (central.state) {
case CBManagerStatePoweredOn:
{
bluetoothPowerOn = YES; //new code
[self start];
break;
}
default:
{
bluetoothPowerOn = NO; //new code
[self stopScan:[NSError hardwareStatusErrorWithMessage:@"Cannot open Bluetooth, please check the setting." hardwareStatus:central.state]];
break;
}
}
}
回答by Zaid Pathan
Do scan when bluetooth is poweredOn:
蓝牙开启时进行扫描:
func centralManagerDidUpdateState(_ central: CBCentralManager) {
switch central.state {
case .unknown:
print("unknown")
case .resetting:
print("resetting")
case .unsupported:
print("unsupported")
case .unauthorized:
print("unauthorized")
case .poweredOff:
print("poweredOff")
centralManager?.stopScan()
case .poweredOn:
print("poweredOn")
centralManager?.scanForPeripherals(withServices: nil, options: nil)
}
}