0

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

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.