连接到BLE设备后,该设备正在发布我感兴趣的特定服务,为了发现该服务,我打电话:

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

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

下面是委托(delegate)方法的完整内容。
- (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

最佳答案

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

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

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

关于iOS Core蓝牙: Advertised service is not discovered on peripheral,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27172556/

10-15 15:29