I am implementing a BLE peripheral in my firmware with Arduino ESP32.
When the central gets connected to it, I'd like to log the name of the central & its address.
How can I do that?
#include <BLEServer.h>
class ServerCallbacks: public BLEServerCallbacks {
void onConnect(BLEServer* pServer) {
ble.log("ServerCallbacks: Connected");
// ==> how can I add the connected device ID or name in the log?
ble.deviceConnected = true;
#if SUPPORT_LEDS_INDICATOR
// === Show France flag colors
// RGBW = 1,2,3, 4
// CODE : BBWRR
// CODE = 33411
const String code = "BBWRR";
ledIndicator.showAccessCode(code);
#endif
#if SUPPORT_BUZZER
/// BLE connection jingle
buzzer.playBleConnectedMelody();
#endif
};
void onDisconnect(BLEServer* pServer) {
ble.deviceConnected = false;
ble.log("ServerCallbacks: Disconnected");
#if SUPPORT_BUZZER
/// BLE connection jingle
buzzer.playBleDisconnectedMelody();
#endif
}
};
/// Somewhere in the setup:
server = BLEDevice::createServer();
// == Set up our calbacks
server->setCallbacks(new ServerCallbacks());
1 Answer 1
(Note: this is all just from reading the library source code, nothing has been tested).
You can't get the details of the just-connected-client. However you can get a list of the currently connected clients.
There is a function in the BLEServer
class:
std::map<uint16_t, conn_status_t> getPeerDevices(bool client);
You can use that to get the list of connected clients. The "key" of the map is the connection id, and the "value" of the map is the "conn_status_t" struct, which includes a pointer to the peer device structure:
typedef struct {
void *peer_device;
bool connected;
uint16_t mtu;
} conn_status_t;
You can keep your own list of "current" connection IDs and compare that to the "live" list when a new connection is made to find which is the new connection.
So you can take that peer_device
pointer, cast it to a BLEClient
pointer, then you call any of the normal BLEClient functions on that, including toString()
which will return a string representation of the client.
That of course isn't the "name" of the client. I am not sure (not being an expert on BLE) how you get the name, but I think you may have to actively query it from the client, which should be perfectly possible now you have the BLEClient
object pointer.
-
Sorry, but this does not look right to me. (Or it may have been changed after you posted the answer.) The
peer_device
points to theBLEserver
object itself. So, we cannot just cast it toBLEClient
See here: github.com/espressif/arduino-esp32/blob/master/libraries/BLE/…anishsane– anishsane07/15/2025 16:21:55Commented Jul 15 at 16:21