In this ESP32 tutorial, we will check how to get the Bluetooth address of the device, using the Arduino core. The tests of this ESP32 tutorial were performed using a DFRobot’s ESP-WROOM-32 device integrated in a ESP32 Development board.
Introduction
In this ESP32 tutorial, we will check how to get the Bluetooth address of the device, using the Arduino core.
The Bluetooth Device Address (sometimes referred as BD_ADDR) is a unique 6 byte identifier assigned to each Bluetooth device by the manufacturer [1].
One important thing to mentioned is that the 3 most significant bytes (upper part of the address) can be used to determine the manufacturer of the device [1].
Regarding the code, we will be using the IDF Bluetooth API, which provides to us a function to retrieve the mention address.
The tests of this ESP32 tutorial were performed using a DFRobot’s ESP-WROOM-32 device integrated in a ESP32 FireBeetle board.
The code
We will start our code by including the libraries needed to both initialize the Bluetooth stack (esp_bt_main.h) and to have access to the function that allows to retrieve the device address (esp_bt_device.h).
#include "esp_bt_main.h"
#include "esp_bt_device.h"
As we have been doing in the previous tutorials, we will create a function to initialize both the Bluetooth controller and host stacks. Although in this tutorial we are not going to actually perform any Bluetooth communication, we need to have the Bluedroid host stack initialized and enabled to be able to retrieve the device address [2].
So, our Bluetooth init function will be similar to what we have been doing in the previous tutorials. We first initialize and enable the controller stack with a call to the btStart function and then we initialize the Bluedroid stack with a call to the esp_bluedroid_init function. After that, we call the esp_bluedroid_enable to enable the Bluedroid stack.
You can check this init function below, already with all the mentioned calls and the error checking.
bool initBluetooth()
{
if (!btStart()) {
Serial.println("Failed to initialize controller");
return false;
}
if (esp_bluedroid_init() != ESP_OK) {
Serial.println("Failed to initialize bluedroid");
return false;
}
if (esp_bluedroid_enable() != ESP_OK) {
Serial.println("Failed to enable bluedroid");
return false;
}
}
We will follow the same approach and encapsulate the printing of the device address in a function, which we will call printDeviceAddress.
void printDeviceAddress() {
// Print code here
}
In order to get the device address, we simply need to call the esp_bt_dev_get_address function.
This function takes no arguments and returns the six bytes of the Bluetooth device address. In case the Bluedroid stack has not been initialized, it will return NULL [2].
Note that the six bytes will be returned as a pointer to an array of uint8_t, which we will store on a variable.
const uint8_t* point = esp_bt_dev_get_address();
As mentioned, this array will have 6 elements which we can iterate and print one by one.
for (int i = 0; i < 6; i++) {
// Format and print the bytes
}
We will use the sprintf function to format each byte of the address in a two characters length hexadecimal string, to make it easier to read and to follow the standard format [1].
We will use the %02X format specifier, which prints each byte as a hexadecimal uppercase string with two characters, with a leading zero padded if needed.
Note that in the standard format the address is displayed with each byte separated by colons [1], which is also the format we are going to use.
for (int i = 0; i < 6; i++) {
char str[3];
sprintf(str, "%02X", (int)point[i]);
Serial.print(str);
if (i < 5){
Serial.print(":");
}
}
Now that we finished our printing function, we will move on to the Arduino setup. There, we will initialize a serial connection to print the results of our program.
Followed by that, we will call the Bluetooth initialization function and the address printing function.
void setup() {
Serial.begin(115200);
initBluetooth();
printDeviceAddress();
}
The final source code can be seen below.
#include "esp_bt_main.h"
#include "esp_bt_device.h"
bool initBluetooth()
{
if (!btStart()) {
Serial.println("Failed to initialize controller");
return false;
}
if (esp_bluedroid_init() != ESP_OK) {
Serial.println("Failed to initialize bluedroid");
return false;
}
if (esp_bluedroid_enable() != ESP_OK) {
Serial.println("Failed to enable bluedroid");
return false;
}
}
void printDeviceAddress() {
const uint8_t* point = esp_bt_dev_get_address();
for (int i = 0; i < 6; i++) {
char str[3];
sprintf(str, "%02X", (int)point[i]);
Serial.print(str);
if (i < 5){
Serial.print(":");
}
}
}
void setup() {
Serial.begin(115200);
initBluetooth();
printDeviceAddress();
}
void loop() {}
Testing the code
To test the code, simply compile it and upload it. When it finishes, open the Arduino IDE Serial Monitor and check the string that gets printed. You should have a result similar to figure 1, which shows the device address in the hexadecimal format we specified.
Figure 1 – Printing the Bluetooth address of the ESP32.
As mentioned in the introductory section, we can use this address to lookup the vendor of the Bluetooth device. You can use this website to make the lookup. As shown in figure 2, the device address shown before has Espressif (the company that makes the ESP32) as vendor.
Figure 2 – Bluetooth device address vendor lookup.