How can I check BLE "IsDeviceConnected" on start?

Hello,
I'm pretty new to this, but I've been trying to chip away at different tutorials and example projects.

My goal is to have an app that autoconnects to specific, saved bluetooth devices.
I have a BlueFruit Circuit Playground with the bleuart.ino example sketch.

My problem is that I connect, then refresh the companion screen to simulate restart. The device is already connected (solid red light), but the app thinks it is not connected and the state of the app doesn't match the connection status. IsDeviceConnected and ConnectedDeviceName are false or blank. So I'm trying to handle this case.

I also get some strange behavior where the device would disconnect and reconnect multiple times.

The current (intended) workflow is

  1. App Loads
  2. If there is already a BLE device connected, show that view and display the name of the device connected.
    -> Otherwise Start a Clock, and start scanning for bluetooth devices nearby.
  3. As devices are found, check for the BLE device name to contain a keyword. (I figure I can set that).
    -> If the name doesn't match the keyword, it ignores that device.
    -> Otherwise it puts it in a list.
  4. When the clock times out, stop scanning for devices. Check the list of "filtered" devices against the database of saved devices by MAC address (I figured those will be unique).
    -> If there's one match - connect to it.
    -> If there's no matches, show the list of "filtered" devices for the user to select and connect to that.
    -> If there's more than one match, show the list of "filtered" devices for the user to select and connect to that.
  5. If a user selected a device, and we connected successfully, try to add it to the database for next time.

Is there a trick to some of the BluetoothLE extension statuses? Do I need delays or communication initialization step? Or maybe there's a better way for me to check the connection status?

I would very much appreciate any feedback or guidance to improve.
Thanks so much!


image

DB_Autoconnect_BLE.aia (211.0 KB)

Probable move this option in Blocks view and place within the when Screen1.Initialize block above the Restart_App procedure call, as it needs to be set true before anything else happens and see if that helps:
image

Certainly, you need to have AutoConnect enabled.

Then it could simply be down to timing.