Skip to content

Commit 2ddce3c

Browse files
authored
Added methods + example to retrive local MAC for BT (espressif#7778)
* Added methods + example to retrive local MAC for BT * Added .skip files in the new example folder * Fixed typos and formatting + added doxygen comments * changed std::string to String * another std::string -> String * Changed std::string to String * chaged string type in example
1 parent efe966d commit 2ddce3c

File tree

10 files changed

+95
-15
lines changed

10 files changed

+95
-15
lines changed

Diff for: libraries/BluetoothSerial/examples/GetLocalMAC/.skip.esp32c3

Whitespace-only changes.

Diff for: libraries/BluetoothSerial/examples/GetLocalMAC/.skip.esp32s2

Whitespace-only changes.

Diff for: libraries/BluetoothSerial/examples/GetLocalMAC/.skip.esp32s3

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// This example demonstrates usage of BluetoothSerial method to retrieve MAC address of local BT device in various formats.
2+
// By Tomas Pilny - 2023
3+
4+
#include "BluetoothSerial.h"
5+
6+
String device_name = "ESP32-example";
7+
8+
#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
9+
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
10+
#endif
11+
12+
#if !defined(CONFIG_BT_SPP_ENABLED)
13+
#error Serial Bluetooth not available or not enabled. It is only available for the ESP32 chip.
14+
#endif
15+
16+
BluetoothSerial SerialBT;
17+
18+
void setup() {
19+
Serial.begin(115200);
20+
SerialBT.begin(device_name); //Bluetooth device name
21+
22+
uint8_t mac_arr[6]; // Byte array to hold the MAC address from getBtAddress()
23+
BTAddress mac_obj; // Object holding instance of BTAddress with the MAC (for more details see libraries/BluetoothSerial/src/BTAddress.h)
24+
String mac_str; // String holding the text version of MAC in format AA:BB:CC:DD:EE:FF
25+
26+
SerialBT.getBtAddress(mac_arr); // Fill in the array
27+
mac_obj = SerialBT.getBtAddressObject(); // Instantiate the object
28+
mac_str = SerialBT.getBtAddressString(); // Copy the string
29+
30+
Serial.print("This device is instantiated with name "); Serial.println(device_name);
31+
32+
Serial.print("The mac address using byte array: ");
33+
for(int i = 0; i < ESP_BD_ADDR_LEN-1; i++){
34+
Serial.print(mac_arr[i], HEX); Serial.print(":");
35+
}
36+
Serial.println(mac_arr[ESP_BD_ADDR_LEN-1], HEX);
37+
38+
Serial.print("The mac address using BTAddress object using default method `toString()`: "); Serial.println(mac_obj.toString().c_str());
39+
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()
40+
41+
Serial.print("The mac address using string: "); Serial.println(mac_str.c_str());
42+
}
43+
44+
void loop(){
45+
46+
}

Diff for: libraries/BluetoothSerial/src/BTAddress.cpp

+14-8
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ BTAddress::BTAddress() {
4444
*
4545
* @param [in] stringAddress The hex representation of the address.
4646
*/
47-
BTAddress::BTAddress(std::string stringAddress) {
47+
BTAddress::BTAddress(String stringAddress) {
4848
if (stringAddress.length() != 17) return;
4949

5050
int data[6];
@@ -86,20 +86,26 @@ esp_bd_addr_t *BTAddress::getNative() const {
8686

8787
/**
8888
* @brief Convert a BT address to a string.
89-
*
90-
* A string representation of an address is in the format:
91-
*
89+
* @param [in] capital changes the letter size
90+
* By default the parameter `capital` == false and the string representation of an address is in the format:
9291
* ```
9392
* xx:xx:xx:xx:xx:xx
9493
* ```
95-
*
94+
* When the parameter `capital` == true the format uses capital letters:
95+
* ```
96+
* XX:XX:XX:XX:XX:XX
97+
* ```
9698
* @return The string representation of the address.
9799
*/
98-
std::string BTAddress::toString() const {
100+
String BTAddress::toString(bool capital) const {
99101
auto size = 18;
100102
char *res = (char*)malloc(size);
101-
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]);
102-
std::string ret(res);
103+
if(capital){
104+
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]);
105+
}else{
106+
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]);
107+
}
108+
String ret(res);
103109
free(res);
104110
return ret;
105111
} // toString

Diff for: libraries/BluetoothSerial/src/BTAddress.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
#include "sdkconfig.h"
1313
#if defined(CONFIG_BT_ENABLED) && defined(CONFIG_BLUEDROID_ENABLED)
1414
#include <esp_gap_bt_api.h> // ESP32 BT
15-
#include <string>
15+
#include <Arduino.h>
1616

