我有一个使用BTLE连接到设备(arduino)的iOS应用程序。我的iPad iOS 7上的一切正常。升级到iOS 8后,CBCentralManager找不到任何外围设备。
- (void)startScanningForSupportedUUIDs
{
[self.centralManager scanForPeripheralsWithServices:nil options:nil];
}
我不知道会出现什么问题。
答案 0 :(得分:16)
我有解决方案,出于某种原因,在iOS 8中实例化CBManager后会有一些延迟。您需要在CBCentralManager打开时开始扫描,使用此方法:
-(void)centralManagerDidUpdateState:(CBCentralManager *)central{
switch (central.state) {
case CBCentralManagerStatePoweredOff:
NSLog(@"CoreBluetooth BLE hardware is powered off");
break;
case CBCentralManagerStatePoweredOn:
{
NSLog(@"CoreBluetooth BLE hardware is powered on and ready");
NSArray *uuidArray = [NSArray arrayWithObjects:[CBUUID UUIDWithString:TRANSFER_SERVICE_UUID], nil];
NSDictionary *options = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES] forKey:CBCentralManagerScanOptionAllowDuplicatesKey];
[centralManager scanForPeripheralsWithServices:uuidArray options:options];
}
break;
case CBCentralManagerStateResetting:
NSLog(@"CoreBluetooth BLE hardware is resetting");
break;
case CBCentralManagerStateUnauthorized:
NSLog(@"CoreBluetooth BLE state is unauthorized");
break;
case CBCentralManagerStateUnknown:
NSLog(@"CoreBluetooth BLE state is unknown");
break;
case CBCentralManagerStateUnsupported:
NSLog(@"CoreBluetooth BLE hardware is unsupported on this platform");
break;
default:
break;
}
答案 1 :(得分:1)
在IOS 7中,即使在CBCentralManager准备就绪之前,您也可以通过启动BLE扫描来逃避。在这种情况下,IOS 7过去常常发出警告 -
CoreBluetooth [API MISUSE]只能在开机状态下接受命令
使用IOS8 - 不再显示警告,扫描实际上没有启动。要解决此问题,请等待CBCentral打开电源 - 即等待CBCentral管理器进入“CBCentralManagerStatePoweredOn”状态,然后开始扫描。它适用于这种变化:)