Serial Bluetooth does not connect again

Hi!

I'm having a problem using Bluetooth Classic Serial.

I made a sketch that when connecting to ESP32 via bluetooth, it sends a message "page1" and after every 4 seconds it sends another "message1":

#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;
String msgB;
bool conect = true;
int timeB;


void setup() {
  Serial.begin(9600);
  SerialBT.begin("FILIPE_BT");
  timeB = millis();
}

void loop() {
  if (SerialBT.hasClient() && conect == true) {
    delay(500);
    writeStrBT("page1");
    conect = false;
  }

  msgBluetooth();

  if ((millis() - timeB) > 4000) {
    timeB = millis();
    writeStrBT("message1");
  }
}

void msgBluetooth() {
  if (!SerialBT.hasClient()) {
    Serial.println("NOT CONNECTED");
    conect = true;
  }
  if (SerialBT.available() > 0) {
    msgB = "";
    while (SerialBT.available() > 0) {
      char ch = SerialBT.read();
      msgB += ch;
      delay(5);
    }
    Serial.println("Message recived:");
    Serial.println(msgB);
  }
}

void writeStrBT(String str) {
  for(unsigned int i = 0; i<str.length(); i++) {
    SerialBT.write(str[i]);
    delay(10);
  }
  SerialBT.write(10);
}

This is working perfectly.

But when bluetooth disconnects, I can't connect again with Serial Bluetooth app for Android. I can only connect again if I restart ESP32.

Does anyone know why? Is there a problem with my code?

Thank you!

This ESP32 sketch just knocks out the temperature/s from an MLX90614.
I use an Android (and an Amazon 'Fire').
Never fails.

#include <Adafruit_MLX90614.h>
#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;

Adafruit_MLX90614 mlx = Adafruit_MLX90614();

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

  mlx.begin();
}

void loop() 
{
  SerialBT.print("Obj ");
  SerialBT.print(mlx.readObjectTempF());
  SerialBT.print(" F\r\n");
  SerialBT.print("Pkg ");
  SerialBT.print(mlx.readAmbientTempF());
  SerialBT.print(" F\r\n\r\n");
  
  delay(10000);
}

Initial connection and message exchange do not fail.
What fails is if the connection is lost for some reason, I can't reconnect, only if I restart ESP.

I can not confirm you issue. Using Kai Morich's Serial Bluetooth Terminal app on an Android phone with your code compiled with esp32 arduino core 2.0.0 I can disconnect and reconnect with page1 the message coming back.

What version of the esp32 core are you using. There was a bug in the disconnect/reconnect performance but it was fixed in 1.0.6

https://github.com/espressif/arduino-esp32/issues/4915

I'm using version 1.0.4...

Does this version have any bugs?

According to the Github thread on the bug, Arduino core version 1.0.4 was ok with and underlying esp IDF version 3.2 but I'm not clear on all the variations of the Arduino core.

Why would you want to use the older version of the core, when the current stable release is 2.2.2. I recommend that you update your core to the latest stable release.

I just updated my version to 2.2.2 and have no issue with disconnect and reconnect with your code.

Update the core version, and if you still have issues, install the Kai Morich terminal app on your phone.

I continued testing here with the Serial Bluetooth app. If I restart ESP, the connection loses and doesn't reconnect. Is there any way to reconnect via ESP? I tried using the MAC Address of the android device and it didn't work.

No. The ESP is the peripheral/server and the connection is done by the phone which is the central/client. A review of the BLE architecture would be helpful

https://devzone.nordicsemi.com/f/nordic-q-a/71/what-is-a-client-and-server-in-ble

A Peripheral can advertise, to let other devices know that it's there, but it is only a Central that can actually send a connection request to estalish a connection.