IOS核心蓝牙:获取API滥用警告

我正在用Core Bluetooth API编写iOS 7中的testing应用程序。 当我testing应用程序时,我发现我收到以下警告消息:

2014-04-28 15:52:09.400 TestBluetooth[626:60b] CoreBluetooth[API MISUSE] can only accept commands while in the powered on state

后来我debugging了应用程序,发现警告来自以下代码行:

[manager scanForPeripheralsWithServices:array options:scanOptions];

那么任何人都可以告诉我为什么我在控制台中得到这个消息?

有我身边的蓝牙4.0安卓设备,但这个应用程序不发现他们作为外围设备。 那么为什么它不发现蓝牙4.0 LE Android设备作为外设?

在开始扫描外围设备之前,您必须等待[-CBCentralManagerDelegate centralManagerDidUpdateState:]callback被调用,然后确认状态为PoweredOn。

请使用下面的代码来解决警告:

(你可以参考https://github.com/luoxubin/BlueTooth4.0中的代码)

 if (bluetoothPowerOn) { //new code [self.centralManager scanForPeripheralsWithServices:[serviceIDs copy] options:@{CBCentralManagerScanOptionAllowDuplicatesKey:@(NO)}]; } //new code -(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; } } }