5

在连接到正在宣传我感兴趣的特定服务的 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
4

1 回答 1

0

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

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

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

于 2016-02-09T04:23:10.747 回答