-
Notifications
You must be signed in to change notification settings - Fork 7.6k
Disconnect all existing connections in multirole BLE mode (server and client at the same time) when only one device disconnected #3011
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Hi,
Could you add custom GAP and GATT handlers to see full logs from esp-idf library during disconnecting? |
Yes :) but it is wrong Disconnecting event. I am only disconnect smartphone application, but BLE buttons disconnected too.
#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"
// The remote service we wish to connect to.
//static BLEUUID serviceUUID("4fafc201-1fb5-459e-8fcc-c5c9c331914b");
static BLEUUID serviceUUID("a3aa");
static BLEUUID charUUID("d368");
// The characteristic of the remote service we are interested in.
//static BLEUUID charUUID("beb5483e-36e1-4688-b7f5-ea07361b26a8");
static boolean doConnect = false;
static BLERemoteCharacteristic* pRemoteCharacteristic;
static BLEAdvertisedDevice* myDevice;
uint8_t mac[6] = {0x00, 0x81, 0xF9, 0x60, 0xA3, 0x08};
BLECharacteristic *pCharacteristic;
class MyServerCallback: public BLEServerCallbacks {
void onConnect(BLEServer* pServer) {
Serial.println("MyServerCallback.onConnect");
};
void onDisconnect(BLEServer* pServer) {
Serial.println("MyServerCallback.onDisconnect");
}
};
class MyClientCallback : public BLEClientCallbacks {
void onConnect(BLEClient* pclient) {
Serial.println("MyClientCallback.onСonnect");
}
void onDisconnect(BLEClient* pclient) {
Serial.println("MyClientCallback.onDisconnect");
}
};
class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {
/**
* Called for each advertising BLE server.
*/
void onResult(BLEAdvertisedDevice advertisedDevice) {
Serial.print("BLE Advertised Device found: ");
Serial.println(advertisedDevice.toString().c_str());
// We have found a device, let us now see if it contains the service we are looking for.
uint8_t* addr = (uint8_t*)advertisedDevice.getAddress().getNative();
if (memcmp(addr, mac, 6) == 0) {
Serial.print("This is our device: ");
BLEDevice::getScan()->stop();
myDevice = new BLEAdvertisedDevice(advertisedDevice);
doConnect = true;
} // Found our server
} // onResult
}; // MyAdvertisedDeviceCallbacks
bool connectToServer() {
Serial.print("Forming a connection to ");
Serial.println(myDevice->getAddress().toString().c_str());
BLEClient* pClient = BLEDevice::createClient();
Serial.println(" - Created client");
pClient->setClientCallbacks(new MyClientCallback());
// Connect to the remove BLE Server.
pClient->connect(myDevice); // if you pass BLEAdvertisedDevice instead of address, it will be recognized type of peer device address (public or private)
Serial.println(" - Connected to server");
// Obtain a reference to the service we are after in the remote BLE server.
BLERemoteService* pRemoteService = pClient->getService(serviceUUID);
if (pRemoteService == nullptr) {
Serial.print("Failed to find our service UUID: ");
Serial.println(serviceUUID.toString().c_str());
pClient->disconnect();
return false;
}
Serial.println(" - Found our service");
// Obtain a reference to the characteristic in the service of the remote BLE server.
pRemoteCharacteristic = pRemoteService->getCharacteristic(charUUID);
if (pRemoteCharacteristic == nullptr) {
Serial.print("Failed to find our characteristic UUID: ");
Serial.println(charUUID.toString().c_str());
pClient->disconnect();
return false;
}
Serial.println(" - Found our characteristic");
// Read the value of the characteristic.
if(pRemoteCharacteristic->canWrite()) {
pRemoteCharacteristic->writeValue(0x01);
}
}
void setup() {
Serial.begin(115200);
Serial.println("Starting BLE work!");
BLEDevice::init("Long name works now");
BLEServer *pServer = BLEDevice::createServer();
pServer->setCallbacks(new MyServerCallback());
BLEService *pService = pServer->createService(SERVICE_UUID);
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!");
//=============================================
BLEScan* pBLEScan = BLEDevice::getScan();
pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
pBLEScan->setInterval(1349);
pBLEScan->setWindow(449);
pBLEScan->setActiveScan(true);
pBLEScan->start(5, false);
}
void loop() {
// put your main code here, to run repeatedly:
String str = "Hello World says Neil";
str += millis();
pCharacteristic->setValue(str.c_str());
if (doConnect == true) {
if (connectToServer()) {
Serial.println("We are now connected to the BLE Server.");
} else {
Serial.println("We have failed to connect to the server; there is nothin more we will do.");
}
doConnect = false;
}
delay(2000);
} Core debug level - DEBUG!!!!! Debug Messages:
Core debug level - ERROR!!!!!
BLEDevice::init("Long name works now");
BLEServer *pServer = BLEDevice::createServer();
//pServer->setCallbacks(new MyServerCallback()); <<----- comment setCallbacks log is different
But In all cases my behavior is the same
|
I have add custom GAP and GATT handlers to see full logs My testings steps is the same in my previous messages
|
This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 14 days if no further activity occurs. Thank you for your contributions. |
[STALE_DEL] This stale issue has been automatically closed. Thank you for your contributions. |
I have the same issue with the latest version. All BLE devices are disconnecting when one is loosing the connection (e.g. if I switch the device off or bringing it outside the range) |
Hardware:
Board: ESP32 Dev Module
Core Installation version: 1.0.3-rc1
IDE name: Arduino IDE
Flash Frequency: 80Mhz
Computer OS: Windows 10
Description:
I have combined two examples BLE_client and BLE_server in one sketch to have ability connect to several BLE devices and control my esp32 board via smartphone application. All works excellent. Smartphone can connect to esp32, read/write characteristics and get notifications. Also, esp32 can connect to several BLE devices (like BLE button) and read/write characteristics and get notifications. But if one device is disconnect (smartphone or only one BLE button) all existed connections was disconnected too (all BLE buttons and smartphone). I think it is wrong behavior. Only one connection should be break, all other should be continue keep.
Sketch: (leave the backquotes for code formatting)
Debug Messages:
The text was updated successfully, but these errors were encountered: