Closed vishesh-varma closed 1 year ago
You can get the device address with NimBLEDevice::getAddress
.
uint8_t mac[6] = {};
esp_read_mac(mac, ESP_MAC_BT);
All you have to do is to convert hex values to chars.
Thanks!
NimBLEAddress addr = NimBLEDevice::getAddress();
const uint8_t *addrs = addr.getNative();
String name = "myDevice " + String((addrs[4]<<8)|(addrs[5]), HEX);
NimBLEDevice::init(name.c_str());
This is how I'm doing it after h2zero's comment. The code is untested right now, but I just wanted to know if this is anywhere near how it should be.
This is the final code snippet that has been tested:
NimBLEDevice::init("X");
NimBLEAddress addr = NimBLEDevice::getAddress();
const uint8_t *addrs = addr.getNative();
char name[15];
sprintf(name, "myDevice %X\n", (addrs[1] << 8) | (addrs[0]));
NimBLEDevice::setDeviceName(name);
Some things to note:
init
with a random name, and then changed the name later on.getNative()
function writes Right-Most Byte(idk if I can call it LSB) at index 0. I wanted the name to be myDevice EEFF
if my MAC Address was AA:BB:CC:DD:EE:FF
, but addrs[4]
and addrs[5]
returned BB
and AA
respectively. So, I changed it index to 1
and 0
.Thanks for the update, glad it's working for you 😄
I want to name by device based on its MAC Address, as I plan to have multiple devices and I wanted to differentiate between them. For example, an ESP with addr:
43:5A:12:03:fA:65:02
would havemyDevice 6502
as its BLE device name.The examples I found on the internet only do this by first advertising, then scanning, and then getting the MAC address from the scanned data. That would work okay if there was only 1 BLE device in the area. But in case there are multiple devices, there's a change that a different BLE device's address will get scanned.
Can I not access the MAC Address without starting the advertisement? Is there a way to add it to the library?