esp32-bluetooth-classic

How to Use Bluetooth Serial on ESP32

  • 4 min

The ESP32 features integrated Bluetooth, which allows us to easily establish wireless communication with other devices (such as smartphones, computers, etc.)

The ESP32 supports both Bluetooth Classic and BLE (Bluetooth Low Energy). Today we are going to look at Bluetooth Classic (in the next post we will cover BLE).

I will refer to Bluetooth Classic as the “normal” one, the traditional one, simply to differentiate it from BLE.

Bluetooth Classic is a wireless communication technology that operates in the 2.4 GHz band and allows data transmission between devices over short distances (generally up to 10 meters).

  • Frequency band: 2.4 GHz (ISM band).
  • Range: Up to 10 meters (can be extended with amplifiers).
  • Transmission speed: Up to 3 Mbps (in version 2.0 + EDR).
  • Topology: Point-to-point (P2P) or multipoint connections.
  • Security: Supports authentication and encryption.

One of the most common and useful applications of this technology is serial port communication, which allows sending and receiving data between the ESP32 and another device via Bluetooth.

Serial Communication over Bluetooth Classic

Serial communication is a simple method for transferring data between devices as a sequence of bits (you’ve surely used it before, for example, with a USB cable).

BluetoothSerial is a library integrated into the ESP32 environment that enables Bluetooth communication in classic mode. This mode is based on the SPP (Serial Port Profile), which emulates a serial connection over a wireless channel.

Let’s see how to use it with an example,

#include "BluetoothSerial.h"

BluetoothSerial SerialBT;

void setup() {
  Serial.begin(115200);
  SerialBT.begin("ESP32_BT_Classic"); // Bluetooth device name
  Serial.println("Bluetooth device is ready to pair.");
}

void loop() {
  if (Serial.available()) {
    SerialBT.write(Serial.read()); // Send data from serial monitor to Bluetooth
  }
  if (SerialBT.available()) {
    Serial.write(SerialBT.read()); // Send data from Bluetooth to serial monitor
  }
}
Copied!
  1. Include the BluetoothSerial library: This library facilitates the implementation of Bluetooth Classic on the ESP32.
  2. Create an instance of BluetoothSerial: SerialBT is the object that handles Bluetooth communication.
  3. Initialize Bluetooth: In setup(), we call SerialBT.begin("ESP32_BT_Classic") to start the Bluetooth device with the name “ESP32_BT_Classic”.
  4. Bidirectional communication: In loop(), the code checks if data is available on the serial port or on Bluetooth and transmits it accordingly.

Connection from an External Device

To test this example, follow these steps:

  1. Load the code onto the ESP32: Connect your ESP32 to the computer and upload the code above.
  2. Pair the device: On your phone or computer, search for Bluetooth devices and select “ESP32_BT_Classic”.
  3. Connect with a terminal application: Use a Bluetooth terminal application (like Serial Bluetooth Terminal on Android) to connect to the ESP32 and send/receive data.

Once the Bluetooth connection is established, you can send and receive data between the ESP32 and the other device.

For example, you can use the functions available in the BluetoothSerial library to send and receive data as if you were using a physical serial port.

Practical Examples