Page 1 of 1

i'm using two ble esp32,esp server takes 6sec to detect disconnection of esp client, how do i reduce this time to 1sec ?

Posted: Mon May 23, 2022 2:43 pm
by Vassimog
  1.  
  2. #include  <BLEDevice.h>
  3. #include  <BLEUtils.h>
  4. #include  <BLEServer.h>
  5.  
  6. #define  SERVICE_UUID        "4fafc201-1fb5-459e-8fcc-c5c9c331914b"
  7. #define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8"
  8.  
  9. bool deviceConnected = false;
  10.  
  11. //Setup callbacks onConnect and onDisconnect
  12. class MyServerCallbacks: public BLEServerCallbacks {
  13.  void onConnect(BLEServer* pServer) {
  14.     deviceConnected = true;
  15.   };
  16.   void onDisconnect(BLEServer* pServer) {
  17.     deviceConnected = false;
  18.     Serial.println("Client has disconnected");
  19.     Serial.println("readvertising");
  20.     BLEDevice::getAdvertising()->start();  // start advertising after disconnect
  21.   }
  22. };
  23.  
  24. void setup() {
  25.   Serial.begin(115200);
  26.   Serial.println("Starting BLE work!");
  27.  
  28.   BLEDevice::init("Long name works now");
  29.   BLEServer *pServer = BLEDevice::createServer();
  30.   BLEService *pService = pServer->createService(SERVICE_UUID);
  31.   BLECharacteristic *pCharacteristic = pService->createCharacteristic(
  32.                                          CHARACTERISTIC_UUID,
  33.                                          BLECharacteristic::PROPERTY_READ |
  34.                                          BLECharacteristic::PROPERTY_WRITE
  35.                                        );
  36.  
  37.   pCharacteristic->setValue("Hello World says Neil");
  38.   pService->start();
  39.   // BLEAdvertising *pAdvertising = pServer->getAdvertising();  // this still is working for backward compatibility
  40.   BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
  41.   pAdvertising->addServiceUUID(SERVICE_UUID);
  42.   pAdvertising->setScanResponse(true);
  43.   pAdvertising->setMinPreferred(0x06);  // functions that help with iPhone connections issue
  44.   pAdvertising->setMinPreferred(0x12);
  45.   BLEDevice::startAdvertising();
  46.   Serial.println"Characteristic defined! Now you can read it in your phone!");
  47. }
  48.  
  49. void loop() {
  50.   // put your main code here, to run repeatedly:
  51. if(deviceConnected){
  52. // code to be executed
  53. }
  54.  
  55. }