【问题标题】:CBPeripheral's discoverServices not returning anythingCBPeripheral 的发现服务不返回任何内容
【发布时间】:2014-05-15 02:51:36
【问题描述】:

我正在尝试连接到蓝牙外围设备,但无法发现服务。当设备被发现时,我打印广告数据并显示以下内容:

{
    kCBAdvDataChannel = 38;
    kCBAdvDataIsConnectable = 1;
    kCBAdvDataLocalName = UR;
    kCBAdvDataServiceUUIDs =     (
        1802
    );
}

如您所见,广告中显然有一项服务。但是,连接后我使用以下代码:

- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral {
    DDLogInfo(@"Connected to %@", peripheral.name);
    [self.bluetoothManager stopScan];
    if (peripheral == self.connectedPeripheral && self.connectedPeripheral.state ==  CBPeripheralStateConnected){
        [self.connectedPeripheral setDelegate:self];
        [self.connectedPeripheral discoverServices:nil];
    }
}

但是 - (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error 永远不会被调用。我还确保在didDisconnectPeripheral 上设置一个断点,以防万一它由于某种原因断开连接,但它似乎没有任何区别。

如果我将scanForPeripheralsWithServices: 与前面提到的服务的UDID 一起使用,也会出现同样的问题,它不会显示任何结果。

我尝试连接的设备是一台 Mac,它使用 IOBluetooth 作为外围设备发布其服务。

你知道这里可能出了什么问题吗?

【问题讨论】:

  • 什么时候设置self.connectedPeripheral?您的代码是否通过了您的 if 测试?
  • centralManager: didDiscoverPeripheral。这只是一个预防措施,以确保它不会被释放,我之前得到了一个错误。是的,它通过了。
  • 奇怪,我进入didConnectPeripheral时一般都会保留这个外设。似乎更合适。尝试重新启动 iPhone。
  • 我会尝试将那部分代码移到那里,看看它是否有所作为。尝试了两部独立的 iPhone。
  • 如果我在连接之前没有保存它,我会收到此错误:CoreBluetooth[WARNING] <CBPeripheral: 0x14e9db20 identifier = 79836C48-3513-F357-EC58-3C32181E2452, Name = "UR", state = connecting> is being dealloc'ed while pending connection

标签: ios objective-c macos bluetooth bluetooth-lowenergy


【解决方案1】:

错误似乎源于服务器实现。很可能是因为没有正确保留东西。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-15
    • 1970-01-01
    • 1970-01-01
    • 2021-11-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多