Skip to content

Added methods + example to retrive local MAC for BT #7778

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Feb 13, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fixed typos and formatting + added doxygen comments
  • Loading branch information
PilnyTomas committed Feb 1, 2023
commit a9d121947997186a2df0be884283c5f26f854b54
12 changes: 6 additions & 6 deletions libraries/BluetoothSerial/src/BTAddress.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
* ```
Expand All @@ -100,11 +100,11 @@ esp_bd_addr_t *BTAddress::getNative() const {
std::string BTAddress::toString(bool capital) const {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see that originally the method returns std::string, but please change it to Arduino String instead.

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;
Expand Down
35 changes: 24 additions & 11 deletions libraries/BluetoothSerial/src/BluetoothSerial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1181,18 +1181,31 @@ std::map<int, std::string> 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() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here std::string =>> String

return getBtAddressObject().toString(true);
}
#endif