iOS CoreBluetooth不扫描iPad Air中的服务

时间:2014-01-04 09:47:24

标签: ios ipad core-bluetooth

我正在开发一个连接到BLE外设并从中接收数据的应用程序。

它扫描外围设备,查找外围设备,发现服务,如果找到了正确的服务,它会接收数据。

它在iPhone 5上运行良好,但是当我在iPad Air上运行它时,它会连接,但不会发现任何服务,也不会收到任何数据。两台设备都运行iOS 7.0.4

这是代码的一些相关部分

- (void)startScan
{

    [manager scanForPeripheralsWithServices:nil options:nil];
}

- (void)stopScan
{
    [manager stopScan];
}



- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral    *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI
{
NSLog(@"Did discover peripheral. peripheral: %@ rssi: %@, id: %@ advertisementData: %@ ", peripheral, RSSI, peripheral.identifier, advertisementData);

    if(![self.dicoveredPeripherals containsObject:peripheral])
        [self.dicoveredPeripherals addObject:peripheral];

    [manager retrievePeripheralsWithIdentifiers:[NSArray arrayWithObject:(id)peripheral.identifier]];

    [manager connectPeripheral:peripheral options:[NSDictionary dictionaryWithObject:   [NSNumber numberWithBool:YES] forKey:CBConnectPeripheralOptionNotifyOnDisconnectionKey]];
}


- (void)centralManager:(CBCentralManager *)central didRetrievePeripherals:(NSArray *)peripherals
{
    NSLog(@"Retrieved peripheral: %lu - %@", (unsigned long)[peripherals count], peripherals);
    [self stopScan];

    /* If there are any known devices, automatically connect to it.*/
    if([peripherals count] >= 1)
    {
        testPeripheral = [peripherals objectAtIndex:0];

        [manager connectPeripheral:testPeripheral
                       options:[NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES] forKey:CBConnectPeripheralOptionNotifyOnDisconnectionKey]];
    }
}


- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral
{
    NSLog(@"Did connect to peripheral: %@", peripheral);
    [self.delegate statusMessage:[NSString stringWithFormat:@"Did connect to peripheral: %@\n", peripheral]];

    [peripheral setDelegate:self];
    [peripheral discoverServices:nil];
}


- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error
{
for (CBService *service in peripheral.services) {
        NSLog(@"discovered service [%@]",service.UUID);
        [peripheral discoverCharacteristics:nil forService:service];
    }
}

iPhone 5上的日志输出是:

Did discover peripheral. peripheral: <CBPeripheral: 0x14587320 identifier = 3EE10DD6-99CD-  7E9C-C492-087CE3B980E6, Name = "BLE Sens", state = disconnected> rssi: -53, id:     <__NSConcreteUUID 0x145f82f0> 3EE10DD6-99CD-7E9C-C492-087CE3B980E6 advertisementData: {
kCBAdvDataChannel = 38;
kCBAdvDataIsConnectable = 1;
kCBAdvDataLocalName = BLESensor;
kCBAdvDataServiceUUIDs =     (
    "Unknown (<fff0>)"
);
kCBAdvDataTxPowerLevel = 0;
} 

Did connect to peripheral: <CBPeripheral: 0x14587320 identifier = 3EE10DD6-99CD-7E9C-C492-087CE3B980E6, Name = "BLE Sens", state = connected>

Connected
discovered service [Device Information]
discovered service [Unknown (<fff0>)]
discovered service [Unknown (<ffe0>)]
Service found with UUID: Device Information

在iPadAir上它只是到达连接部分而没有找到任何服务:

Did discover peripheral. peripheral: <CBPeripheral: 0x1700a1440 identifier = 23890BB4-FB07-AA1A-8D4F-B1427C859447, Name = "BLE Sens", state = disconnected> rssi: -60, id:   <__NSConcreteUUID 0x17802f740> 23890BB4-FB07-AA1A-8D4F-B1427C859447 advertisementData: {
    kCBAdvDataChannel = 39;
kCBAdvDataIsConnectable = 1;
kCBAdvDataLocalName = BLESensor;
kCBAdvDataServiceUUIDs =     (
    "Unknown (<fff0>)"
);
kCBAdvDataTxPowerLevel = 0;
} 

Did connect to peripheral: <CBPeripheral: 0x1700a1440 identifier = 23890BB4-FB07-AA1A-8D4F-B1427C859447, Name = "BLE Sens", state = connected>
 Connected 

1 个答案:

答案 0 :(得分:0)

Apple承认这是iOS 7.0中的一个问题。他们承诺在iOS 7.1中解决它。我用iOS 7.1测试了它,它已经解决了。