Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
276 changes: 276 additions & 0 deletions libraries/BLE/examples/Client_multiconnect/Client_multiconnect.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,276 @@
/**
* A BLE client example that connects to multiple BLE servers simultaneously.
*
* This example demonstrates how to:
* - Scan for multiple BLE servers
* - Connect to multiple servers at the same time
* - Interact with characteristics on different servers
* - Handle disconnections and reconnections
*
* The example looks for servers advertising the service UUID: 4fafc201-1fb5-459e-8fcc-c5c9c331914b
* and connects to up to MAX_SERVERS servers.
*
* Created by lucasssvaz
* Based on the original Client example by Neil Kolban and chegewara
*/

#include "BLEDevice.h"

// The remote service we wish to connect to.
static BLEUUID serviceUUID("4fafc201-1fb5-459e-8fcc-c5c9c331914b");
// The characteristic of the remote service we are interested in.
static BLEUUID charUUID("beb5483e-36e1-4688-b7f5-ea07361b26a8");

// Maximum number of servers to connect to
#define MAX_SERVERS 3

// Structure to hold information about each connected server
struct ServerConnection {
BLEClient *pClient;
BLEAdvertisedDevice *pDevice;
BLERemoteCharacteristic *pRemoteCharacteristic;
bool connected;
bool doConnect;
String name;
};

// Array to manage multiple server connections
ServerConnection servers[MAX_SERVERS];
int connectedServers = 0;
static bool doScan = true;

// Callback function to handle notifications from any server
static void notifyCallback(BLERemoteCharacteristic *pBLERemoteCharacteristic, uint8_t *pData, size_t length, bool isNotify) {
// Find which server this notification came from
for (int i = 0; i < MAX_SERVERS; i++) {
if (servers[i].connected && servers[i].pRemoteCharacteristic == pBLERemoteCharacteristic) {
Serial.print("Notify from server ");
Serial.print(servers[i].name);
Serial.print(" - Characteristic: ");
Serial.print(pBLERemoteCharacteristic->getUUID().toString().c_str());
Serial.print(" | Length: ");
Serial.print(length);
Serial.print(" | Data: ");
Serial.write(pData, length);
Serial.println();
break;
}
}
}

// Client callback class to handle connect/disconnect events
class MyClientCallback : public BLEClientCallbacks {
int serverIndex;

public:
MyClientCallback(int index) : serverIndex(index) {}

void onConnect(BLEClient *pclient) {
Serial.print("Connected to server ");
Serial.println(servers[serverIndex].name);
}

void onDisconnect(BLEClient *pclient) {
servers[serverIndex].connected = false;
connectedServers--;
Serial.print("Disconnected from server ");
Serial.print(servers[serverIndex].name);
Serial.print(" | Total connected: ");
Serial.println(connectedServers);
doScan = true; // Resume scanning to find replacement servers
}
};

// Function to connect to a specific server
bool connectToServer(int serverIndex) {
Serial.print("Connecting to server ");
Serial.print(serverIndex);
Serial.print(" at address: ");
Serial.println(servers[serverIndex].pDevice->getAddress().toString().c_str());

servers[serverIndex].pClient = BLEDevice::createClient();
Serial.println(" - Created client");

// Set the callback for this specific server connection
servers[serverIndex].pClient->setClientCallbacks(new MyClientCallback(serverIndex));

// Connect to the remote BLE Server
servers[serverIndex].pClient->connect(servers[serverIndex].pDevice);
Serial.println(" - Connected to server");
servers[serverIndex].pClient->setMTU(517); // Request maximum MTU from server

// Obtain a reference to the service we are after in the remote BLE server
BLERemoteService *pRemoteService = servers[serverIndex].pClient->getService(serviceUUID);
if (pRemoteService == nullptr) {
Serial.print("Failed to find service UUID: ");
Serial.println(serviceUUID.toString().c_str());
servers[serverIndex].pClient->disconnect();
return false;
}
Serial.println(" - Found service");

// Obtain a reference to the characteristic in the service
servers[serverIndex].pRemoteCharacteristic = pRemoteService->getCharacteristic(charUUID);
if (servers[serverIndex].pRemoteCharacteristic == nullptr) {
Serial.print("Failed to find characteristic UUID: ");
Serial.println(charUUID.toString().c_str());
servers[serverIndex].pClient->disconnect();
return false;
}
Serial.println(" - Found characteristic");

// Read the value of the characteristic
if (servers[serverIndex].pRemoteCharacteristic->canRead()) {
String value = servers[serverIndex].pRemoteCharacteristic->readValue();
Serial.print("Initial characteristic value: ");
Serial.println(value.c_str());
}

// Register for notifications if available
if (servers[serverIndex].pRemoteCharacteristic->canNotify()) {
servers[serverIndex].pRemoteCharacteristic->registerForNotify(notifyCallback);
Serial.println(" - Registered for notifications");
}

servers[serverIndex].connected = true;
connectedServers++;
Serial.print("Successfully connected! Total servers connected: ");
Serial.println(connectedServers);
return true;
}

