CoreBluetooth Issue: App Not Reconnecting Automatically After Restart (Bluetooth Keyboard App)

Hello everyone,

I'm experiencing an issue with CoreBluetooth in my Bluetooth keyboard app, and I'm hoping someone here might be able to help.

Description

My app uses CoreBluetooth to establish a connection between an iPhone and a computer, functioning as a Bluetooth keyboard. The app is built using Flutter, with custom native plugins for handling CoreBluetooth functionalities, and the app operates in the peripheral role. Here are the behaviors I've observed:

  1. When I turn Bluetooth on and off on both devices, the connection re-establishes automatically.
  2. When I move out of range and then come back within range, the connection re-establishes automatically.
  3. Rebooting both the app and the computer also results in an automatic reconnection.

Issue

However, when I force quit (kill) the app and then reopen it, the Bluetooth connection does not re-establish automatically.

Steps Taken

  1. I've added the necessary restoration key in the Info.plist.
  2. I have written the willRestoreState function, but I have not called .add(service) within it because doing so causes errors related to adding duplicate services.
  3. I've ensured that the app's Bluetooth permissions and background modes are correctly set.
  4. I've reviewed the CoreBluetooth documentation but haven't found a solution that addresses this specific scenario.

Questions

  1. Is there a specific method or approach I need to implement to ensure automatic reconnection after the app is restarted without causing duplicate service errors?
  2. Are there any best practices or common pitfalls I should be aware of when handling Bluetooth reconnections in CoreBluetooth?
  3. Could there be an issue with the state restoration process that I'm not aware of, or is there an alternative method to achieve the desired reconnection behavior?

Any insights or suggestions would be greatly appreciated!

Thank you in advance for your help.

Best regards, Pairat Atichart

Answered by Engineer in 796905022

If your app is relying on willRestoreState() to add services and otherwise prepare your app to act as a peripheral, keep in mind that a force quit of the app destroys the app's "state", and it will neither be "restored" (launched automatically) nor willRestoreState() will be called.

After a force quit the app must assume that it is starting up for the first time, and create all necessary CoreBluetooth objects, and take all necessary CoreBluetooth actions from scratch. If you are saving the state of certain objects and variables on disk to reuse after a launch, assume that all those will no longer be valid after a force quit. The same is true if you actually turn off Bluetooth from the Settings app as opposed to the Control Center.


Argun Tekant /  DTS Engineer / Core Technologies

If your app is relying on willRestoreState() to add services and otherwise prepare your app to act as a peripheral, keep in mind that a force quit of the app destroys the app's "state", and it will neither be "restored" (launched automatically) nor willRestoreState() will be called.

After a force quit the app must assume that it is starting up for the first time, and create all necessary CoreBluetooth objects, and take all necessary CoreBluetooth actions from scratch. If you are saving the state of certain objects and variables on disk to reuse after a launch, assume that all those will no longer be valid after a force quit. The same is true if you actually turn off Bluetooth from the Settings app as opposed to the Control Center.


Argun Tekant /  DTS Engineer / Core Technologies

CoreBluetooth Issue: App Not Reconnecting Automatically After Restart (Bluetooth Keyboard App)
 
 
Q