Skip to content

As a developer, I want to know how to start working with Bluetooth on an ESP32.

What can I use to set up a connection?

The easiest way to implement Bluetooth on an ESP32 is to use the BluetoothSerial class. This class handles most of the Bluetooth connection for you. This thus makes it easy to set up and handle Bluetooth connections.
Another way to set up a bluetooth connection is to use Bluetooth low energy. BLE or bluetooth low energy is more power efficient when sending small amounts of data than regular bluetooth. The standard ESP32 library contains classes to make setting up BLE as easy as possible.
There’s also 3rd party libraries which make communication over Bluetooth even simpler by implementing abstraction layers. These libraries are made by 3rd parties and would thus have to be downloaded from the arduino library manager or from another source.

How do I set up a Bluetooth connection?

Bluetooth classic

For a simple Bluetooth connection, you can create an instance of the BluetoothSerial class. This will automatically initialize the Bluetooth server, which a client (a phone) can then connect to.

Example for setting up a Bluetooth classic server

//This example code is in the Public Domain (or CC0 licensed, at your option.)
//By Evandro Copercini - 2018
//
//This example creates a bridge between Serial and Classical Bluetooth (SPP)
//and also demonstrate that SerialBT have the same functionalities of a normal Serial

#include "BluetoothSerial.h"

#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif

BluetoothSerial SerialBT;

void setup() {
  Serial.begin(115200);
  SerialBT.begin("ESP32test"); //Bluetooth device name
  Serial.println("The device started, now you can pair it with bluetooth!");
}

void loop() {
  if (Serial.available()) {
    SerialBT.write(Serial.read());
  }
  if (SerialBT.available()) {
    Serial.write(SerialBT.read());
  }
  delay(20);
}
(Santos, 2019a)

Bluetooth low energy

For a Bluetooth low energy connection, there’s more work involved. First you initialize the BLEServer class, after which you create a server, which you give some characteristics. You then advertise this service so other devices (clients) can find and connect to the service.

Example for setting up a Bluetooth low energy server

/*
    Based on Neil Kolban example for IDF: https://github.com/nkolban/esp32-snippets/blob/master/cpp_utils/tests/BLE%20Tests/SampleServer.cpp
    Ported to Arduino ESP32 by Evandro Copercini
    updates by chegewara
*/

#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEServer.h>

// See the following for generating UUIDs:
// https://www.uuidgenerator.net/

#define SERVICE_UUID        "4fafc201-1fb5-459e-8fcc-c5c9c331914b"
#define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8"

void setup() {
  Serial.begin(115200);
  Serial.println("Starting BLE work!");

  BLEDevice::init("Long name works now");
  BLEServer *pServer = BLEDevice::createServer();
  BLEService *pService = pServer->createService(SERVICE_UUID);
  BLECharacteristic *pCharacteristic = pService->createCharacteristic(
                                         CHARACTERISTIC_UUID,
                                         BLECharacteristic::PROPERTY_READ |
                                         BLECharacteristic::PROPERTY_WRITE
                                       );

  pCharacteristic->setValue("Hello World says Neil");
  pService->start();
  // BLEAdvertising *pAdvertising = pServer->getAdvertising();  // this still is working for backward compatibility
  BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
  pAdvertising->addServiceUUID(SERVICE_UUID);
  pAdvertising->setScanResponse(true);
  pAdvertising->setMinPreferred(0x06);  // functions that help with iPhone connections issue
  pAdvertising->setMinPreferred(0x12);
  BLEDevice::startAdvertising();
  Serial.println("Characteristic defined! Now you can read it in your phone!");
}

void loop() {
  // put your main code here, to run repeatedly:
  delay(2000);
}
(Santos, 2019b)

Conclusion

There’s multiple ways to set up a Bluetooth connection on the ESP32. Both using Bluetooth classic and using Bluetooth low energy is made easy by the standard Bluetooth classes and methods. Using the examples above, I will be able to start working with Bluetooth on the ESP32.

While the Bluetooth classic is easier to set up, I think that for our project, using Bluetooth low energy will be beneficial for us as it uses less power.

Sources

  • Santos, S. (2019a). ESP32 Bluetooth Classic with Arduino IDE - Getting Started | Random Nerd Tutorials. Random Nerd Tutorials. https://randomnerdtutorials.com/esp32-bluetooth-classic-arduino-ide/
  • Santos, S. (2019b). ESP32 Bluetooth Low Energy (BLE) on Arduino IDE | Random Nerd Tutorials. Random Nerd Tutorials. https://randomnerdtutorials.com/esp32-bluetooth-low-energy-ble-arduino-ide/

As a developer, I would like to know what bluetooth low energy is

Why Bluetooth low energy exists

Regular Bluetooth can handle a lot of data but consumes a lot of power and can be quite expensive (Proctor, 2021). Bluetooth low energy is meant for applications that don’t need to exchange a lot of data and need to run for long amounts of time without replacing batteries. Bluetooth low energy hardware also costs less (Proctor, 2021).

How does Bluetooth low energy work?

While Bluetooth low energy uses the same radio band as regular Bluetooth, namely 2.4GHz (BasuMallick, 2022). Most of the technical similarities end there. Because while regular Bluetooth requires a continuous connection, Bluetooth low energy does not (BasuMallick, 2022). When using BLE, a client device only needs to connect whenever it needs to send data (BasuMallick, 2022). Between connections, the Bluetooth device is in standby, thus preserving large amounts of power (BasuMallick, 2022). To save even more energy, the protocol slows data transmission making it consume very little power (BasuMallick, 2022). They may only consume as little as 0.01 to 0.5 Watts of power(BasuMallick, 2022).

How does Buetooth low energy exchange data

Data is exchanged via attributes stored on the server. These attributes are stored in profiles known as GATT (BasuMallick, 2022). The device can request the value of those characteristics which effectively allows for simple data communication.

Conclusion

Bluetooth low energy is a power efficient Bluetooth technology that can transfer small amounts of data. Bluetooth low energy communicates through attribute profiles stored on the server.

Sources

  • Proctor, B. (2021, August 19). Bluetooth Vs. Bluetooth Low Energy: What’s The Difference? [2021 Update] | Blog | Link Labs. Retrieved May 3, 2023, from https://www.link-labs.com/blog/bluetooth-vs-bluetooth-low-energy BasuMallick, C. (2022, June 10). What Is Bluetooth LE? Meaning, Working, Architecture, Uses, and Benefits - Spiceworks. Spiceworks. Retrieved May 3, 2023, from https://www.spiceworks.com/tech/iot/articles/what-is-bluetooth-le/#_002

Last update: May 18, 2023