// Scan callback class to find BLE servers
class MyAdvertisedDeviceCallbacks : public BLEAdvertisedDeviceCallbacks {
void onResult(BLEAdvertisedDevice advertisedDevice) {
Serial.print("BLE Device found: ");
Serial.println(advertisedDevice.toString().c_str());

// Check if this device has the service we're looking for
if (advertisedDevice.haveServiceUUID() && advertisedDevice.isAdvertisingService(serviceUUID)) {
Serial.println(" -> This device has our service!");

// Check if we already know about this device
String deviceAddress = advertisedDevice.getAddress().toString().c_str();
bool alreadyKnown = false;

for (int i = 0; i < MAX_SERVERS; i++) {
if (servers[i].pDevice != nullptr) {
if (servers[i].pDevice->getAddress().toString() == deviceAddress) {
alreadyKnown = true;
break;
}
}
}

if (alreadyKnown) {
Serial.println(" -> Already connected or connecting to this device");
return;
}

// Find an empty slot for this server
for (int i = 0; i < MAX_SERVERS; i++) {
if (servers[i].pDevice == nullptr || (!servers[i].connected && !servers[i].doConnect)) {
servers[i].pDevice = new BLEAdvertisedDevice(advertisedDevice);
servers[i].doConnect = true;
servers[i].name = "Server_" + String(i);
Serial.print(" -> Assigned to slot ");
Serial.println(i);

// If we've found enough servers, stop scanning
int pendingConnections = 0;
for (int j = 0; j < MAX_SERVERS; j++) {
if (servers[j].connected || servers[j].doConnect) {
pendingConnections++;
}
}
if (pendingConnections >= MAX_SERVERS) {
Serial.println("Found enough servers, stopping scan");
BLEDevice::getScan()->stop();
doScan = false;
}
break;
}
}
}
}
};

void setup() {
Serial.begin(115200);
Serial.println("=================================");
Serial.println("BLE Multi-Client Example");
Serial.println("=================================");
Serial.print("Max servers to connect: ");
Serial.println(MAX_SERVERS);
Serial.println();

// Initialize all server connections
for (int i = 0; i < MAX_SERVERS; i++) {
servers[i].pClient = nullptr;
servers[i].pDevice = nullptr;
servers[i].pRemoteCharacteristic = nullptr;
servers[i].connected = false;
servers[i].doConnect = false;
servers[i].name = "";
}

// Initialize BLE
BLEDevice::init("ESP32_MultiClient");

// Set up BLE scanner
BLEScan *pBLEScan = BLEDevice::getScan();
pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
pBLEScan->setInterval(1349);
pBLEScan->setWindow(449);
pBLEScan->setActiveScan(true);
pBLEScan->start(5, false);

Serial.println("Scanning for BLE servers...");
}

void loop() {
// Process any pending connections
for (int i = 0; i < MAX_SERVERS; i++) {
if (servers[i].doConnect) {
if (connectToServer(i)) {
Serial.println("Connection successful");
} else {
Serial.println("Connection failed");
// Clear this slot so we can try another server
delete servers[i].pDevice;
servers[i].pDevice = nullptr;
}
servers[i].doConnect = false;
}
}

// If we're connected to servers, send data to each one
if (connectedServers > 0) {
for (int i = 0; i < MAX_SERVERS; i++) {
if (servers[i].connected && servers[i].pRemoteCharacteristic != nullptr) {
// Create a unique message for each server
String newValue = servers[i].name + " | Time: " + String(millis() / 1000);

Serial.print("Sending to ");
Serial.print(servers[i].name);
Serial.print(": ");
Serial.println(newValue);

// Write the value to the characteristic
servers[i].pRemoteCharacteristic->writeValue(newValue.c_str(), newValue.length());
}
}
} else {
Serial.println("No servers connected");
}

// Resume scanning if we have room for more connections
if (doScan && connectedServers < MAX_SERVERS) {
Serial.println("Resuming scan for more servers...");
BLEDevice::getScan()->start(5, false);
doScan = false;
delay(5000); // Wait for scan to complete
}

delay(2000); // Delay between loop iterations
}
5 changes: 5 additions & 0 deletions libraries/BLE/examples/Client_multiconnect/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
fqbn_append: PartitionScheme=huge_app

