Annakkili Arumugam
Annakkili Arumugam

Reputation: 71

ESP32 BLE advertising stopped once the device is connected:

I am working on the ESP32 BLE. I am using Arduino IDE for my programming.

In my project I want to use BLE in continuous advertising mode. But actually what it happen means, the BLE advertise is stopped once the device is connected to BLE app for one time. After that the BLE advertise is stopped. I can't able to connect to that BLE device after that. I can't even see its advertising in BLE mobile app (used open source mobile app: nRF connect, BLE scanner..). Then it starts its advertising process after only the Hard reset was happened in the ESP32 board.

I faced this issue when the BLE code is compiled in my Laptop. I have checked 3 or more system, in that the base example BLE_server (in-build ESP32 Arduino IDE example)code which was compiled in one system was worked perfectly. The the code which was compiled in the remaining 3 systems causes the issue which I have mentioned above.I have attached the Arduino IDE configuration details- image here. kindly do find the attachment.

Here I have used Arduino IDE version 1.8.13. In my laptop I have used Java(TM) SE development kit 18.0.1.1(64 bit) & python version 2.7.15.

I have updated the Arduino IDE version And ESP32 board version to.

What is the reason for this issue? is there any configuration missing in my Arduino IDE?

Is there may be any issue in compilation process?

Kindly help me to over come this issue?

Waiting for your positive reply.

Thanks in advance!!!

Upvotes: 7

Views: 3725

Answers (1)

fbiego
fbiego

Reputation: 418

Advertising stops once the device is connected, you need to restart it once that happens

/*
    Based on Neil Kolban example for IDF: https://github.com/nkolban/esp32-snippets/blob/master/cpp_utils/tests/BLE%20Tests/SampleServer.cpp
    Ported to Arduino ESP32 by Evandro Copercini
    updates by chegewara
*/

#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEServer.h>

// See the following for generating UUIDs:
// https://www.uuidgenerator.net/

#define SERVICE_UUID        "4fafc201-1fb5-459e-8fcc-c5c9c331914b"
#define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8"

class MyServerCallbacks: public BLEServerCallbacks {
    void onConnect(BLEServer* pServer) {
      pServer->startAdvertising(); // restart advertising
    };

    void onDisconnect(BLEServer* pServer) {
      pServer->startAdvertising(); // restart advertising
    }
};

void setup() {
  Serial.begin(115200);
  Serial.println("Starting BLE work!");

  BLEDevice::init("Long name works now");
  BLEServer *pServer = BLEDevice::createServer();
  
  pServer->setCallbacks(new MyServerCallbacks()); //set the callback function
  
  BLEService *pService = pServer->createService(SERVICE_UUID);
  BLECharacteristic *pCharacteristic = pService->createCharacteristic(
                                         CHARACTERISTIC_UUID,
                                         BLECharacteristic::PROPERTY_READ |
                                         BLECharacteristic::PROPERTY_WRITE
                                       );

  pCharacteristic->setValue("Hello World says Neil");
  pService->start();
  // 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!");
}

void loop() {
  // put your main code here, to run repeatedly:
  delay(2000);
}

Upvotes: 5

Related Questions