5

当我获得委托时didConnectPeripheral:(CBPeripheral *)peripheral ,我可以将外围设备存储在一个数组中,然后使用它稍后重新连接,而不是使用 retrievePeripherals 及其后续didRetrievePeripherals?如果可行且没有风险,似乎会更容易。

多少以后可以(CBPeripheral *)peripheral重复使用?与该外围设备断开连接后它仍然有效吗?

工作流程:

  1. scanForPeripheralsWithServices()- 扫描外围设备
  2. didDiscoverPeripheral:(CBPeripheral *)peripheral- 当它被检测到时 connectPeripheral:peripheral
  3. didConnectPeripheral:(CBPeripheral *)peripheral停止扫描并存储以(CBPeripheral *)peripheral备后用。
  4. ...读取或写入特征...
  5. cancelPeripheralConnection
  6. didDisconnectPeripheral

稍后,要重新连接...

  1. connectPeripheral:peripheral- 从带有外围设备的阵列
  2. didConnectPeripheral:(CBPeripheral *)peripheral ...
4

1 回答 1

5

YES, it will work (but it's terrible practice). The retrievePeripherals: method was specifically created so that you can reconnect to peripherals between subsequent launches of an application. You can use your method, but once the app is shutdown, you will never be able to connect to the peripheral again (without putting it into advertising mode and starting from scratch basically). You can store the uuid between launches, but you cannot store a CBPeripheral object. So there's the big downside right there.

So to sum up: it will work, but it doesn't really gain you anything. It is not faster than calling retrievePeripherals: and then connecting them. Your suggested method is only limiting your abilities for connections in CoreBluetooth. But an interesting question nonetheless.

于 2013-09-06T17:36:39.010 回答