【问题标题】:iOS8 and BTLE | CBCentralManager unable to find peripheralsiOS8 和 BTLE | CBCentralManager 找不到外围设备
【发布时间】:2014-10-12 21:42:14
【问题描述】:
我有一个使用 BTLE 连接到设备 (arduino) 的 iOS 应用。在我的 iPad iOS 7 上一切正常。升级到 iOS 8 后,CBCentralManager 没有找到任何外围设备。
- (void)startScanningForSupportedUUIDs
{
[self.centralManager scanForPeripheralsWithServices:nil options:nil];
}
我不知道可能是什么问题。
【问题讨论】:
标签:
ios
ios8
core-bluetooth
btle
【解决方案1】:
我有解决方案,由于某些原因,在 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;
}
【解决方案2】:
在 IOS 7 中,即使在 CBCentralManager 准备好之前,您也可以通过启动 BLE 扫描来摆脱困境。 IOS 7 曾经在这种情况下发出警告 -
CoreBluetooth[API MISUSE] 只能在开机状态下接受命令
使用 IOS8 - 警告不再出现,扫描实际上并没有开始。要解决此问题,请等待 CBCentral 开机 - 即等待 CBCentral 管理器进入“CBCentralManagerStatePoweredOn”状态,然后开始扫描。它适用于该更改:)