0

I am using two esp32, one configured as server and the other as client,the server takes about 3 seconds to detect disconnection of client (when it’s out of range or turned off), while client takes 6 seconds to detect disconnection of server, how do I set the supervision timeout so that Both sever and client detect disconnection after only 1 sec

Here’s the server code :


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


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

bool deviceConnected = false;

//Setup callbacks onConnect and onDisconnect
class MyServerCallbacks: public BLEServerCallbacks {
  void onConnect(BLEServer* pServer) {
    deviceConnected = true;
  };
  void onDisconnect(BLEServer* pServer) { // this method takes 6 sec when I’m using two esps (when esp client disconnects from server ) but it’s immediate when my phone disconnects from server 

    deviceConnected = false;
    Serial.println("Client has disconnected");
    Serial.println("readvertising");
    BLEDevice::getAdvertising()->start();  // start advertising after disconnect
  }
};
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:
  if(deviceConnected){
//code executed only when client is connected 
}

}

I’ve been told to use this function


void BLEServer::updateConnParams(esp_bd_addr_t remote_bda, uint16_t minInterval, uint16_t maxInterval, uint16_t latency, uint16_t timeout)
{
    esp_ble_conn_update_params_t conn_params;
    memcpy(conn_params.bda, remote_bda, sizeof(esp_bd_addr_t));
    conn_params.min_int = minInterval;
    conn_params.max_int = maxInterval;
    conn_params.latency = latency;
    conn_params.timeout = timeout;
    
esp_ble_gap_update_conn_params(&conn_params);
}

When I do I get this error


multiple definition of `BLEServer::updateConnParams(unsigned char*, unsigned short, unsigned short, unsigned short, unsigned short)'

BLE_Server.ino:31: first defined here

Vass
  • 1
  • 2
  • Since you didn't post the code using it it's difficult to advise you on how to solve the problem, but what do you think "multiple definition" means? – romkey May 25 '22 at 23:38

1 Answers1

1

Is it possible you redeclared the function in your code? It’s already a part of the BLEServer library.

We miss a lot of code, but just call the BLEServer::updateConnParams after you made your connection and define the parameters you need. The min/max interval can be the same, or just use the interval which suites you, and make sure max interval * 1.25 ms * latency (max missed calls before connection would be defined as lost) is not greater than the timeout (value * 10 ms)

RemyHx
  • 325
  • 3
  • 9
  • Thank you so much, I’ll try that and see what happens, btw this is the server code, I didn’t add the client code cause the post would’ve been very long, does this function set the timeout for both server and client or only for server, cause server taking a few secs doesn’t really affect my work ,my biggest concern is the client, it has to detect disconnection immediately , also how do I add the client MAC address (xx:xx:xx:xx:xx:xx)to the remote bda variable which is of type uint8_t – Vass May 27 '22 at 01:21
  • Maybe the easiest solution is to make sure the client reads a characteristic every 500 ms or whatsoever. It will cost a lot of energy (and some processing time), but maybe that’s not a concern. All kinds of variants are possible, writing with a forced respond etc. – RemyHx May 27 '22 at 06:02
  • Your last question: take a peek here; https://github.com/espressif/arduino-esp32/issues/3801 – RemyHx May 27 '22 at 06:03
  • thank you for the explanation, it didnt work, the client still detetcs absence of server after 6 secs, maybe the onDisconnect is configured to detect after 6 sec ? i uploaded the client code to this old post i made : https://stackoverflow.com/questions/72333431/is-there-a-way-to-reduce-the-time-a-ble-client-takes-to-detect-disconnection-fro – Vass May 27 '22 at 11:52
  • Btw what I meant by "it didn’t work" is that i didn't achieve my goal with that function, but your guess was right the reason I was getting that error is because i put the bleserver::updateConnParams(….){} in the code when it’s already defined in the library, so what’s left to do now is making this function give me the result I’m looking for – Vass May 27 '22 at 13:14