ESP32 Bluetooth does not reconnect after multiple connection disconnection

ranjitlawand
Posts: 1
Joined: Wed Apr 12, 2023 1:35 pm

ESP32 Bluetooth does not reconnect after multiple connection disconnection

Postby ranjitlawand » Wed Apr 12, 2023 1:42 pm

Hello,

I’m currently using ESP32 with Platform IO and Arduino framework. The basic workflow is receiving and sending data from an Android application using classic Bluetooth and communicating it to another controller over UART. We are also using wifi to send data packets to MQTT.

The flow will work fine for 150 - 200 connections but also for 1200 - 1500 connections meaning that we are unable to predict when ESP32 will not accept any connection request over Bluetooth. At certain times ESP32 will begin accepting connection requests without any restarts, at other times a restart is required.

The Bluetooth and wifi are working simultaneously. We receive data on MQTT with wifi even when ESP32 stops accepting Bluetooth requests.

We see two kinds of behavior:-

Android app is not able to connect to ESP32 at all.
Android app is able to connect but the connection is disconnected within a few milliseconds.
But rarely do we see that a few connections are successful and we are able to complete data exchange in this state.

We are using SerialBT.flush() after sending data packets over Bluetooth. After every disconnection, we call functions in the following order after every disconnection.

Code: Select all

void initBT() {
  if (!SerialBT.begin(deviceId)) {
    Serial.println("An error occurred initializing Bluetooth");
    ESP.restart();
  } else {
    Serial.println("Bluetooth initialized");
  }

  SerialBT.register_callback(btCallback);
  Serial.println("The device started, now you can pair it with bluetooth");
}

SerialBT.clearWriteError();
SerialBT.flush();
SerialBT.end();
delay(10);

if(restartBluetoothAfterSyncFlag == true)
{
  restartBluetoothAfterSyncFlag = false;
  btStop();
  delay(100);
  btStart();
  delay(100);
}

Serial.println("The bluetooth is reinitialized!");
initBT();
The restartBluetoothAfterSyncFlag flag makes sure that Bluetooth is only restarted after communication with the application.

The second issue is ESP32 connects to the Android application and disconnects within a second.

We want to figure out these 2 issues. Is there anything missing in the firmware which is causing the issue or is it something related to ESP32 or Arduino Framework or Serial Bluetooth or any library?

ESP Platform version - 6.1.0

Libraries -
Arduino
WiFi
WiFiClient
HTTPClient
ESP32_FTPClient
SPIFFS
ArduinoJson
SerialTransfer
PubSubClient
Wire
BluetoothSerial

Who is online

Users browsing this forum: Baidu [Spider] and 88 guests