Bluetooth app not working properly, connecting and disconnecting randomly

Hello there.
I'm making a bluetooth app to connect to an esp32-Vroom-32 so that I can make the controls of a miniature house with LEDs lights and servo doors. My app connects to the esp32 but the buttons only work sometimes. So when I turn it on and off and then on again it seems to work for a few seconds. I'm stumped as to why this is happening. I stitched my code together from a few youtube videos on the topic.

The phone I'm using to test the app is a moto e6 play with android 9.0

I tried using the AI companion to see if there were any errors, but nothing conclusive showed up.

These are my blocks:

This is my app file:
AppWorking.aia (20.2 KB)

if I need to send something else or if you have a question, please ask away.

Hi Jairo

You can't request permissions without checking the Android version, and then requesting permissions that version requires. Search the forum for the permissions Classic Bluetooth requires on your version of Android. If you are going to distribute/share your App then your code needs to check the Android Version or SDK Version. You can use the extension "GetApiLevel" to do that.

You have a nice microprocessor, the Vroom supports Classic Bluetooth and BLE (and WiFi).

To help you with the logic of your button events, we need to see your esp32 Script (Sketch). I'm not available today but another Power User will pick-up the baton. :slight_smile:

Note that Bluetooth's enemy is metal obstacles. The Vroom's enemy is insufficient power.

https://www.professorcad.co.uk/appinventortips#TipsBluetooth
https://www.professorcad.co.uk/appinventortips#BluetoothFailure

Any reason why you are using BT instead of BLE?

There isn't one. This is my first app and I used the first bluetooth extension that I was able to find. Do you recommend I use a specific one? I searched in the forums and I found this one: BLE extension - Date Built: 2023-02-23 - permissions & connecting issuses on Android 12+ Is this good?

Hey Chris, thanks for the answer.

This the code I'm using for my esp32:
ESP32-Project.ino (3.7 KB)

I will try to work on using the extension you said. But I need to access my school's lab to be able to test the app so I might take a little while to respond.

Dear @Jairo_Lourenco_dos_S ,
wait a while !
In your ESP32 code you are using the classic BT and therefore you shall not use the BLE on AI2 side.
So, classic BT on ESP32 shall pair classic BT on AI2, as in your first post.

IMHO, due to its ease of use and long-term reliability, this is the best choice.
Later on, whether you'll need a lower power consumption, and once you have become familiar with the BT, you might pass to BLE. Only in that case (BLE) you need the extension.

Having said that, there are several things you shall pay attention to.
In your AI2 code you transmits bytes, while you assign strings to variables, that is:
image
Since button1 is assigned to be "11" (or "10"), this means a couple of characters "1" "0" or "1" 1".
Each one in ASCII is a byte (0x31 0x30) = "10"; (0x31 0x31) = "11". When you transmits a single byte, only the first one is transmitted.
If you still want to transmit only one byte at a time, you shall assign to the button1 variable a mathematical value (i.e. blue blocks).
image
Since you will do different actions according to the received data, while you don't use characters ?
By sending characters, i.e. "A", "B", "C",..... your case intruction on the ESP32 code will be simply
case "A":
....
...
break;
case "B":
...
...
break;
and so on without the need to work on 'dadorecibido' (which in turn shall be declared simply char)
In addition to this, I see that in your ESP32 code you send data to the app:
image

but where is the clock in AI2 to receive these data ? I don't see it in the blocks you posted.

In attachment you can find an old example that I did some time ago.
The app sends characters according to pushbuttons, the ESP32 receives those characters, displays them on the PC monitor and sends them back to AI2.
BT_to_ESP32.aia (25.5 KB)
ESP32_BT_Monitor.ino (1.4 KB)

Hoping it can help!

2 Likes

Hello @uskiara! Thanks so much for your response!

Wow, I hadn't even considered using characters in my case statements. That's brilliant!

If you're okay with it, I'd love to use your app as inspiration for my own project.

As for the data-sending part, I ended up abandoning that idea. Initially, I was planning to use a DHT11 sensor to monitor the humidity and temperature of my little 3D-printed house, but I ran into issues with the data transmission and now knowing how to display the text in the app.

I will make a few changes in my app and hopefully test it the day after tomorrow. I will keep you posted on any other issues I may have.

Thanks again.

1 Like

You're welcome !
Sure, feel free to use my app (whether it helps). This is the aim of the community :hugs:

Moreover, if you want to get experience you can also take a look to KIO4.com and to professorcad.co.uk (@Juan_Antonio and @ChrisWard ) web sites, where you can find "everything" you want to know about BT-Arduino-AI2
Huge information can be found also here (thanks to @abg):
FAQ Section: Bluetooth < version 4 (BlueTooth Client) and Arduino
Best wishes.

EDIT: if you want to transmit from Arduino (i.e. DHT11) please consider to transmit characters/strings also in that case. Since Temp & Hum are float type you can convert them to int, then to strings, so the AI2 app can use the receive.text, which is far simpler to handle.