BLE connect only one time - ESP32

SANKARI
Posts: 1
Joined: Thu Apr 07, 2022 11:53 am

BLE connect only one time - ESP32

Postby SANKARI » Thu Apr 07, 2022 12:45 pm

Hi all,
I am using ESP WROOM 32 module. Trying to communicate with nearby android devices through BLE.

* With the ESP32 board is running, I go to the BLE application on my phone and scan and connect to the ESP32.
* Received the data from ESP 32 to android device.
* I then exit from my BLE application.
* If at this point I open the BLE app and try to scan the ESP 32 BLE server device again, the ESP 32 server device is not showing in the list of the scanning devices.
* The only way to trigger the whole thing again is by uploading the BLE server code again.

Below is the code snippet,

Based on Neil Kolban example for IDF: https://github.com/nkolban/esp32-snippe ... Server.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"

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

BLEDevice::init("Long name works now");
BLEServer *pServer = BLEDevice::createServer();
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);
}





Thanks in advance,
Regards,
sankari.

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

Re: BLE connect only one time - ESP32

Postby chegewara » Mon Apr 11, 2022 10:16 am

You have to start advertising again, either after connect or after disconnect, because during connecting advertising is stopped.

Who is online

Users browsing this forum: No registered users and 53 guests