ESP32 BLE, does not connect a second time after disconnecting

Hello, I am having a problem with the ESP32 C3 BLE board, when I connect to it, from my mobile phone, and then I disconnect, later the device is not shown, when I scan it again from the app, it does not appear in the list of available devices. To reconnect I need to press the reset button on the ESP32 C3 board

My Arduino code:

/*
    Simple sketch to control a led with BLE protocol by
    Daniel Carrasco (https://www.electrosoftcloud.com)
*/
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEServer.h>
// See the following for generating UUIDs:
// https://www.uuidgenerator.net/
#define SERVICE_UUID        "xxxxxx-xxxxxx-xxxxxx-xxx-xxxxxxx"
#define CONTROL_CHARACTERISTIC_UUID "xxxxxxxx-xxxxx-xxxxx-xxxx-xxxxxxx"
#define LED 19
char ledStatus = 48; // 0 in ASCII
// New characteristic with object to manage it
BLECharacteristic controlCharacteristic(
  CONTROL_CHARACTERISTIC_UUID,
  BLECharacteristic::PROPERTY_READ |
  BLECharacteristic::PROPERTY_WRITE
);
void setup() {
  Serial.begin(115200);
  Serial.println("Starting BLE!");
  Serial.println("Initializing device");
  BLEDevice::init("Led control electrosoftcloud"); // Initializing the device with its name
  Serial.println("Creating server");
  BLEServer *pServer = BLEDevice::createServer(); // Create the server
  Serial.println("Adding service UUID");
  BLEService *pService = pServer->createService(SERVICE_UUID); // Creating a new service into server
  
  // Adding a characteristic with the object name (official UUID), without object (this characteristic will not change)
  Serial.println("Adding name characteristic");
  BLECharacteristic *nameCharacteristic = pService->createCharacteristic(
                                         BLEUUID((uint16_t)0x2A00),
                                         BLECharacteristic::PROPERTY_READ
                                       );
  nameCharacteristic->setValue("Led");
  // Adding a characteristic to control the led with 0 and 1
  Serial.println("Adding control characteristic");
  pService->addCharacteristic(&controlCharacteristic);
  controlCharacteristic.setValue(&ledStatus); // Value uint8_t with length 1
  Serial.println("Starting...");
  pService->start();
  Serial.println("Creating advertising");
  // BLEAdvertising *pAdvertising = pServer->getAdvertising();  // this still is working for backward compatibility
  BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
  pAdvertising->addServiceUUID(SERVICE_UUID);
  pAdvertising->setScanResponse(true);
  pAdvertising->setMinPreferred(0x06);  // functions that help with iPhone connections issue
  pAdvertising->setMinPreferred(0x12);
  BLEDevice::startAdvertising();
  Serial.println("Characteristic defined! Now you can read it in your phone!");
  pinMode (LED, OUTPUT); // Set the LED pin as OUTPUT
}
void loop() {
  std::string controlValue = controlCharacteristic.getValue();
  if (controlValue[0] != ledStatus) {
    Serial.print("Value changed... new value: ");
    Serial.println(controlValue[0]);
    ledStatus = controlValue[0];
    if (ledStatus == 48) {
      digitalWrite(LED, LOW); // LED Off
    }
    else if (ledStatus == 49) {
      digitalWrite(LED, HIGH); // LED On
    }
  }
}

My android APP Scan

Connecting ESP32 C3 device from my app
Device: Led control electrosoftcloud

Disconnecting ESP32 C3 from APP

Try scan again, but not show my ESP32 C3 devide for second time
Device: Led control electrosoftcloud

I don't know what I have wrong in my arduino code, since it is my first time with BLE
Thnks

You only connect once in setup().
I'd break out the connection code into its own function. Then, you can call it during setup.
At the beginning of loop(), poll the connection status. If it's disconnected, then call your connection function.

Hello, can you explain to me what piece of code I should put in the loop(), since I am new to ESP32 BLE, thanks

Looks to be everything in setup() except the last line about the LED.
But you may have to wait a bit so the other device recognized the disconnect.

Same problem here.
Following.

A bump in a 6 month old thread isn't likely to be super productive.
Try posting your code using code tags </>

I have the identical situation. What's the point of repeating it in a new thread??

Have you tried the suggestions posted in the thread?

Have since moved over to the ArduinoBLE library.

You must start advertising again after a disconnection.
Use: pServer->startAdvertising();

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.