简体   繁体   中英

Reconnection procedure in iOS BLE application

Consider a scenario where i have a bonded connection established with a peer device from my iPhone 4S running iOS 5.1.

And then there is a disconnection due to some reason (either user initiated or otherwise).

In this case if the app has to auto reconnect to the previously paired device, as far as i know Apple exposes the API retrieve_peripheral which is supposed to give the call back did_retrieve_peripheral with the previously connected or paired peripheral structure filled.

And using this peripheral instance i can compare my previously paired uuid and if matches can attempt connection (reconnection). But i am not able to fetch the peripheral since the count is coming as 0 in did retrieve peripheral callback.

Am I missing anything?

Also have anybody tried replicating or storing the entire peripheral structure in the app which includes the (services discovered in the peripheral, characteristics etc). and used this in reconnection.

To use the retrievePeripheral API, you need to save the CBPeripheral UUID somewhere (I use CoreData). The retrievePeripheral call takes an NSArray of UUIDs as a parameter. Looks like you're not providing the saved UUID to your retrievePeripheral call. When your didRetrievePeripherals callback is called, you will get an array of CBPeripheral objects. You can then reconnect to these peripherals. You don't need to scan for these and you also won't need to pair with the peripherals.

I have the same problem and I think one solution: when connection is lost, masterhave to scan for peripherials in the same way that we scan and connect, and slave have to pass to advertisin state and both have to recognice caractheristics services uuid's and caracteristics for the services again.

This is the way I going to try to reconnect.

Of course I thing we have to refresh on your own way with some more functions

You should save the peripheral UUID (peripheral.identifier.UUIDString) when you successfully connect to the device. I am using NSUserDefaults to save the UUID.

When you want to connect to the device use the following method from CBCentralManager retrievePeripheralsWithIdentifiers: and pass as parameter the UUID. This will return an array of CBPeripherals, find yours and connect to it.

Don't try to save the CBPeripheral object anywhere. It is a very bad idea. Just use its UUID.

If your device is already connected to the phone you could retrieve the CBPeripheral object using the method retrieveConnectedPeripheralsWithServices from CBCentralManager. You need to pass the services that the peripheral is using as parameters.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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