ESP32 bluetooth disconnecting randomly

My esp32 disconnects from the app I created randomly. does anyone have a solution or trouble shoot to find the cause?

Not enough information...

does it do this when you don't send any data for a while or are you sending data all the time.
Are you using serial Bluetooth or BLE?

Are you switching screens?

It usually happens when the app is idle. I am using serial bluetooth. #include "BluetoothSerial.h"
String DATA;
#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run make menuconfig to and enable it
#endif

I only use one screen for this.

I connect the app to the esp32 via bluetooth and then it disconnects while app is idle.

Hello Peter

How long is it idle for approximately?

Sure you don't have a Clock Timer in the App that could interrupt?

What is your Android Device Make/Model and Android version - could this be a battery saving feature of the device?

the time could be 5min or 20 min. seems random.
I have three clocks running in the app
I am going to try a newer phone tomorrow.

Hi @Peter_C,
as Chris said, it could be the power saving feature that switches off the BT, therefore causing the ESP32 to disconnect after a while.
Just to troubleshoot the root cause, could you make your app to "stay alive" i.e. firing a clock every 1 minute and do a BT handshake between app and ESP (for example transmitting one character from the app to the ESP and visa versa) ?
There are exensions like those made by @Taifun that enable the screen to stay on so the app never goes to sleep. To save batteries, you can also add another extension that reduces the luminosity of the screen to a minimum.
I agree that these aren't the final solution, but maybe this allows you to find the root cause.
BTW on ESP32 side, do you have perhaps a "timeout" that disconnects the BT after a period of latency ?
All the best.

1 Like

Thanks. I do use a clock to keep checking the connection and keep the screen alive. It actually plays a sound when the bluetooth disconnects. I am now. thinking the disconnect is comming from the ESP32 and not the app.

If that's so, (ESP32 disconnection) you can avoid this by sending periodically a handshake character i.e. ESP32 Tx("?") => APP; answer: APP => Tx("?") back to ESP. In this case you can also verify whether the transmitted character is sent back the same ("?" == "?").
But, if in the meanwhile the app goes to sleep and does not answer to the ESP, probably the ESP (in the library itself) decides to stop the communication, anyway.
In one project of mine, I have an ESP32 exchanging data with a pad for hours, without any trouble. But in my case, tha app stays alive always.
One hint for ESP32: please verify if your Arduino IDE version uses the "correct" board,: in some cases the delay() function blocks the serial line, and consequently also the BT. I've seen that not in all versions of the IDE this happens, and behaves differently upon the board being selected.

Or, to discriminate if it is your app or the ESP32 the root cause, you can use a BT terminal app like Serial Bluetooth Terminal, which allows you to monitor the BT Tx and Rx from your device to your phone (or pad running the terminal app), without running your app.

1 Like

well here it is. looks like the 5v power line is too noisy. Now I am using a USB to get the power to the ESP32. so far, so good. Peter

2 posts were split to a new topic: Data from BLE are read only once

Hello Peter

...could it be that the power cable is routed too close to the ESP32? I would be choosing a power supply (of the correct amperage and decent quality) instead of a USB source.