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
How to force client to disconnect
Re: How to force client to disconnect
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.
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)
And on the main program just added the function on the loop:
My knowledge on C++ coding is really limited, so I might have done something silly. For now I can't make it work.
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");
}
}
- 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
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");
}
}
Re: How to force client to disconnect
My mistake here, the code is working fine. I was just out of date for the arduino-esp32 git repository
-
- Posts: 17
- Joined: Mon Nov 30, 2015 12:58 pm
Re: How to force client to disconnect
Question, What is longest period of time your esp32's, wifi remains connected to network and responsive, days, weeks etc. ?
-
- Posts: 1
- Joined: Mon Feb 12, 2018 4:15 pm
Re: How to force client to disconnect
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
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 140 guests