ESP32 is a microcontroller board that features dual Bluetooth support. ESP32 contains both the Classic and low energy variant of Bluetooth that is BLE. Bluetooth helps to exchange short information with more accuracy and privacy. Today we will look at how we can extract ESP32 Bluetooth Addresses using Arduino IDE.
Introduction to ESP32 Bluetooth Address
Bluetooth address is also referred to as Bluetooth MAC address. It is a unique 48-bit identifier address assigned to different Bluetooth devices so they can be recognized inside a network. Bluetooth device address is usually represented as a 6-byte number which is written in Hexadecimal, and each separated by a colon. (example – 00:11:22:33:FF:EE).
Bluetooth Address first half is called Organizationally Unique Identifier (OUI). It determines the manufacturer of Bluetooth devices. OUI is assigned by IEEE (Institute of Electrical and Electronics Engineers).
Bluetooth Address contains three halves:
- NAP
- UAP
- LAP
NAP (Non-Significant Address Part)-2 bytes: It contains the first 16 bits of OUI. NAP value is used for frequency hopping.
UAP (Upper Address Part)-1 byte: It has remaining 8 bits of OUI. The UAP is used for seeding in different Bluetooth algorithms.
LAP (Lower Address Part)-3 bytes: This Bluetooth address portion is given by device manufacturer. LAP value identifies Bluetooth devices as Access code parts.
Let’s find out ESP32 Bluetooth MAC address.
Finding ESP32 Bluetooth Address
To get an ESP32 Bluetooth address first we have to initialize the ESP32 Bluetooth. Once the Bluetooth is enabled we can print the device address using the printDeviceAddress() function. Now we will upload code to get the device Bluetooth address.
Code
Open Arduino IDE and upload the given code in the ESP32 board:
#include "esp_bt_device.h"
#include "BluetoothSerial.h"
BluetoothSerial SerialBT;
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);
SerialBT.begin("ESP32 Bluetooth");
printDeviceAddress();
}
void loop() {}
Code started by including the necessary Bluetooth libraries which include the Bluetooth stack library and the (esp_bt_device.h) library that allows us to use the printDeviceAddress() function.
To get ESP32 Bluetooth address we will call the esp_bt_dev_get_address function. This function takes no argument, and it returns the six bytes Bluetooth address. These six bytes will return as a pointer to array unint8_t where it will store inside a variable.
For loop is used to print the six bytes one by one. A sprintf function is used that will format each character of byte into two characters hexadecimal string. By doing this the Bluetooth address will be printed in standard format.
Also, we used the %02X format specifier which will print each character in hexadecimal uppercase string with two characters. A colon will also be printed after each byte.
Output
After uploading code to ESP32 we can see the Bluetooth device address on the serial monitor of Arduino IDE.
The address printed in our case is 7C:9E:BD:4B:3B:22. The first three bytes 7C:9E:BD will give us the information about the manufacturer of the device.
Extracting Details About ESP32 Using the Bluetooth Address
As we have got the ESP32 Bluetooth device address now we can extract information related to the manufacturer of the Bluetooth device.
For that copy the first three bytes of the address 7C:9E:BD. Open any Bluetooth device address lookup online. Here is a free tool you can try (Bluetooth MAC Lookup).
Paste the first three bytes of Bluetooth address and press enter.
New window will open showing us the manufacturer of ESP32 board which is Espressif Inc.
We have successfully extracted an ESP32 Bluetooth address and verified it using an online free tool.
Conclusion
Bluetooth address is a unique 48-bit address that allows devices to identify them during wireless communication and data transfer. Here this tutorial is a simple guide on how to extract ESP32 Bluetooth addresses using the Arduino code. Further we verify the address using an online free tool.