Nik
Nik

Reputation: 1777

how to retrive the BTLE peripherals when user manually quit app from background?

Explanation :

I am connecting to BTLE peripheral using core bluetooth framework, saving the peripheral identifier in a database.
At the same time, I am saving the peripherals in an array to handle reconnection with peripherals.
But, when user manually quit the app, my array becomes empty.
due to which I won't be able to reconnect those peripherals again.

Now, I have peripherals identifiers in my database.
I have read about it and found that there is a delegate method

- (NSArray<CBPeripheral *> *)retrievePeripheralsWithIdentifiers:(NSArray<NSUUID *> *)identifiers NS_AVAILABLE(NA, 7_0)

To get the peripherals but it returns nothing.

How I can get the peripherals again?

https://developer.apple.com/library/content/documentation//NetworkingInternetWeb/Conceptual/CoreBluetooth_concepts/BestPracticesForInteractingWithARemotePeripheralDevice/BestPracticesForInteractingWithA

I also check above document. In that they mentioned about the methods to reconnect to the peripherals

Retrieving a List of Known Peripherals

The first time you discover a peripheral, the system generates an identifier (a UUID, represented by an NSUUID object) to identify the peripheral. You can then store this identifier (using, for instance, the resources of the NSUserDefaults class), and later use it to try to reconnect to the peripheral using the retrievePeripheralsWithIdentifiers: method of the CBCentralManager class. The following describes one way to use this method to reconnect to a peripheral you’ve previously connected to.

When your app launches, call the retrievePeripheralsWithIdentifiers: method, passing in an array containing the identifiers of the peripherals you’ve previously discovered and connected to (and whose identifiers you have saved), like this:

    knownPeripherals =
        [myCentralManager retrievePeripheralsWithIdentifiers:savedIdentifiers];
The central manager tries to match the identifiers you provided to the identifiers of previously discovered peripherals and returns the results as an array of CBPeripheral objects. If no matches are found, the array is empty and you should try one of the other two reconnection options. If the array is not empty, let the user select (in the UI) which peripheral to try to reconnect to.

When the user selects a peripheral, try to connect to it by calling the connectPeripheral:options: method of the CBCentralManager class. If the peripheral device is still available to be connected to, the central manager calls the centralManager:didConnectPeripheral: method of its delegate object and the peripheral device is successfully reconnected.

Note: A peripheral device may not be available to be connected to for a few reasons. For instance, the device may not be in the vicinity of the central. In addition, some Bluetooth low energy devices use a random device address that changes periodically. Therefore, even if the device is nearby, the address of the device may have changed since the last time it was discovered by the system, in which case the CBPeripheral object you are trying to connect to doesn’t correspond to the actual peripheral device. If you cannot reconnect to the peripheral because its address has changed, you must rediscover it using the scanForPeripheralsWithServices:options: method.

Upvotes: 2

Views: 873

Answers (1)

lipponen
lipponen

Reputation: 753

On application startup I would just discover all the peripherals and then compare discovered peripherals with the peripheral identifiers stored in the database.

After the comparison of the discovered peripherals and peripherals, the connections for desired peripherals can be made.

Upvotes: 0

Related Questions