iOS Core Bluetooth:在外围设备上未发现广告服务

时间:2014-11-27 14:06:43

标签: ios objective-c xcode core-bluetooth cbperipheral

在连接到广告我感兴趣的特定服务的BLE设备后,为了发现这项服务,我致电:

[self.peripheral discoverServices:@[[self.class serviceUUID]]];

调用- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error委托方法时没有错误,但为外设返回的服务数为0,因此没有进一步发现任何特征。有谁知道为什么会这样?提前致谢!

以下是委托方法的完整主体。

- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error {
    if (error) {
        NSLog(@"didDiscoverServices failed: %@", error);
        return;
    }

    NSLog(@"didDiscoverServices succeeded for peripheral: %@.", peripheral.name);
    NSLog(@"Number of services: %d", peripheral.services.count);

    for (CBService *s in peripheral.services) {
        NSLog (@"Discovered service UUID: %@", s.UUID);
        if ([s.UUID isEqual:[self.class serviceUUID]]) {
            NSLog(@"Discover characteristics...");
            [self.peripheral discoverCharacteristics:@[[self.class controlPointCharacteristicUUID], [self.class packetCharacteristicUUID]] forService:s];
        }
    }
}

控制台显示:

2014-11-27 15:54:51.933 k[197:13362] didDiscoverServices succeeded for peripheral: BootK
2014-11-27 15:54:51.934 k[197:13362] Number of services: 0

1 个答案:

答案 0 :(得分:0)

我唯一能想到的是[self.class serviceUUID]与您正在寻找的服务的UUID不匹配。当您启动扫描时,是否按服务UUID进行过滤?

[self.centralManager scanForPeripheralsWithServices:[self.class serviceUUID] options:nil];

如果没有,这可以解释为什么您能够发现并连接到它,但却没有发现该特定服务。