ESP-IDF: ESP32 Bluetooth controller MAC address retrieving

The code below is used to get the MAC address of the Bluetooth interface of an ESP32 microcontroller. As I am not a developer and programmer, I created the code in VS Code with the help of AI and the Espressif discussion forum. We found useful examples in the posts of this forum and also on Github. Honestly, it was an interesting experience with what AI can do and what it can’t handle.

Please don’t forget to enable Bluetooth and Bluetooth Classic mode functions in the ESP-IDF SDK Configuration Editor (menuconfig). You can use the gear icon on the bottom bat in VS Code to enter the menuconfig.

The code:

/************************************************************************************************

ESP32: ESP-IDF code creted in VS Code to retrieve the MAC address of the Bluetooth controller
Code created by AI and OK1TK with support of the Espressive ESP32 forum
***********************************************************************************************/
#include <stdio.h>
#include "esp_bt.h"
#include "esp_bt_main.h"
#include "esp_bt_device.h"
#include "esp_log.h"
#include "nvs_flash.h"

void app_main()
{

//Non-volatile storage (NVS) library is designed to store key-value pairs in flash.
esp_err_t ret = nvs_flash_init(); //Initialize the default NVS partition.
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
ESP_ERROR_CHECK(nvs_flash_erase());
ret = nvs_flash_init();
}
ESP_ERROR_CHECK( ret );

// Initialization of Bluetooth controller configuration
esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
ESP_LOGI("BT", "Call esp_bt_controller_init(&bt_cfg)");
if ((ret = esp_bt_controller_init(&bt_cfg)) != ESP_OK) {
ESP_LOGE("BT", "%s initialize controller FAILED!: %s\n", __func__, esp_err_to_name(ret));
return;
}
else{
ESP_LOGI("BT", "Initialize controller OK");
}

// Enable Bluetooth controller in CLASSIC Bluetooth mode
ESP_LOGI("BT", "Call esp_bt_controller_enable(ESP_BT_MODE_CLASSIC_BT)");
if ((ret = esp_bt_controller_enable(CONFIG_BT_CLASSIC_ENABLED)) != ESP_OK) {
ESP_LOGE("BT", "%s enable controller FAILED!: %s\n", __func__, esp_err_to_name(ret));
return;
}
else{
ESP_LOGI("BT", "Enable controller OK");
}

// Initialization of Bluedroid (necessary to be initialzed before calling "esp_bt_dev_get_address()")
ESP_LOGI("BT", "Call esp_bluedroid_init()");
if ((ret = esp_bluedroid_init()) != ESP_OK) {
ESP_LOGE("BT", "%s initialize bluedroid FAILED!: %s\n", __func__, esp_err_to_name(ret));
return;
}
else{
ESP_LOGI("BT", "Initialize bluedroid OK!");
}

// Eneable Bluedroid
ESP_LOGI("BT", "Call esp_bluedroid_enable()");
if ((ret = esp_bluedroid_enable()) != ESP_OK) {
ESP_LOGE("BT", "%s enable bluedroid FAILED!: %s\n", __func__, esp_err_to_name(ret));
return;
}
else{
ESP_LOGI("BT", "Enable bluedroid OK!");
}

// Declaration of a variable to store the MAC address
const uint8_t *bt_mac;
// Retrieving the the MAC address of the Bluetooth interface
bt_mac = esp_bt_dev_get_address();
if (bt_mac == NULL) {
ESP_LOGE("BT", "Unable to retrieve the MAC address of the Bluetooth interface");
return;
}

// Writing the MAC address of the Bluetooth interface to the terminal
ESP_LOGI("BT", "The Bluetooth interface MAC address: %02x:%02x:%02x:%02x:%02x:%02x",
bt_mac[0], bt_mac[1], bt_mac[2], bt_mac[3], bt_mac[4], bt_mac[5]);
}