Reboot the Bluetooth device when the application is completed by the user

I have a bluetooth device with a button on it. When the device button is clicked, the notification attribute update and my app show a local notification. It is so simple.

I am connecting a bluetooth device in the foreground as shown below:

var restoreOptions = [CBPeripheralManagerOptionRestoreIdentifierKey: "customIdentifier"] centralManager.connect(peripheral, options: restoreOptions) 

Then the set value of the notification flag is true:

  peripheral.setNotifyValue(true, for: notificationCharacteristic) 

When updating the notification features, the application displays a local notification:

  func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) { showLocalNotification() } 

When the notification attribute changes, doUpdateValueFor is started and a local notification is displayed. There are no problems in the foreground and in the background. But when the application is terminated by the user, the bluetooth device is disconnected and didUpdateValueFor does not start. I want to reconnect the Bluetooth device when a characteristic change even the application is terminated by the user. How can i do this? Hope I can tell you about my problem. Thanks.

+5
source share
1 answer

You need to enable and program the application for processing Core-Bluetooth background mode

this way you can connect to the device if your application is killed by the OS to free up some resources.

β€œEven if your application supports one or both of the Bluetooth background modes, it may not work forever. At some point, the system may need to terminate the application to free memory for the current foreground application, which causes any active or pending connections, for example , lose. Starting with iOS 7, Core Bluetooth supports saving state information for objects of the central and peripheral dispatchers and restoring this state when the application starts. It has the function to support the long-term action with the use of Bluetooth devices "

but you cannot reconnect if the user terminated your application.

0
source

Source: https://habr.com/ru/post/1274062/


All Articles