Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can iOS re-connect to Bluetooth LE peripheral by specifying )CBPeripheral *)peripheral instead of doing retrievePeripherals?

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:

  1. scanForPeripheralsWithServices() - to scan for a peripheral
  2. didDiscoverPeripheral:(CBPeripheral *)peripheral - when it's detected connectPeripheral:peripheral
  3. didConnectPeripheral:(CBPeripheral *)peripheral stopScan and store the (CBPeripheral *)peripheral for later.
  4. ... read or write characteristics ...
  5. cancelPeripheralConnection
  6. didDisconnectPeripheral

LATER, TO RE-CONNECT...

  1. connectPeripheral:peripheral - from array with peripheral
  2. didConnectPeripheral:(CBPeripheral *)peripheral ...
like image 823
Doug Null Avatar asked Sep 06 '13 16:09

Doug Null


1 Answers

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.

like image 200
Tommy Devoy Avatar answered Sep 18 '22 21:09

Tommy Devoy