简体   繁体   English

iOS10上的CoreBluetooth:CBPeripheral discoverServices后的超时

[英]CoreBluetooth on iOS10: Timeout after CBPeripheral discoverServices

Our App connects to Bluetooth LE Devices via CoreBluetooth. 我们的应用程序通过CoreBluetooth连接蓝牙LE设备。

On iOS 8 and 9 everything works correctly. 在iOS 8和9上,一切正常。 On iOS 10 we geht a Timeout Error ( Error Domain=CBErrorDomain Code=6 "The connection has timed out unexpectedly." ) 在iOS 10上,我们发现超时错误( Error Domain=CBErrorDomain Code=6 "The connection has timed out unexpectedly."
in the CBCentralManagerDelegate: CBCentralManagerDelegate:

- (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(nullable NSError *)error;

after calling discoverServices on a connected CBPeripheral . 在连接的CBPeripheral上调用discoverServices之后。

Does anyone know whats going wrong? 有谁知道什么出错了? Is this an iOS 10 issue? 这是iOS 10问题吗? Is there a certain BLE Log to check? 是否有某个BLE日志需要检查?

Setup iOS 10.0.1 (14A403) on iPad Pro 9.7 with a Nordic Semiconductor nRF51822 使用Nordic Semiconductor nRF51822在iPad Pro 9.7上安装iOS 10.0.1(14A403)

OK, I solved the Problem. 好的,我解决了问题。 I mixed up CBUUID and NSUUID 我混淆了CBUUIDNSUUID
starting with iOS 10 CBPeripheral discoverServices only accepts CBUUID . 从iOS 10开始CBPeripheral discoverServices只接受CBUUID NSUUID does not work anymore. NSUUID不再起作用了。 Maybe NSUUID only worked accidentally on older iOS versions. 也许NSUUID只是在较旧的iOS版本上偶然工作。 The documentation clearly states: 文件明确指出:

A list of CBUUID objects representing the service types to be discovered. 表示要发现的服务类型的CBUUID对象列表。

NSArray *services = @[
    [CBUUID UUIDWithString:ServiceUUID] //Correct
    //[[NSUUID alloc] initWithUUIDString:ServiceUUID] //Does work on iOS 9 but not on iOS 10
];
[self.peripheral discoverServices:services];

looking forward to port the app to strongly typed Swift. 期待将应用程序移植到强类型Swift。

Make sure you aren't allowing the CBPeripheral to be deallocated before peripheral:didDiscoverServices: is called in your CBCentralManagerDelegate . 请确保您不允许CBPeripheral之前被释放peripheral:didDiscoverServices:叫你CBCentralManagerDelegate This is easily accomplished by assigning the peripheral to a property, eg: 这可以通过将外围设备分配给属性来轻松完成,例如:

@property (nonatomic, string) CBPeripheral *peripheral;

...

- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral {

  self.peripheral = peripheral
  [peripheral discoverServices:<desired service UUIDs>];
  ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM