How to force client to disconnect

blekyo
Posts: 10
Joined: Tue Nov 14, 2017 2:15 am

How to force client to disconnect

Postby blekyo » Wed Dec 13, 2017 10:54 am

Hi,
I'm actually using "ESP32_BLE_Arduino" library of nkolban, which is working fine. I have my esp32 with my custom service and characteristic. in the other hand, an ios or android app who can connect to the esp32 via BLE and update the caracteristic.

The issue I have now is that if someone use an app like "lightBlue" or "nRF Connect" and stay connected to my esp32 BLE, I'm stuck, no one else can connect to the device. Which is some way what I want, but I would like to force a client to disconnect inside my esp32 if the client has been connected let's say more than 5 seconds.

For now, I'm trying to launch a timer when client is connected, and if the time limit is reached, I launch an ESP.restart();

So far now, my code does not work, and my esp32 just restart every seconds.

Any suggestions?
Thanks

blekyo
Posts: 10
Joined: Tue Nov 14, 2017 2:15 am

Re: How to force client to disconnect

Postby blekyo » Fri Dec 15, 2017 11:49 am

Ok so here is a piece of my code which is working properly. As you can see, once someone connected to my esp32, if he stay connected more than 10 seconds I display a message. All this process is working properly.

Code: Select all

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

uint32_t beginTimer=0;

BLEDevice ble;
BLEServer *pServer;
bool _BLEClientConnected = false;

#define BLE_NAME  "MYESP"
#define SERVICE_UUID        "488baf1a-9ef4-45b3-9da6-e0fc1ce33c8f"
#define CHARACTERISTIC_UUID "1775cf90-4eb4-433f-87b4-c05df29e120a"

class MyServerCallbacks : public BLEServerCallbacks {
  void onConnect(BLEServer* pServer) {
    _BLEClientConnected = true;
  };

  void onDisconnect(BLEServer* pServer) {
    _BLEClientConnected = false;
  }
};

class MyCharacteristicCallbacks: public BLECharacteristicCallbacks {
    void onWrite(BLECharacteristic *pCharacteristic) {
      std::string value = pCharacteristic->getValue();
      pCharacteristic->setValue("OK");
    }
};

void setup() {
  Serial.begin(115200);
 
  ble.init(BLE_NAME);
  pServer = new BLEServer();
  pServer->setCallbacks(new MyServerCallbacks());
 
  BLEService *pService = pServer->createService(SERVICE_UUID);

  BLECharacteristic *pCharacteristic = pService->createCharacteristic(
                                         CHARACTERISTIC_UUID,
                                         BLECharacteristic::PROPERTY_WRITE
                                       );
  pCharacteristic->setCallbacks(new MyCharacteristicCallbacks());
  pCharacteristic->setValue("KO");
  pService->start();

  BLEAdvertising *pAdvertising = pServer->getAdvertising();
  pAdvertising->start();
}

void loop() {
  if(_BLEClientConnected == true && beginTimer == 0){
    beginTimer = millis();
    Serial.println("Client connected timer started");
  }
  else if(_BLEClientConnected == true && beginTimer != 0 && (millis() - beginTimer) > 10000){
    Serial.println("Client connected for too long");
    _BLEClientConnected = false;
  }
  else if(_BLEClientConnected == false && beginTimer != 0){
    beginTimer = 0;
    Serial.println("Client disconnected timer reset");
  }
}

But once I add the disconnection functionality, my esp32 just keep restarting. Here is the code who's not working:
- I've added a function on BLEServer.cpp to add the disconnection feature (note that I've added the prototype on BLEServer.h)

Code: Select all

/**
 * @brief Disconnect Client
 *
 * Force a client to disconnect
 */
void BLEServer::disconnectClient() {
   ESP_LOGD(LOG_TAG, ">> disconnectClient()");
   esp_err_t errRc = ::esp_ble_gatts_close(getGattsIf(), getConnId());
   if (errRc != ESP_OK) {
      ESP_LOGE(LOG_TAG, "esp_ble_gatts_close: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
      return;
   }
   ESP_LOGD(LOG_TAG, "<< disconnectClient()");
} // disconnectClient


And on the main program just added the function on the loop:

Code: Select all

void loop() {
  if(_BLEClientConnected == true && beginTimer == 0){
    beginTimer = millis();
    Serial.println("Client connected timer started");
  }
  else if(_BLEClientConnected == true && beginTimer != 0 && (millis() - beginTimer) > 10000){
    Serial.println("Client connected for too long");
    _BLEClientConnected = false;
    pServer->disconnectClient() ;
  }
  else if(_BLEClientConnected == false && beginTimer != 0){
    beginTimer = 0;
    Serial.println("Client disconnected timer reset");
  }
}

My knowledge on C++ coding is really limited, so I might have done something silly. For now I can't make it work.

blekyo
Posts: 10
Joined: Tue Nov 14, 2017 2:15 am

Re: How to force client to disconnect

Postby blekyo » Thu Dec 21, 2017 1:59 am

My mistake here, the code is working fine. I was just out of date for the arduino-esp32 git repository :?

hml9083@hotmail.com
Posts: 9
Joined: Mon Nov 30, 2015 12:58 pm

Re: How to force client to disconnect

Postby hml9083@hotmail.com » Thu Jan 04, 2018 1:22 pm

Question, What is longest period of time your esp32's, wifi remains connected to network and responsive, days, weeks etc. ?

gwangchen@163.com
Posts: 1
Joined: Mon Feb 12, 2018 4:15 pm

Re: How to force client to disconnect

Postby gwangchen@163.com » Mon Feb 12, 2018 4:22 pm

Hi there

Im doing a project by using ESP32 BLE to create a data passing chain. and Im having the same problem now.
What did you put on BLEServer.cpp and BLEServer.h? Im kind of confused.
and where do you declear the pServer in a loop?

Can you help me out?

Thanks a lot

Who is online

Users browsing this forum: No registered users and 8 guests