9

我有一个使用 BTLE 连接到设备(arduino)的 iOS 应用程序。在我的 iPad iOS 7 上一切正常。升级到 iOS 8 后,CBCentralManager 没有找到任何外围设备。

- (void)startScanningForSupportedUUIDs
{
   [self.centralManager scanForPeripheralsWithServices:nil options:nil];

}

我不知道可能是什么问题。

4

2 回答 2

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;
}
于 2014-09-08T20:45:44.277 回答
1

在 IOS 7 中,您甚至可以在 CBCentralManager 准备好之前通过启动 BLE 扫描来摆脱困境。IOS 7 曾经在这种情况下发出警告 -

CoreBluetooth[API MISUSE] 只能在开机状态下接受命令

使用 IOS8 - 警告不再出现,扫描实际上并没有开始。要解决此问题,请等待 CBCentral 开机 - 即等待 CBCentral 管理器进入“CBCentralManagerStatePoweredOn”状态,然后开始扫描。它适用于该更改:)

于 2014-10-16T23:09:09.070 回答