【问题标题】:iOS Core Bluetooth: Advertised service is not discovered on peripheraliOS 核心蓝牙:未在外围设备上发现广告服务
【发布时间】:2015-01-26 03:54:03
【问题描述】:

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

【问题讨论】:

  • 你的外设是什么?您是否尝试过应用商店中的 LightBlue 应用程序,看看它是否可以发现服务?您是否尝试过扫描 nil 而不是特定服务以查看会发生什么?
  • 它适用于 LightBlue。扫描nil 没有解决问题。我尝试使用 Nordic 提供的一些最新示例代码,而不是使用上述块的旧示例代码,现在它可以工作了 :) 谢谢

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


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-08-27
    • 1970-01-01
    • 1970-01-01
    • 2017-03-17
    • 1970-01-01
    • 2014-03-19
    • 2013-06-09
    • 2017-01-19
    相关资源
    最近更新 更多