0

$USD

$USD
TUTORIALS ESP32

ESP32 Arduino Bluetooth Classic: Setting the device name

DFRobot Mar 14 2018 1786

The objective of this esp32 arduino tutorial post is to explain how we can set the name of the ESP32 operating as a Bluetooth device, so it displays a user friendly name for other devices that discover it.  The tests of this ESP32 tutorial were performed using a DFRobot’s ESP32 device integrated in a ESP32 development board.

Introduction

The objective of this esp32 tutorial is to explain how we can set the name of the ESP32 operating as a Bluetooth device, so it displays a user friendly name for other devices that discover it.

You can check the previous post for an introduction on how to get started using the Bluetooth classic on the ESP32 running the Arduino core and how to initialize the Bluetooth functionality and make the device discoverable.

Please note that the mentioned post also introduces some more theoretical features of the Bluetooth protocol and of its architecture on the ESP32, which are useful for understanding the code from this tutorial.

We will keep using IDF’s lower level API, so we know what’s going on under the hood. However, we are going to be programming on the Arduino environment, which allows us to keep the best of both frameworks.

The tests of this ESP32 tutorial were performed using a DFRobot’s ESP-WROOM-32 device integrated in a ESP32 FireBeetle board.

The code

The code for this esp32 arduino tutorial will be very similar to the previous one, since we will need to initialize the controller and the host stack and make the ESP32 discoverable, so other Bluetooth devices can see it. Additionally, we will call a function from the Bluetooth API to set the device name.

We start by making the includes of the libraries needed to initialize the Bluedroid host stack (esp_bt_main.h) and to have access to the GAP (Generic Access Profile) functionalities (esp_gap_bt_api.h).

#include "esp_bt_main.h"
#include "esp_gap_bt_api.h"

Additionally, we will need the esp_bt_device.h library, which exposes the function needed to setup the name of the device. You can read the IDF documentation for this library here.

#include "esp_bt_device.h"

Next we will handle the Bluetooth initialization in a dedicated function, which we will call initBluetooth. This function will initialize both the controller and host stacks, set the device discoverability and set its name.

So, our function will receive as input the name to assign to the device and will return a Boolean value indicating if the initialization was successful or not.

bool initBluetooth(const char *deviceName) {
// initialization code here
}

The first part of the init function will be the same of the previous tutorial. We first need to initialize the controller stack by calling the btStart function (source code here).

Note that this function is a Arduino wrapper that calls some IDF functions under the hood. Thus, it abstracts some of the controller stack initialization logic that we would have to handle while using only IDF calls for this step.

if (!btStart()) {
    Serial.println("Failed to initialize controller");
    return false;
}

Next we need to handle the Bluedroid host stack initialization. In this case, at the time of writing, there isn’t yet a Arduino wrapper on top of the IDF calls. Nonetheless the initialization logic is as simple as first calling an init function (esp_bluedroid_init), followed by an enable function (esp_bluedroid_enable).

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;
}

Now that we have handled the initialization, we will set the device name. We will do this before configuring it as discoverable, so it shows the correct name as soon as some other device finds it.

To do it, we simply call the esp_bt_dev_set_device_name function, which receives as input the name to assign to the device. we will pass to it the deviceName parameter we have defined for our initBluetooth function.

This function returns a value of type esp_err_t, which is the same returned by the previously called functions. Although we are not including this validation in the code below for simplicity, note that we can check for the ESP_OK value to make sure there was no problem in setting the device name.

esp_bt_dev_set_device_name(deviceName);

To finalize, we make the device discoverable for other Bluetooth enabled devices with a call to the esp_bt_gap_set_scan_mode function, passing as input the enumerated value ESP_BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE.

esp_bt_gap_set_scan_mode(ESP_BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE);

With this call, we end our Bluetooth initialization function. Note that to make this function more robust and reusable, we could improve it by returning different error reasons when something fails, rather than just a binary value.

Now we will use this function on the Arduino setup function. But first thing we will do is opening a serial connection, to print a message in case any initialization error occurs.

Serial.begin(115200);

Next, we call the Bluetooth init function, passing as input the name we want to assign to the ESP32. We will call it “ESP32 BT“.

Additionally, we will do the error checking on this call so we are sure everything executed correctly.

if(!initBluetooth("ESP32 BT")){
    Serial.println("Bluetooth init failed");
};

The final complete code can be seen below.

#include "esp_bt_main.h"
#include "esp_gap_bt_api.h"
#include "esp_bt_device.h"
 
bool initBluetooth(const char *deviceName)
{
  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;
  }
 
  esp_bt_dev_set_device_name(deviceName);
 
  esp_bt_gap_set_scan_mode(ESP_BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE);
 
}
 
void setup() {
  Serial.begin(115200);
 
  if(!initBluetooth("ESP32 BT")){
    Serial.println("Bluetooth init failed");
  };
 
}
 
void loop() {}

Testing the code

To test the code, simply compile it and upload it to the ESP32 using the Arduino IDE. After that, you can open the IDE serial monitor to confirm no error message was printed.

If everything was executed without errors, then your ESP32 should be visible from other Bluetooth enabled devices, showing the name we configured in the code. Figure 1 exemplifies the device seen from a Bluetooth enabled laptop, on Windows 8.

Figure 1 – ESP32 discoverable as Bluetooth device.


DFRobot supply lots of esp32 arduino tutorials and esp32 projects for makers to learn.