Automatic connection with a BT device, after reboot or shutdown of that BT device (hc-06)

I want to be able to establish an automatic connection with a BT device and maintain its connectivity even after reboot or shutdown of this BT device (hc-06).

  1. In the first step and when I open the app for the first time, the Bluetooth is connecting automatically and the connection is OK.

  2. And now, in the case of shutdown of electricity, (the hc-06 is OFF), and the status of the connection is maintained on (Connected with a green text color). This is the first problem.

  3. Then the electricity has come back so (hc-06 is ON). At this moment the app can’t connect automatically. And the status of the connection is maintained on (Connected with a green text color).

  4. How can I test the state of the hc-06? and how can I reconnect it automatically?

Can you show us your connectivity blocks?
I think you should be able to ask using a timer, whether your device is still connected.
It will never tell you be itself if it was disconnected.
Cheers, Ghica.


App Inventor will not find out whether your BT device switched off, unless you send it something. In that case you will get a Screen error, which you can catch by using the when … ErrorOccurred:
Here I show only the message you see. You can also check for the error number etc.
If you encounter that situation, you should disconnect and then you can try again, I assume.
Cheers, Ghica.

1. The message at the beginning of clock1 is sent continuously to test, as you said if there is an error message, it will be detected, and consequently put the connection statue in red and disconnected, and also disconnects the BT.
Until now it works as I want: when there is no electricity the application is not connected and as soon as I turn on the electricity the application automatically connects.

2. just there is only one problem: it is that when the application disconnects following the power cut, I cannot press the buttons as if they are blocked, the application hangs for a while. If I power the BT again it reconnects directly no problem. But still a liver as soon as I cut the BT’s power supply it disconnects and it blocks, and an error message appears.

3. There is a solution: I have to stop the clock1, and like that, the application does not hang when it disconnects, but I did not know how to reactivate the clock1 as soon as the BT is powered.
how can i detect that the BT is powered, so that i activate clock1 again.

4. so we come back to the same problem at the beginning. :frowning:

I do not understand at all what you are trying to do. You have a list picker to connect with a BT device that is available, but that is not necessarily 98:D3:51:FD:28. What is the purpose of these blocks?

Then, you have now a Screen1.ErrorOccurred block, but in there you are ignoring what the error was. Although it is unlikely that there are other errors than the “Broken pipe” message that I showed, you still have to check for that properly. The error number would be the best thing to check, just by trying and displaying a notifyer or using DoIt you can find out what it is. So, you should delete that set message block, because it is set as an input parameter. It is bad manners to override it in this way.

What is the normal way of communicating with your BT device? I doubt that this is sending “mmm” messages! I think you should take that part out. What will happen now is:

  • Disable the timer at Screen1.Initialize
  • Assume the power on your BT device is cut.
  • This means you get an error message. The Screen1.ErrorOccured block is activated
  • In this block: set your labels, disconnect your BT device and enable the timer
  • There is a newer block, DisconnectOnError that you could maybe use instead of the Screen1 error block, I did not try it. If you do, you should keep the time activated I think.
  • the timer will now periodically check if it can connect in the way you do it already now, except the sendText seems useless to me and may cause problems in your BT device.

Try things out. Not all BT devices are created equal.
Cheers, Ghica.

I try doing same auto connection
this is my sketch

it works well concerning autoconnection:
after first connection when i tap button "connect", if HC05 shut down, it will reconnect as soon as it power on again.

the only problem i have with this is freezing when HC05 is out of connection. then if you tap on "Test" button the counter above it need lot of time to increment...(it works well as soon as connection is established again)

is ther a way to try connecting without freeze app ?

i got it:
using assync extension.