iOS8 and BTLE | CBCentralManager unable to find peripherals

南笙酒味 提交于 2020-01-01 09:05:08

问题


I have an iOS app that is connecting to a device (arduino) using a BTLE. Everything is working fine on my iPad iOS 7. After upgrading to iOS 8, the CBCentralManager is not finding any peripherals.

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

}

I don't know what can be the problem.


回答1:


I have the solution, for some reason in iOS 8 there is some delay after instantiate your CBManager. You need to start to scan when the CBCentralManager is on, in this method:

-(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:


In IOS 7 you could get away by starting a BLE scan even before the CBCentralManager was ready. IOS 7 used to spit out a warning in such cases -

CoreBluetooth[API MISUSE] can only accept commands while in the powered on state

With IOS8 - the warning no more appears and the scan does not actually start. To overcome the problem, wait for the CBCentral to power on - ie, wait for CBCentral manager to get to the "CBCentralManagerStatePoweredOn" state and then start the scan. It works fine with that change:)



来源:https://stackoverflow.com/questions/25389358/ios8-and-btle-cbcentralmanager-unable-to-find-peripherals

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!