When I get delegate didConnectPeripheral:(CBPeripheral *)peripheral
can I just store peripheral in an array, and then use it to re-connect later, instead of using retrievePeripherals and its subsequent didRetrievePeripherals
?
Seems like it would be easier, if it's feasible and has no risk.
How much later can (CBPeripheral *)peripheral
be re-used? Is it still valid after disconnection with that peripheral?
Workflow:
scanForPeripheralsWithServices()
- to scan for a peripheraldidDiscoverPeripheral:(CBPeripheral *)peripheral
- when it's detected
connectPeripheral:peripheral
didConnectPeripheral:(CBPeripheral *)peripheral
stopScan and store the (CBPeripheral *)peripheral
for later.cancelPeripheralConnection
didDisconnectPeripheral
LATER, TO RE-CONNECT...
connectPeripheral:peripheral
- from array with peripheraldidConnectPeripheral:(CBPeripheral *)peripheral
...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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With