Skip to content

Commit 8576ae8

Browse files
committed
Add NimBLEConnInfo::toString method.
1 parent 4b606b0 commit 8576ae8

4 files changed

Lines changed: 39 additions & 3 deletions

File tree

examples/Bluetooth_5/NimBLE_extended_server/NimBLE_extended_server.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ static uint8_t secondaryPhy = BLE_HCI_LE_PHY_1M;
4343
/** Handler class for server events */
4444
class ServerCallbacks : public NimBLEServerCallbacks {
4545
void onConnect(NimBLEServer* pServer, NimBLEConnInfo& connInfo) override {
46-
Serial.printf("Client connected:: %s\n", connInfo.getAddress().toString().c_str());
46+
Serial.printf("Client connected:\n%s", connInfo.toString().c_str());
4747
}
4848

4949
void onDisconnect(NimBLEServer* pServer, NimBLEConnInfo& connInfo, int reason) override {

examples/Bluetooth_5/NimBLE_multi_advertiser/NimBLE_multi_advertiser.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ static uint8_t secondaryPhy = BLE_HCI_LE_PHY_1M;
4343
/** Handler class for server events */
4444
class ServerCallbacks : public NimBLEServerCallbacks {
4545
void onConnect(NimBLEServer* pServer, NimBLEConnInfo& connInfo) override {
46-
Serial.printf("Client connected: %s\n", connInfo.getAddress().toString().c_str());
46+
Serial.printf("Client connected:\n%s", connInfo.toString().c_str());
4747
}
4848

4949
void onDisconnect(NimBLEServer* pServer, NimBLEConnInfo& connInfo, int reason) override {

examples/NimBLE_Server/NimBLE_Server.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ static NimBLEServer* pServer;
1717
** Remove as you see fit for your needs */
1818
class ServerCallbacks : public NimBLEServerCallbacks {
1919
void onConnect(NimBLEServer* pServer, NimBLEConnInfo& connInfo) override {
20-
Serial.printf("Client address: %s\n", connInfo.getAddress().toString().c_str());
20+
Serial.printf("Client connected:\n%s", connInfo.toString().c_str());
2121

2222
/**
2323
* We can use the connection handle here to ask for different connection parameters.

src/NimBLEConnInfo.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#endif
2626

2727
#include "NimBLEAddress.h"
28+
#include <cstdio>
2829

2930
/**
3031
* @brief Connection information.
@@ -70,6 +71,41 @@ class NimBLEConnInfo {
7071
/** @brief Gets the key size used to encrypt the connection */
7172
uint8_t getSecKeySize() const { return m_desc.sec_state.key_size; }
7273

74+
/** @brief Get a string representation of the connection info, useful for debugging */
75+
std::string toString() const {
76+
std::string str;
77+
// 294 chars max expected from all labels + worst-case values, round up to 300.
78+
str.resize(300);
79+
80+
snprintf(&str[0],
81+
str.size(),
82+
" Address: %s\n"
83+
" ID Address: %s\n"
84+
" Connection Handle: %u\n"
85+
" Connection Interval: %.1f ms\n"
86+
" Connection Timeout: %u ms\n"
87+
" Connection Latency: %u\n"
88+
" MTU: %u bytes\n"
89+
" Role: %s\n"
90+
" Bonded: %s\n"
91+
" Encrypted: %s\n"
92+
" Authenticated: %s\n"
93+
" Security Key Size: %u\n",
94+
getAddress().toString().c_str(),
95+
getIdAddress().toString().c_str(),
96+
getConnHandle(),
97+
getConnInterval() * 1.25f,
98+
getConnTimeout() * 10,
99+
getConnLatency(),
100+
getMTU(),
101+
isMaster() ? "Master" : "Slave",
102+
isBonded() ? "Yes" : "No",
103+
isEncrypted() ? "Yes" : "No",
104+
isAuthenticated() ? "Yes" : "No",
105+
getSecKeySize());
106+
return str;
107+
}
108+
73109
private:
74110
friend class NimBLEServer;
75111
friend class NimBLEClient;

0 commit comments

Comments
 (0)