1717

1818
/**
@@ -24,12 +24,12 @@ class BTAddress {
2424
public:
2525
BTAddress();
2626
BTAddress(esp_bd_addr_t address);
27-
BTAddress(std::string stringAddress);
27+
BTAddress(String stringAddress);
2828
bool equals(BTAddress otherAddress);
2929
operator bool () const;
3030

3131
esp_bd_addr_t* getNative() const;
32-
std::string toString() const;
32+
String toString(bool capital = false) const;
3333

3434
private:
3535
esp_bd_addr_t m_address;

Diff for: libraries/BluetoothSerial/src/BTAdvertisedDeviceSet.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ bool BTAdvertisedDeviceSet::haveRSSI() const { return m_haveRSSI; }
3939
* @return A string representation of this device.
4040
*/
4141
std::string BTAdvertisedDeviceSet::toString() {
42-
std::string res = "Name: " + getName() + ", Address: " + getAddress().toString();
42+
std::string res = "Name: " + getName() + ", Address: " + std::string(getAddress().toString().c_str(), getAddress().toString().length());
4343
if (haveCOD()) {
4444
char val[6];
4545
snprintf(val, sizeof(val), "%d", getCOD());

Diff for: libraries/BluetoothSerial/src/BTScanResultsSet.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ void BTScanResultsSet::clear() {
8484
}
8585

8686
bool BTScanResultsSet::add(BTAdvertisedDeviceSet advertisedDevice, bool unique) {
87-
std::string key = advertisedDevice.getAddress().toString();
87+
std::string key = std::string(advertisedDevice.getAddress().toString().c_str(), advertisedDevice.getAddress().toString().length());
8888
if (!unique || m_vectorAdvertisedDevices.count(key) == 0) {
8989
m_vectorAdvertisedDevices.insert(std::pair<std::string, BTAdvertisedDeviceSet>(key, advertisedDevice));
9090
return true;

Diff for: libraries/BluetoothSerial/src/BluetoothSerial.cpp

+27-2
Original file line numberDiff line numberDiff line change
@@ -661,8 +661,6 @@ static bool _init_bt(const char *deviceName)
661661
}
662662
}
663663

664-
// Why only master need this? Slave need this during pairing as well
665-
// if (_isMaster && esp_bt_gap_register_callback(esp_bt_gap_cb) != ESP_OK) {
666664
if (esp_bt_gap_register_callback(esp_bt_gap_cb) != ESP_OK) {
667665
log_e("gap register failed");
668666
return false;
@@ -1183,4 +1181,31 @@ std::map<int, std::string> BluetoothSerial::getChannels(const BTAddress &remoteA
11831181
return sdpRecords;
11841182
}
11851183

1184+
/**
1185+
* @brief Gets the MAC address of local BT device in byte array.
1186+
*
1187+
* @param mac [out] The mac
1188+
*/
1189+
void BluetoothSerial::getBtAddress(uint8_t *mac) {
1190+
const uint8_t *dev_mac = esp_bt_dev_get_address();
1191+
memcpy(mac, dev_mac, ESP_BD_ADDR_LEN);
1192+
}
1193+
/**
1194+
* @brief Gets the MAC address of local BT device as BTAddress object.
1195+
*
1196+
* @return The BTAddress object.
1197+
*/
1198+
BTAddress BluetoothSerial::getBtAddressObject() {
1199+
uint8_t mac_arr[ESP_BD_ADDR_LEN];
1200+
getBtAddress(mac_arr);
1201+
return BTAddress(mac_arr);
1202+
}
1203+
/**
1204+
* @brief Gets the MAC address of local BT device as string.
1205+
*
1206+
* @return The BT MAC address string.
1207+
*/
1208+
String BluetoothSerial::getBtAddressString() {
1209+
return getBtAddressObject().toString(true);
1210+
}
11861211
#endif

Diff for: libraries/BluetoothSerial/src/BluetoothSerial.h

+3
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ class BluetoothSerial: public Stream
8585
const int MAX_INQ_TIME = (ESP_BT_GAP_MAX_INQ_LEN * INQ_TIME);
8686

8787
operator bool() const;
88+
void getBtAddress(uint8_t *mac);
89+
BTAddress getBtAddressObject();
90+
String getBtAddressString();
8891
private:
8992
String local_name;
9093
int timeoutTicks=0;

0 commit comments

Comments
 (0)