ESP32 Arduino Bluetooth Serial does not reconnect after disconnecting

Thread Starter

Elazar

Joined Oct 29, 2019
49
Hi all,

The basic ESP32 Arduino Bluetooth Serial example pairs and connects as expected, BUT once I disconnect the Bluetooth terminal it stops responding to new events at all,

once I reset the esp32, it starts working again with the same problem. (it doesn't matter if it keeps being paired or not)

I have tested it with the windows Bluetooth terminal and with the Bluetooth Any terminal. (from the windows store)

I have downloaded the latest esp32-Arduino library from GitHub and I still get the same problem.

I have tested it with several esp32 modules, and they all respond with the same problem.

So it looks like a bug in the library code.

See the simple Arduino example code below:

C++:
#include "BluetoothSerial.h"

#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif

BluetoothSerial SerialBT;

void setup() {
  Serial.begin(115200);
  SerialBT.begin("ESP32test"); //Bluetooth device name
  Serial.println("The device started, now you can pair it with bluetooth!");
}

void loop() {
  if (Serial.available()) {
    SerialBT.write(Serial.read());
  }
  if (SerialBT.available()) {
    Serial.write(SerialBT.read());
  }
  delay(20);
}
Does anyone know of another Bluetooth serial library that does work?

Thank You!
 

trebla

Joined Jun 29, 2019
542
I see in your code example the only events to respond are tied with Bluetooth. If you disconnect Bluetooth terminal then there aren't any event left for responding.
 

Thread Starter

Elazar

Joined Oct 29, 2019
49
Hi Trebla,

Correct,

However the problem is that I can't reconnect or pair again at all,
in order to reconnect I need to reset the ESP32

Once I press disconnect in the terminal it won't connect when pressing the connect button again.
 

ErnieM

Joined Apr 24, 2011
8,377
Your program looks like this:
Code:
Connect_To_BT()
Loop:
    DoSomeStuff()
    End Loop
What is missing? A check that BT is still active inside the loop, and some way to reconnect it if the connection drops.

I had a similar problem with my ESP32 and wifi. I could not figure out how to restore wifi once lost, so I had to issue a reset.

The fun part was resetting into the same state I was at.
 

Thread Starter

Elazar

Joined Oct 29, 2019
49
Well,
the Bluetooth and WiFi libraries in Arduino should keep working and reacting to new events in the background all the time,
and it should be able to reconnect after disconnecting!

if it doesn't reconnect, it means that it stopped working at some point, so it is probably a bug in the library!

I am working on debugging the library,

I also found code that works for the ESP32, (not yet for the Arduino) and it connects and disconnects many times without any problem, so I am working to make it compatible with the Arduino coding style, and API.
 

activerfid

Joined May 30, 2020
12
Hi all,

The basic ESP32 Arduino Bluetooth Serial example pairs and connects as expected, BUT once I disconnect the Bluetooth terminal it stops responding to new events at all,

once I reset the esp32, it starts working again with the same problem. (it doesn't matter if it keeps being paired or not)

I have tested it with the windows Bluetooth terminal and with the Bluetooth Any terminal. (from the windows store)

I have downloaded the latest esp32-Arduino library from GitHub and I still get the same problem.

I have tested it with several esp32 modules, and they all respond with the same problem.

So it looks like a bug in the library code.

See the simple Arduino example code below:

C++:
#include "BluetoothSerial.h"

#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif

BluetoothSerial SerialBT;

void setup() {
  Serial.begin(115200);
  SerialBT.begin("ESP32test"); //Bluetooth device name
  Serial.println("The device started, now you can pair it with bluetooth!");
}

void loop() {
  if (Serial.available()) {
    SerialBT.write(Serial.read());
  }
  if (SerialBT.available()) {
    Serial.write(SerialBT.read());
  }
  delay(20);
}
Does anyone know of another Bluetooth serial library that does work?

Thank You!
Similar issues with Bluetooth dropout on another ESP32-based product. At a guess I would say there is some sort of Bluetooth compatibility issue, possibly related to WiFi coexistence, which has not been implemented properly. Using a separate Bluetooth dongle (to the one already installed in my W10 PC) improved the issue I was seeing (pairing, dropout and then unable to re-pair) but still drops out occasionally.

Regards, Ralph
 

trebla

Joined Jun 29, 2019
542
the Bluetooth and WiFi libraries in Arduino should keep working and reacting to new events in the background all the time,
and it should be able to reconnect after disconnecting!
It will be nice but i don't think the library works in this way. If you look at this librays source file you find the function SerialBT.available() only checks the receive register status not connection status. You must rewrite your code so it can detect possible errors and react to them in predicted way or your system will be hang or give meaningless output.
One example of detecting connection status is here.
 

RobNevada

Joined Jul 29, 2019
66
Try placing
SerialBT.begin("ESP32test");
Under void loop. Once the program is loaded the define and void setup are only read once. The void loop runs over and over again. If the wifi is disconnected you would have to restart the board to see the connection again as the above code line is not active. You may have to set values that ask if wifi is active. If wifi is not active do this, else SerialBT.begin("ESP32test");
 

Thread Starter

Elazar

Joined Oct 29, 2019
49
Hi All,

It was a BUG in one of the library releases, now they have released a fix for this,
it can be downloaded and installed via the Arduino board manager,

I have updated the ESP32 boards to 1.0.6 and now it works fine!
 

rowifi1

Joined Jun 13, 2021
1
Hi All,

It was a BUG in one of the library releases, now they have released a fix for this,
it can be downloaded and installed via the Arduino board manager,

I have updated the ESP32 boards to 1.0.6 and now it works fine!
This is great, but how do Install this into platform IO. ???
 
Hi Trebla,

Correct,

However the problem is that I can't reconnect or pair again at all,
in order to reconnect I need to reset the ESP32

Once I press disconnect in the terminal it won't connect when pressing the connect button again.
Please call the pServer->getAdvertising()->start(); in void onDisconnect(BLEServer *pServer) function it solved the reconnect issue
 
Hi...I modified an application with the MIT AI, which essentially ought to simply associate with the board by calling its adress to trade message through sequential (no BLE utilized). After that it should detach again when squeezing another button.

To this point everything turns out great and true to form. Yet, here's the issue: When I attempt to interface again to the ESP32 by calling its adress as before it isn't reacting. The application reports that there is no association conceivable, solely after rebooting the board.

Did anyone experience such issue previously or can even assist with a functioning code?
 
Top