requires_any:
- CONFIG_SOC_BLE_SUPPORTED=y
- CONFIG_ESP_HOSTED_ENABLE_BT_NIMBLE=y
33 changes: 30 additions & 3 deletions libraries/BLE/src/BLERemoteCharacteristic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,11 @@ bool BLERemoteCharacteristic::canWriteNoResponse() {
* @brief Retrieve the map of descriptors keyed by UUID.
*/
std::map<std::string, BLERemoteDescriptor *> *BLERemoteCharacteristic::getDescriptors() {
// Retrieve descriptors if not already done (lazy loading)
if (!m_descriptorsRetrieved) {
log_d("Descriptors not yet retrieved, retrieving now...");
retrieveDescriptors();
}
return &m_descriptorMap;
} // getDescriptors

Expand All @@ -132,6 +137,11 @@ uint16_t BLERemoteCharacteristic::getHandle() {
*/
BLERemoteDescriptor *BLERemoteCharacteristic::getDescriptor(BLEUUID uuid) {
log_v(">> getDescriptor: uuid: %s", uuid.toString().c_str());
// Retrieve descriptors if not already done (lazy loading)
if (!m_descriptorsRetrieved) {
log_d("Descriptors not yet retrieved, retrieving now...");
retrieveDescriptors();
}
std::string v = uuid.toString().c_str();
for (auto &myPair : m_descriptorMap) {
if (myPair.first == v) {
Expand Down Expand Up @@ -287,6 +297,7 @@ void BLERemoteCharacteristic::removeDescriptors() {
delete myPair.second;
}
m_descriptorMap.clear();
m_descriptorsRetrieved = false; // Allow descriptors to be retrieved again
} // removeCharacteristics

/**
Expand Down Expand Up @@ -366,6 +377,7 @@ BLERemoteCharacteristic::BLERemoteCharacteristic(uint16_t handle, BLEUUID uuid,
m_notifyCallback = nullptr;
m_rawData = nullptr;
m_auth = ESP_GATT_AUTH_REQ_NONE;
m_descriptorsRetrieved = false;

retrieveDescriptors(); // Get the descriptors for this characteristic
log_v("<< BLERemoteCharacteristic");
Expand Down Expand Up @@ -549,6 +561,7 @@ void BLERemoteCharacteristic::retrieveDescriptors() {
offset++;
} // while true
//m_haveCharacteristics = true; // Remember that we have received the characteristics.
m_descriptorsRetrieved = true;
log_v("<< retrieveDescriptors(): Found %d descriptors.", offset);
} // getDescriptors

Expand Down Expand Up @@ -663,14 +676,15 @@ BLERemoteCharacteristic::BLERemoteCharacteristic(BLERemoteService *pRemoteServic

m_handle = chr->val_handle;
m_defHandle = chr->def_handle;
m_endHandle = 0;
m_charProp = chr->properties;
m_pRemoteService = pRemoteService;
m_notifyCallback = nullptr;
m_rawData = nullptr;
m_auth = 0;
m_descriptorsRetrieved = false;

retrieveDescriptors(); // Get the descriptors for this characteristic
// Don't retrieve descriptors in constructor for NimBLE to avoid deadlock
// Descriptors will be retrieved on-demand when needed (e.g., for notifications)

log_v("<< BLERemoteCharacteristic(): %s", m_uuid.toString().c_str());
} // BLERemoteCharacteristic
Expand Down Expand Up @@ -781,6 +795,7 @@ bool BLERemoteCharacteristic::retrieveDescriptors(const BLEUUID *uuid_filter) {

// If this is the last handle then there are no descriptors
if (m_handle == getRemoteService()->getEndHandle()) {
m_descriptorsRetrieved = true;
log_d("<< retrieveDescriptors(): No descriptors found");
return true;
}
Expand All @@ -789,7 +804,9 @@ bool BLERemoteCharacteristic::retrieveDescriptors(const BLEUUID *uuid_filter) {
desc_filter_t filter = {uuid_filter, &taskData};
int rc = 0;

rc = ble_gattc_disc_all_dscs(getRemoteService()->getClient()->getConnId(), m_handle, m_endHandle, BLERemoteCharacteristic::descriptorDiscCB, &filter);
rc = ble_gattc_disc_all_dscs(
getRemoteService()->getClient()->getConnId(), m_handle, getRemoteService()->getEndHandle(), BLERemoteCharacteristic::descriptorDiscCB, &filter
);

if (rc != 0) {
log_e("ble_gattc_disc_all_dscs: rc=%d %s", rc, BLEUtils::returnCodeToString(rc));
Expand All @@ -806,6 +823,7 @@ bool BLERemoteCharacteristic::retrieveDescriptors(const BLEUUID *uuid_filter) {
return false;
}

m_descriptorsRetrieved = true;
log_d("<< retrieveDescriptors(): Found %d descriptors.", m_descriptorMap.size() - prevDscCount);
return true;
} // retrieveDescriptors
Expand Down Expand Up @@ -966,6 +984,15 @@ bool BLERemoteCharacteristic::setNotify(uint16_t val, notify_callback notifyCall

m_notifyCallback = notifyCallback;

// Retrieve descriptors if not already done (lazy loading)
if (!m_descriptorsRetrieved) {
log_d("Descriptors not yet retrieved, retrieving now...");
if (!retrieveDescriptors()) {
log_e("<< setNotify(): Failed to retrieve descriptors");
return false;
}
}

BLERemoteDescriptor *desc = getDescriptor(BLEUUID((uint16_t)0x2902));
if (desc == nullptr) {
log_w("<< setNotify(): Callback set, CCCD not found");
Expand Down
Loading
Loading