Page 1 of 1

ESP32 BLE, does not connect a second time after disconnecting

Posted: Thu Sep 01, 2022 4:28 pm
by saagcs
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:

Code: Select all

#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
Image

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

Disconnecting ESP32 C3 from APP
Image

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

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

Re: ESP32 BLE, does not connect a second time after disconnecting

Posted: Fri Sep 02, 2022 7:31 am
by chegewara
You have to "catch" onDisconnect() event and re-start advertising.