ESP32 Arduino: Getting the Bluetooth Device Address

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 FireBeetle 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.

If you prefer, you can check a video tutorial on how to obtain the device Bluetooth address on my Youtube channel. The approach used in this video is slightly different since we take advantage of the BluetoothSerial.h library to initialize the Bluetooth stack.


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.

ESP32 Arduino Bluetooth printing address.png

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.

ESP32 Bluetooth address vendor lookup.png

Figure 2 – Bluetooth device address vendor lookup.


References

[1] https://macaddresschanger.com/what-is-bluetooth-address-BD_ADDR

[2] http://esp-idf.readthedocs.io/en/latest/api-reference/bluetooth/esp_bt_device.html?highlight=esp_bt_dev_get_address


Related posts

16 thoughts on “ESP32 Arduino: Getting the Bluetooth Device Address”

  1. hey, nice work. i have been looking for something like this. but i have this one question: can we write the same code with an HC05 module of bluetooth and arduino UNO or the firebeetle board you have used is necessary? hope to get a reply at the earliest

    1. Hi!

      Thanks for the feedback 🙂

      This code shown here will only work on a ESP32-based board.

      The ESP32 has integrated Bluetooth and device specific APIs, even though we are using the Arduino core to program it.

      The functions used here are not available for the Arduino uno. Think of the functions shown here as part of a library that only works for the ESP32.

      When it comes to a system such as an Arduino uno + HC05, the working principle is completely different.

      Basically, you have two distinct devices. Thus, your Arduino Uno has to talk with the HC05 via serial, and the HC05 will act as a bridge that will send/receive the data over Bluetooth.

      Nonetheless, there should be plenty of tutorials for the HC05 and HC06 around the web. When I was more actively using Arduino boards, I recall these cheap devices being very popular to bring Bluetooth functionalities to the Arduino boards.

      Hope this clarifies 🙂

      Best regards,
      Nuno Santos

  2. hey, nice work. i have been looking for something like this. but i have this one question: can we write the same code with an HC05 module of bluetooth and arduino UNO or the firebeetle board you have used is necessary? hope to get a reply at the earliest

    1. Hi!
      Thanks for the feedback 🙂
      This code shown here will only work on a ESP32-based board.
      The ESP32 has integrated Bluetooth and device specific APIs, even though we are using the Arduino core to program it.
      The functions used here are not available for the Arduino uno. Think of the functions shown here as part of a library that only works for the ESP32.
      When it comes to a system such as an Arduino uno + HC05, the working principle is completely different.
      Basically, you have two distinct devices. Thus, your Arduino Uno has to talk with the HC05 via serial, and the HC05 will act as a bridge that will send/receive the data over Bluetooth.
      Nonetheless, there should be plenty of tutorials for the HC05 and HC06 around the web. When I was more actively using Arduino boards, I recall these cheap devices being very popular to bring Bluetooth functionalities to the Arduino boards.
      Hope this clarifies 🙂
      Best regards,
      Nuno Santos

  3. Look great work on detect Bluetooth ID address.

    Question, what if my specific mobile’s bluetooth id address only and your code detect mine within range to send output to unlock door or disabling alarm system?

    Possible?

  4. Hi. The tutorial is great

    I have a same question with David. what if my specific mobile’s bluetooth id address only and your code detect mine within range to send output to start a relay? is it possible?

    Btw, could u please help me with the circuit diagram? I’m just a newbie. Thanks

Leave a Reply

Discover more from techtutorialsx

Subscribe now to keep reading and get access to the full archive.

Continue reading