我正在使用Core Bluetooth API在iOS 7中编写测试应用程序。当我测试应用程序时,我发现我收到以下警告消息:
TestBluetooth [626:60b] CoreBluetooth [API MISUSE]只能在开机状态下接受命令
后来我调试了应用程序并发现警告来自以下代码行:
[manager scanForPeripheralsWithServices:array options:scanOptions];
所以有人可以告诉我为什么我在控制台收到此消息?
我周围有蓝牙4.0 Android设备,但这个应用程序并没有发现它们是外围设备。那么为什么它不发现蓝牙4.0 LE Android设备作为外围设备?
答案 0 :(得分:64)
您必须等到[-CBCentralManagerDelegate centralManagerDidUpdateState:]
回调被调用。然后,在开始扫描外围设备之前验证状态是否为PoweredOn
。
答案 1 :(得分:2)
请使用以下代码解决警告:
(您可以参考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;
}
}
}
答案 2 :(得分:0)
在蓝牙开机时进行扫描:
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)
}
}