Question

In project I'm currently working on I'm using singleton class to deal with all Bluetooth related tasks. It's CBCentralManager object handles connection with multiple peripherals. When CBPeripherals are discovered, I let user pick peripheral and connect to it. When connection is established, he/she can pick another available device, so connection is done sequentially.

But when it comes to retrieving connection, I get an array of retrieved peripherals. What would be the best practice to retrieve multiple peripherals? Is it OK to create multiple connection request to peripheral at once, or should it be done sequentially ?

Right now I'm looping through retrieved peripherals and trying to connect with them without waiting. It works, but it takes some time, although it may be problem with my ble hardware I'm connecting to.

What's more when I fire method retrievePeripheralsWithIdentifiers: it gives me no delegate callback, so I'm doing retrieving in centralManager:didDisconnectPeripheral:error: method, where I do

NSArray *retrieved = [self.centralMgr retrievePeripheralsWithIdentifiers:identifiers];

and then loop through retrieved objects.

It works, but is it a correct way ?

Was it helpful?

Solution

In iOS 7 the Core Bluetooth API was updated. retrievePeripheralsWithIdentifiers: directly returns the peripherals you request and there is no callback any more.

Rule of thumb is if you have a CBPeripheral object, then you can reuse it. There is no need to retrieve again. Your question is a little confusing regarding your current way of using the APIs but whenever you're in doubt you can refer to the Core Bluetooth Programming Guide.

There is only one exception: if the Core Bluetooth framework restarts or is power cycled, then the CBPeripheral objects are invalidated. Reuse will result in errors. In this case, new instanced must be obtained.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top