Can iOS reconnect to a Bluetooth LE peripheral device by specifying) CBPeripheral *) instead of retrievePeripherals?

When I get the didConnectPeripheral:(CBPeripheral *)peripheral delegate didConnectPeripheral:(CBPeripheral *)peripheral can I just store the peripheral device in an array and then use it to reconnect later, instead of using retrievePeripherals and its subsequent didRetrievePeripherals ? It seems to be easier if it is feasible and has no risk.

How much later can (CBPeripheral *)peripheral be reused? Is this permissible after disconnecting from this peripheral device?

Workflow:

  • scanForPeripheralsWithServices() - for scanning peripheral devices
  • didDiscoverPeripheral:(CBPeripheral *)peripheral - when connectPeripheral:peripheral is detected
  • didConnectPeripheral:(CBPeripheral *)peripheral stopScan and keep (CBPeripheral *)peripheral for later versions.
  • ... read or write specifications ...
  • cancelPeripheralConnection
  • didDisconnectPeripheral

LATER, TO CONNECT ...

  • connectPeripheral:peripheral - from an array with a peripheral device
  • didConnectPeripheral:(CBPeripheral *)peripheral ...
+4
source share
1 answer

YES, it will work (but it's a terrible practice). The retrievePeripherals: method was specially created so that you can reconnect to peripherals between subsequent launches of the application. You can use your method, but as soon as the application shuts down, you can never reconnect to the periphery (without putting it in advertising mode and starting from scratch basically). You can save the uuid between runs, but you cannot save the CBPeripheral object. So there is a big flaw.

So, to summarize: it will work, but it really won’t bring anything. This is no faster than calling retrievePeripherals: and then connecting them. Your suggested method limits your connectivity in CoreBluetooth . But, nevertheless, an interesting question.

+5
source

Source: https://habr.com/ru/post/1500977/


All Articles