Bluetooth Auto Connect without user action HC-05, HC-06, ESP32, no ListPicker or TinyDB needed

I made this Bluetooth auto connect program a while ago for my HC-05 connected to my Arduino Nano, and it has been working great, so I thought I would share my code for anyone who may need it.

If anyone spots an error or has any suggestions to improve it, please let me know.

How it works:
The user dose need to select a device to connect to. You do not need to use a ListPicker or save any settings to a TinyDB database.

Simply enter the name of your Bluetooth module in the “DeviceName” variable in the ConnectBT code block, e.g.: HC-05, or HC-06.
If you are using an ESP32, enter the name you set your Bluetooth module to in your ESP code.

When you start your App, the Auto Connect will determine the status of your device and Bluetooth module and display the appropriate help or connecting box then automatically connect.

It is fully automatic and will automatically re-connect if the signal is lost or warn if power to your module is lost. It is also speech enabled, to warn the user when an error is detected, and includes a simple animated connecting progress bar.

How to test:

  1. Unplug your Bluetooth module for a second, then plug it back in to simulate a lost connection.
    The App should display a “Connection Lost” box, then automatically re-connect in a few seconds.

  2. Unplug your Bluetooth module and LEAVE it unplugged.
    The App should display a “Connection Lost” box, then automatically try to re-connect.

    When it detects that the module is NOT plugged in, it should display a "Your Bluetooth device is not turned on" box and continue trying to re-connect.
    Plug in your module, it should automatically re-connect in a few seconds.

  3. Open your device settings and turn OFF Bluetooth.
    The App should display a "Unable to connect. Bluetooth is not enabled." box.
    Turn your device Bluetooth back on, the App should automatically re-connect in a few seconds.

  4. Open your device settings and un-pair your Bluetooth module.
    The App should display a "Unable to connect. Your device is not paired." box.
    Re-pair your Bluetooth module, the App should automatically re-connect in a few seconds.

Blocks:

blocks2

blocks3

AutoBtConnect2.aia (9.0 KB)

I hope someone may find this code useful.
Mike.

5 Likes

Wow Nice Topic, Keep it up :star_struck: :+1:
I suggest you change your topic tag from General Discussion to Tutorials and Guides
Because I think tags like that really don't match what you wrote. :sweat_smile:

Thanks, Salman_Dev.
I'll look into changing the topic tag.

Mike.

1 Like

It won't let me edit the topic tag, I guess a mod would have to move it to Tutorials and Guides, if they think it belongs there.

Thanks,
Mike.

1 Like

yeah you are right

How about you working on modifying the method? Change so that you don't have to manually enter the BT module name in blocks. When connecting to a module for the first time, the user selects the module to which he wants to connect. The next time you launch the app, it doesn't ask for BT anymore. There must also be a reset button for the learned module so that you can connect to another module.

If this is a tutorial, also remove unnecessary things like animations, redundant labels, clocks, and speech. The tutorial must be as simple as possible with as few blocks as possible to be understandable to novices.

Thank you for your input, Patryk.

This was not really meant to be a tutorial, hence the reason I chose the General Discussion category.

I was simply sharing an idea that works for me, in the hopes that other people would find it useful. It was only suggested by Salman_Dev that it might be better if I changed the category.

That is why I did not ask for it to be moved, but rather left it up to the moderators to decide.

Mike.

1 Like

Hi,
i am new to MIT, that i don't even know the basics. Can you please help me to know how can i plot a line graph of 6 analog reading from Arduino through one Bluetooth module.

after I download the aia file, I put the name of the device (microbit), but it is not work. It say that unable to connect. May I ask any missing procedures for me?

@Cheung
In my case (ESP32) also doesn't work. Did you solve the problem?

I'm so happy that you put this up