ESP32 BLE, does not connect a second time after disconnecting

saagcs
Posts: 1
Joined: Thu Sep 01, 2022 4:20 pm

ESP32 BLE, does not connect a second time after disconnecting

Postby saagcs » Thu Sep 01, 2022 4:28 pm

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

chegewara
Posts: 2364
Joined: Wed Jun 14, 2017 9:00 pm

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

Postby chegewara » Fri Sep 02, 2022 7:31 am

You have to "catch" onDisconnect() event and re-start advertising.

Who is online

Users browsing this forum: No registered users and 49 guests