user80781: 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());




user4143: (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.

80781: <p>I am implementing a BLE peripheral in my firmware with Arduino ESP32.</p>
<p>When the central gets connected to it, I'd like to log the name of the central &amp; its address.</p>
<p>How can I do that?</p>
<pre class="lang-cpp prettyprint-override"><code>#include &lt;BLEServer.h&gt;



class ServerCallbacks: public BLEServerCallbacks {
    void onConnect(BLEServer* pServer) {
        ble.log(&quot;ServerCallbacks: Connected&quot;);

// ==&gt; 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 = &quot;BBWRR&quot;;        
        ledIndicator.showAccessCode(code);
        #endif


        #if SUPPORT_BUZZER
        /// BLE connection jingle
        buzzer.playBleConnectedMelody();
        #endif
    };
 
    void onDisconnect(BLEServer* pServer) {
        ble.deviceConnected = false;
        ble.log(&quot;ServerCallbacks: Disconnected&quot;);

        #if SUPPORT_BUZZER
        /// BLE connection jingle
        buzzer.playBleDisconnectedMelody();
        #endif
    }
};


/// Somewhere in the setup:
server = BLEDevice::createServer();
    
// == Set up our calbacks
server-&gt;setCallbacks(new ServerCallbacks());


</code></pre>
 4143: <p>(Note: this is all just from reading the library source code, nothing has been tested).</p>
<p>You can't get the details of the just-connected-client. However you <em>can</em> get a list of the currently connected clients.</p>
<p>There is a function in the <code>BLEServer</code> class:</p>
<pre><code>std::map&lt;uint16_t, conn_status_t&gt; getPeerDevices(bool client);
</code></pre>
<p>You can use that to get the list of connected clients. The &quot;key&quot; of the map is the connection id, and the &quot;value&quot; of the map is the &quot;conn_status_t&quot; struct, which includes a pointer to the peer device structure:</p>
<pre class="lang-cpp prettyprint-override"><code>typedef struct {
    void *peer_device;
    bool connected;
    uint16_t mtu;
} conn_status_t;
</code></pre>
<p>You can keep your own list of &quot;current&quot; connection IDs and compare that to the &quot;live&quot; list when a new connection is made to find which is the new connection.</p>
<p>So you can take that <code>peer_device</code> pointer, cast it to a <code>BLEClient</code> pointer, then you call any of the normal BLEClient functions on that, including <code>toString()</code> which will return a string representation of the client.</p>
<p>That of course isn't the &quot;name&quot; 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 <code>BLEClient</code> object pointer.</p>
