From 437e651e0c55ccb89219185358f662024902c74c Mon Sep 17 00:00:00 2001 From: h2zero Date: Sun, 8 Mar 2026 17:08:13 -0600 Subject: [PATCH] Add NimBLEConnInfo::toString method. --- .../NimBLE_extended_server/main/main.cpp | 2 +- .../NimBLE_multi_advertiser/main/main.cpp | 2 +- examples/NimBLE_Server/main/main.cpp | 2 +- src/NimBLEConnInfo.h | 36 +++++++++++++++++++ 4 files changed, 39 insertions(+), 3 deletions(-) diff --git a/examples/Bluetooth_5/NimBLE_extended_server/main/main.cpp b/examples/Bluetooth_5/NimBLE_extended_server/main/main.cpp index 607cd02..a19b49e 100644 --- a/examples/Bluetooth_5/NimBLE_extended_server/main/main.cpp +++ b/examples/Bluetooth_5/NimBLE_extended_server/main/main.cpp @@ -36,7 +36,7 @@ static uint8_t secondaryPhy = BLE_HCI_LE_PHY_1M; /** Handler class for server events */ class ServerCallbacks : public NimBLEServerCallbacks { void onConnect(NimBLEServer* pServer, NimBLEConnInfo& connInfo) override { - printf("Client connected:: %s\n", connInfo.getAddress().toString().c_str()); + printf("Client connected:\n%s", connInfo.toString().c_str()); } void onDisconnect(NimBLEServer* pServer, NimBLEConnInfo& connInfo, int reason) override { diff --git a/examples/Bluetooth_5/NimBLE_multi_advertiser/main/main.cpp b/examples/Bluetooth_5/NimBLE_multi_advertiser/main/main.cpp index 2269ecb..3322e54 100644 --- a/examples/Bluetooth_5/NimBLE_multi_advertiser/main/main.cpp +++ b/examples/Bluetooth_5/NimBLE_multi_advertiser/main/main.cpp @@ -36,7 +36,7 @@ static uint8_t secondaryPhy = BLE_HCI_LE_PHY_1M; /** Handler class for server events */ class ServerCallbacks : public NimBLEServerCallbacks { void onConnect(NimBLEServer* pServer, NimBLEConnInfo& connInfo) override { - printf("Client connected: %s\n", connInfo.getAddress().toString().c_str()); + printf("Client connected:\n%s", connInfo.toString().c_str()); } void onDisconnect(NimBLEServer* pServer, NimBLEConnInfo& connInfo, int reason) override { diff --git a/examples/NimBLE_Server/main/main.cpp b/examples/NimBLE_Server/main/main.cpp index 3fe4322..7f8d265 100644 --- a/examples/NimBLE_Server/main/main.cpp +++ b/examples/NimBLE_Server/main/main.cpp @@ -16,7 +16,7 @@ static NimBLEServer* pServer; ** Remove as you see fit for your needs */ class ServerCallbacks : public NimBLEServerCallbacks { void onConnect(NimBLEServer* pServer, NimBLEConnInfo& connInfo) override { - printf("Client address: %s\n", connInfo.getAddress().toString().c_str()); + printf("Client connected:\n%s", connInfo.toString().c_str()); /** * We can use the connection handle here to ask for different connection parameters. diff --git a/src/NimBLEConnInfo.h b/src/NimBLEConnInfo.h index cd1e4c3..1c63cd8 100644 --- a/src/NimBLEConnInfo.h +++ b/src/NimBLEConnInfo.h @@ -25,6 +25,7 @@ #endif #include "NimBLEAddress.h" +#include /** * @brief Connection information. @@ -70,6 +71,41 @@ class NimBLEConnInfo { /** @brief Gets the key size used to encrypt the connection */ uint8_t getSecKeySize() const { return m_desc.sec_state.key_size; } + /** @brief Get a string representation of the connection info, useful for debugging */ + std::string toString() const { + std::string str; + // 294 chars max expected from all labels + worst-case values, round up to 300. + str.resize(300); + + snprintf(&str[0], + str.size(), + " Address: %s\n" + " ID Address: %s\n" + " Connection Handle: %u\n" + " Connection Interval: %.1f ms\n" + " Connection Timeout: %u ms\n" + " Connection Latency: %u\n" + " MTU: %u bytes\n" + " Role: %s\n" + " Bonded: %s\n" + " Encrypted: %s\n" + " Authenticated: %s\n" + " Security Key Size: %u\n", + getAddress().toString().c_str(), + getIdAddress().toString().c_str(), + getConnHandle(), + getConnInterval() * 1.25f, + getConnTimeout() * 10, + getConnLatency(), + getMTU(), + isMaster() ? "Master" : "Slave", + isBonded() ? "Yes" : "No", + isEncrypted() ? "Yes" : "No", + isAuthenticated() ? "Yes" : "No", + getSecKeySize()); + return str; + } + private: friend class NimBLEServer; friend class NimBLEClient;