From 0ba418092642396f7c60d62fce53d303d078fc24 Mon Sep 17 00:00:00 2001 From: Tomas Pilny Date: Tue, 31 Jan 2023 14:10:44 +0100 Subject: [PATCH 1/7] Added methods + example to retrive local MAC for BT --- .../examples/GetLocalMAC/GetLocalMAC.ino | 46 +++++++++++++++++++ libraries/BluetoothSerial/src/BTAddress.cpp | 18 +++++--- libraries/BluetoothSerial/src/BTAddress.h | 2 +- .../BluetoothSerial/src/BluetoothSerial.cpp | 16 ++++++- .../BluetoothSerial/src/BluetoothSerial.h | 3 ++ 5 files changed, 76 insertions(+), 9 deletions(-) create mode 100644 libraries/BluetoothSerial/examples/GetLocalMAC/GetLocalMAC.ino diff --git a/libraries/BluetoothSerial/examples/GetLocalMAC/GetLocalMAC.ino b/libraries/BluetoothSerial/examples/GetLocalMAC/GetLocalMAC.ino new file mode 100644 index 00000000000..69f219e1b62 --- /dev/null +++ b/libraries/BluetoothSerial/examples/GetLocalMAC/GetLocalMAC.ino @@ -0,0 +1,46 @@ +// This example demonstrates usage of BluetoothSerial method to retrieve MAC address of local BT device in various formats. +// By Tomas Pilny - 2023 + +#include "BluetoothSerial.h" + +String device_name = "ESP32-example"; + +#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED) +#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it +#endif + +#if !defined(CONFIG_BT_SPP_ENABLED) +#error Serial Bluetooth not available or not enabled. It is only available for the ESP32 chip. +#endif + +BluetoothSerial SerialBT; + +void setup() { + Serial.begin(115200); + SerialBT.begin(device_name); //Bluetooth device name + + uint8_t mac_arr[6]; // Byte array to hold the MAC address from getBtAddress() + BTAddress mac_obj; // Object holding instance of BTAddress with the MAC (for more details see libraries/BluetoothSerial/src/BTAddress.h) + std::string mac_str; // String holding the text version of MAC in format AA:BB:CC:DD:EE:FF + + SerialBT.getBtAddress(mac_arr); // Fill in the array + mac_obj = SerialBT.getBtAddressObject(); // Instantiate the object + mac_str = SerialBT.getBtAddressString(); // Copy the string + + Serial.print("This device is instantiated with name "); Serial.println(device_name); + + Serial.print("The mac address using byte array: "); + for(int i = 0; i < ESP_BD_ADDR_LEN-1; i++){ + Serial.print(mac_arr[i], HEX); Serial.print(":"); + } + Serial.println(mac_arr[ESP_BD_ADDR_LEN-1], HEX); + + Serial.print("The mac address using BTAddress object using default method `toString()`: "); Serial.println(mac_obj.toString().c_str()); + Serial.print("The mac address using BTAddress object using method `toString(true)`\n\twhich prints the MAC with capital letters: "); Serial.println(mac_obj.toString(true).c_str()); // This actually what is used inside the getBtAddressString() + + Serial.print("The mac address using string: "); Serial.println(mac_str.c_str()); +} + +void loop(){ + +} diff --git a/libraries/BluetoothSerial/src/BTAddress.cpp b/libraries/BluetoothSerial/src/BTAddress.cpp index 72fe7587b72..db1d10e678c 100644 --- a/libraries/BluetoothSerial/src/BTAddress.cpp +++ b/libraries/BluetoothSerial/src/BTAddress.cpp @@ -86,19 +86,25 @@ esp_bd_addr_t *BTAddress::getNative() const { /** * @brief Convert a BT address to a string. - * - * A string representation of an address is in the format: - * + * @param [in] capital changes the letter size + * By default the parameter `capital` == false and the string representation of an address is in the format: * ``` * xx:xx:xx:xx:xx:xx * ``` - * + * When the parameter `caputal` == true the format uses capital letters: + * ``` + * XX:XX:XX:XX:XX:XX + * ``` * @return The string representation of the address. */ -std::string BTAddress::toString() const { +std::string BTAddress::toString(bool capital) const { auto size = 18; char *res = (char*)malloc(size); - snprintf(res, size, "%02x:%02x:%02x:%02x:%02x:%02x", m_address[0], m_address[1], m_address[2], m_address[3], m_address[4], m_address[5]); + if(capital){ + snprintf(res, size, "%02X:%02X:%02X:%02X:%02X:%02X", m_address[0], m_address[1], m_address[2], m_address[3], m_address[4], m_address[5]); + }else{ + snprintf(res, size, "%02x:%02x:%02x:%02x:%02x:%02x", m_address[0], m_address[1], m_address[2], m_address[3], m_address[4], m_address[5]); + } std::string ret(res); free(res); return ret; diff --git a/libraries/BluetoothSerial/src/BTAddress.h b/libraries/BluetoothSerial/src/BTAddress.h index 3e51d053ebb..54b92349fda 100644 --- a/libraries/BluetoothSerial/src/BTAddress.h +++ b/libraries/BluetoothSerial/src/BTAddress.h @@ -29,7 +29,7 @@ class BTAddress { operator bool () const; esp_bd_addr_t* getNative() const; - std::string toString() const; + std::string toString(bool capital = false) const; private: esp_bd_addr_t m_address; diff --git a/libraries/BluetoothSerial/src/BluetoothSerial.cpp b/libraries/BluetoothSerial/src/BluetoothSerial.cpp index 800ef97e68c..42a0fcd3e62 100644 --- a/libraries/BluetoothSerial/src/BluetoothSerial.cpp +++ b/libraries/BluetoothSerial/src/BluetoothSerial.cpp @@ -661,8 +661,6 @@ static bool _init_bt(const char *deviceName) } } - // Why only master need this? Slave need this during pairing as well -// if (_isMaster && esp_bt_gap_register_callback(esp_bt_gap_cb) != ESP_OK) { if (esp_bt_gap_register_callback(esp_bt_gap_cb) != ESP_OK) { log_e("gap register failed"); return false; @@ -1183,4 +1181,18 @@ std::map BluetoothSerial::getChannels(const BTAddress &remoteA return sdpRecords; } +void BluetoothSerial::getBtAddress(uint8_t *mac){ + const uint8_t *dev_mac = esp_bt_dev_get_address(); + memcpy(mac, dev_mac, ESP_BD_ADDR_LEN); +} + +BTAddress BluetoothSerial::getBtAddressObject(){ + uint8_t mac_arr[ESP_BD_ADDR_LEN]; + getBtAddress(mac_arr); + return BTAddress(mac_arr); +} + +std::string BluetoothSerial::getBtAddressString(){ + return getBtAddressObject().toString(true); +} #endif diff --git a/libraries/BluetoothSerial/src/BluetoothSerial.h b/libraries/BluetoothSerial/src/BluetoothSerial.h index 4b225239333..d3c7d374d10 100644 --- a/libraries/BluetoothSerial/src/BluetoothSerial.h +++ b/libraries/BluetoothSerial/src/BluetoothSerial.h @@ -85,6 +85,9 @@ class BluetoothSerial: public Stream const int MAX_INQ_TIME = (ESP_BT_GAP_MAX_INQ_LEN * INQ_TIME); operator bool() const; + void getBtAddress(uint8_t *mac); + BTAddress getBtAddressObject(); + std::string getBtAddressString(); private: String local_name; int timeoutTicks=0; From c6f86584d6e510a933182305bb77569ebba08af1 Mon Sep 17 00:00:00 2001 From: Tomas Pilny Date: Tue, 31 Jan 2023 16:01:27 +0100 Subject: [PATCH 2/7] Added .skip files in the new example folder --- libraries/BluetoothSerial/examples/GetLocalMAC/.skip.esp32c3 | 0 libraries/BluetoothSerial/examples/GetLocalMAC/.skip.esp32s2 | 0 libraries/BluetoothSerial/examples/GetLocalMAC/.skip.esp32s3 | 0 3 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 libraries/BluetoothSerial/examples/GetLocalMAC/.skip.esp32c3 create mode 100644 libraries/BluetoothSerial/examples/GetLocalMAC/.skip.esp32s2 create mode 100644 libraries/BluetoothSerial/examples/GetLocalMAC/.skip.esp32s3 diff --git a/libraries/BluetoothSerial/examples/GetLocalMAC/.skip.esp32c3 b/libraries/BluetoothSerial/examples/GetLocalMAC/.skip.esp32c3 new file mode 100644 index 00000000000..e69de29bb2d diff --git a/libraries/BluetoothSerial/examples/GetLocalMAC/.skip.esp32s2 b/libraries/BluetoothSerial/examples/GetLocalMAC/.skip.esp32s2 new file mode 100644 index 00000000000..e69de29bb2d diff --git a/libraries/BluetoothSerial/examples/GetLocalMAC/.skip.esp32s3 b/libraries/BluetoothSerial/examples/GetLocalMAC/.skip.esp32s3 new file mode 100644 index 00000000000..e69de29bb2d From a9d121947997186a2df0be884283c5f26f854b54 Mon Sep 17 00:00:00 2001 From: Tomas Pilny Date: Wed, 1 Feb 2023 11:09:12 +0100 Subject: [PATCH 3/7] Fixed typos and formatting + added doxygen comments --- libraries/BluetoothSerial/src/BTAddress.cpp | 12 +++---- .../BluetoothSerial/src/BluetoothSerial.cpp | 35 +++++++++++++------ 2 files changed, 30 insertions(+), 17 deletions(-) diff --git a/libraries/BluetoothSerial/src/BTAddress.cpp b/libraries/BluetoothSerial/src/BTAddress.cpp index db1d10e678c..dbc13cc485e 100644 --- a/libraries/BluetoothSerial/src/BTAddress.cpp +++ b/libraries/BluetoothSerial/src/BTAddress.cpp @@ -91,7 +91,7 @@ esp_bd_addr_t *BTAddress::getNative() const { * ``` * xx:xx:xx:xx:xx:xx * ``` - * When the parameter `caputal` == true the format uses capital letters: + * When the parameter `capital` == true the format uses capital letters: * ``` * XX:XX:XX:XX:XX:XX * ``` @@ -100,11 +100,11 @@ esp_bd_addr_t *BTAddress::getNative() const { std::string BTAddress::toString(bool capital) const { auto size = 18; char *res = (char*)malloc(size); - if(capital){ - snprintf(res, size, "%02X:%02X:%02X:%02X:%02X:%02X", m_address[0], m_address[1], m_address[2], m_address[3], m_address[4], m_address[5]); - }else{ - snprintf(res, size, "%02x:%02x:%02x:%02x:%02x:%02x", m_address[0], m_address[1], m_address[2], m_address[3], m_address[4], m_address[5]); - } + if(capital){ + snprintf(res, size, "%02X:%02X:%02X:%02X:%02X:%02X", m_address[0], m_address[1], m_address[2], m_address[3], m_address[4], m_address[5]); + }else{ + snprintf(res, size, "%02x:%02x:%02x:%02x:%02x:%02x", m_address[0], m_address[1], m_address[2], m_address[3], m_address[4], m_address[5]); + } std::string ret(res); free(res); return ret; diff --git a/libraries/BluetoothSerial/src/BluetoothSerial.cpp b/libraries/BluetoothSerial/src/BluetoothSerial.cpp index 42a0fcd3e62..db5f9699bb9 100644 --- a/libraries/BluetoothSerial/src/BluetoothSerial.cpp +++ b/libraries/BluetoothSerial/src/BluetoothSerial.cpp @@ -1181,18 +1181,31 @@ std::map BluetoothSerial::getChannels(const BTAddress &remoteA return sdpRecords; } -void BluetoothSerial::getBtAddress(uint8_t *mac){ - const uint8_t *dev_mac = esp_bt_dev_get_address(); - memcpy(mac, dev_mac, ESP_BD_ADDR_LEN); +/** + * @brief Gets the MAC address of local BT device in byte array. + * + * @param mac [out] The mac + */ +void BluetoothSerial::getBtAddress(uint8_t *mac) { + const uint8_t *dev_mac = esp_bt_dev_get_address(); + memcpy(mac, dev_mac, ESP_BD_ADDR_LEN); } - -BTAddress BluetoothSerial::getBtAddressObject(){ - uint8_t mac_arr[ESP_BD_ADDR_LEN]; - getBtAddress(mac_arr); - return BTAddress(mac_arr); +/** + * @brief Gets the MAC address of local BT device as BTAddress object. + * + * @return The BTAddress object. + */ +BTAddress BluetoothSerial::getBtAddressObject() { + uint8_t mac_arr[ESP_BD_ADDR_LEN]; + getBtAddress(mac_arr); + return BTAddress(mac_arr); } - -std::string BluetoothSerial::getBtAddressString(){ - return getBtAddressObject().toString(true); +/** + * @brief Gets the MAC address of local BT device as string. + * + * @return The BT MAC address string. + */ +std::string BluetoothSerial::getBtAddressString() { + return getBtAddressObject().toString(true); } #endif From 51c34aaa4354c79d7a09ab82a75aea2004f98450 Mon Sep 17 00:00:00 2001 From: Tomas Pilny Date: Wed, 8 Feb 2023 14:08:53 +0100 Subject: [PATCH 4/7] changed std::string to String --- libraries/BluetoothSerial/src/BTAddress.cpp | 4 ++-- libraries/BluetoothSerial/src/BTAddress.h | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/libraries/BluetoothSerial/src/BTAddress.cpp b/libraries/BluetoothSerial/src/BTAddress.cpp index dbc13cc485e..1ca9ef5eab3 100644 --- a/libraries/BluetoothSerial/src/BTAddress.cpp +++ b/libraries/BluetoothSerial/src/BTAddress.cpp @@ -44,7 +44,7 @@ BTAddress::BTAddress() { * * @param [in] stringAddress The hex representation of the address. */ -BTAddress::BTAddress(std::string stringAddress) { +BTAddress::BTAddress(String stringAddress) { if (stringAddress.length() != 17) return; int data[6]; @@ -97,7 +97,7 @@ esp_bd_addr_t *BTAddress::getNative() const { * ``` * @return The string representation of the address. */ -std::string BTAddress::toString(bool capital) const { +String BTAddress::toString(bool capital) const { auto size = 18; char *res = (char*)malloc(size); if(capital){ diff --git a/libraries/BluetoothSerial/src/BTAddress.h b/libraries/BluetoothSerial/src/BTAddress.h index 54b92349fda..a4a15fbfe12 100644 --- a/libraries/BluetoothSerial/src/BTAddress.h +++ b/libraries/BluetoothSerial/src/BTAddress.h @@ -24,12 +24,12 @@ class BTAddress { public: BTAddress(); BTAddress(esp_bd_addr_t address); - BTAddress(std::string stringAddress); + BTAddress(String stringAddress); bool equals(BTAddress otherAddress); operator bool () const; esp_bd_addr_t* getNative() const; - std::string toString(bool capital = false) const; + String toString(bool capital = false) const; private: esp_bd_addr_t m_address; From 7268d13c74a7e2a17e0e985905bf803074234fb7 Mon Sep 17 00:00:00 2001 From: Tomas Pilny Date: Wed, 8 Feb 2023 14:10:24 +0100 Subject: [PATCH 5/7] another std::string -> String --- libraries/BluetoothSerial/src/BluetoothSerial.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/BluetoothSerial/src/BluetoothSerial.cpp b/libraries/BluetoothSerial/src/BluetoothSerial.cpp index db5f9699bb9..5f7e67b7ac9 100644 --- a/libraries/BluetoothSerial/src/BluetoothSerial.cpp +++ b/libraries/BluetoothSerial/src/BluetoothSerial.cpp @@ -1205,7 +1205,7 @@ BTAddress BluetoothSerial::getBtAddressObject() { * * @return The BT MAC address string. */ -std::string BluetoothSerial::getBtAddressString() { +String BluetoothSerial::getBtAddressString() { return getBtAddressObject().toString(true); } #endif From f186d6763b7062cc383a9415adaadb30dcc1fe56 Mon Sep 17 00:00:00 2001 From: Tomas Pilny Date: Sat, 11 Feb 2023 09:14:39 +0100 Subject: [PATCH 6/7] Changed std::string to String --- libraries/BluetoothSerial/src/BTAddress.cpp | 2 +- libraries/BluetoothSerial/src/BTAddress.h | 2 +- libraries/BluetoothSerial/src/BTAdvertisedDeviceSet.cpp | 2 +- libraries/BluetoothSerial/src/BTScanResultsSet.cpp | 2 +- libraries/BluetoothSerial/src/BluetoothSerial.h | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/libraries/BluetoothSerial/src/BTAddress.cpp b/libraries/BluetoothSerial/src/BTAddress.cpp index 1ca9ef5eab3..2cde9edf4e8 100644 --- a/libraries/BluetoothSerial/src/BTAddress.cpp +++ b/libraries/BluetoothSerial/src/BTAddress.cpp @@ -105,7 +105,7 @@ String BTAddress::toString(bool capital) const { }else{ snprintf(res, size, "%02x:%02x:%02x:%02x:%02x:%02x", m_address[0], m_address[1], m_address[2], m_address[3], m_address[4], m_address[5]); } - std::string ret(res); + String ret(res); free(res); return ret; } // toString diff --git a/libraries/BluetoothSerial/src/BTAddress.h b/libraries/BluetoothSerial/src/BTAddress.h index a4a15fbfe12..a173a296fe0 100644 --- a/libraries/BluetoothSerial/src/BTAddress.h +++ b/libraries/BluetoothSerial/src/BTAddress.h @@ -12,7 +12,7 @@ #include "sdkconfig.h" #if defined(CONFIG_BT_ENABLED) && defined(CONFIG_BLUEDROID_ENABLED) #include // ESP32 BT -#include +#include /** diff --git a/libraries/BluetoothSerial/src/BTAdvertisedDeviceSet.cpp b/libraries/BluetoothSerial/src/BTAdvertisedDeviceSet.cpp index 8a9e26e4d5a..14bf01fe815 100644 --- a/libraries/BluetoothSerial/src/BTAdvertisedDeviceSet.cpp +++ b/libraries/BluetoothSerial/src/BTAdvertisedDeviceSet.cpp @@ -39,7 +39,7 @@ bool BTAdvertisedDeviceSet::haveRSSI() const { return m_haveRSSI; } * @return A string representation of this device. */ std::string BTAdvertisedDeviceSet::toString() { - std::string res = "Name: " + getName() + ", Address: " + getAddress().toString(); + std::string res = "Name: " + getName() + ", Address: " + std::string(getAddress().toString().c_str(), getAddress().toString().length()); if (haveCOD()) { char val[6]; snprintf(val, sizeof(val), "%d", getCOD()); diff --git a/libraries/BluetoothSerial/src/BTScanResultsSet.cpp b/libraries/BluetoothSerial/src/BTScanResultsSet.cpp index e347228c848..e7745e431ce 100644 --- a/libraries/BluetoothSerial/src/BTScanResultsSet.cpp +++ b/libraries/BluetoothSerial/src/BTScanResultsSet.cpp @@ -84,7 +84,7 @@ void BTScanResultsSet::clear() { } bool BTScanResultsSet::add(BTAdvertisedDeviceSet advertisedDevice, bool unique) { - std::string key = advertisedDevice.getAddress().toString(); + std::string key = std::string(advertisedDevice.getAddress().toString().c_str(), advertisedDevice.getAddress().toString().length()); if (!unique || m_vectorAdvertisedDevices.count(key) == 0) { m_vectorAdvertisedDevices.insert(std::pair(key, advertisedDevice)); return true; diff --git a/libraries/BluetoothSerial/src/BluetoothSerial.h b/libraries/BluetoothSerial/src/BluetoothSerial.h index d3c7d374d10..32d6ed0d64b 100644 --- a/libraries/BluetoothSerial/src/BluetoothSerial.h +++ b/libraries/BluetoothSerial/src/BluetoothSerial.h @@ -87,7 +87,7 @@ class BluetoothSerial: public Stream operator bool() const; void getBtAddress(uint8_t *mac); BTAddress getBtAddressObject(); - std::string getBtAddressString(); + String getBtAddressString(); private: String local_name; int timeoutTicks=0; From a9ba46a1e012cb2fd08491afee905228733d3661 Mon Sep 17 00:00:00 2001 From: Tomas Pilny Date: Sat, 11 Feb 2023 10:28:34 +0100 Subject: [PATCH 7/7] chaged string type in example --- libraries/BluetoothSerial/examples/GetLocalMAC/GetLocalMAC.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/BluetoothSerial/examples/GetLocalMAC/GetLocalMAC.ino b/libraries/BluetoothSerial/examples/GetLocalMAC/GetLocalMAC.ino index 69f219e1b62..a3ca6b026b6 100644 --- a/libraries/BluetoothSerial/examples/GetLocalMAC/GetLocalMAC.ino +++ b/libraries/BluetoothSerial/examples/GetLocalMAC/GetLocalMAC.ino @@ -21,7 +21,7 @@ void setup() { uint8_t mac_arr[6]; // Byte array to hold the MAC address from getBtAddress() BTAddress mac_obj; // Object holding instance of BTAddress with the MAC (for more details see libraries/BluetoothSerial/src/BTAddress.h) - std::string mac_str; // String holding the text version of MAC in format AA:BB:CC:DD:EE:FF + String mac_str; // String holding the text version of MAC in format AA:BB:CC:DD:EE:FF SerialBT.getBtAddress(mac_arr); // Fill in the array mac_obj = SerialBT.getBtAddressObject(); // Instantiate the object