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 will look at Bluetooth Classic (in the next entry we will cover BLE).
I will refer to Bluetooth Classic as “normal”, the traditional one, just to differentiate it from BLE.
Bluetooth Classic is a wireless communication technology that operates in the 2.4 GHz band and enables data transmission between devices over short distances (typically 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 stream of bits (you have probably used it, for example, with a USB cable).
BluetoothSerial is an integrated library in 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
}
}
- Include the
BluetoothSerial
library: This library facilitates the implementation of Bluetooth Classic on the ESP32. - Create an instance of
BluetoothSerial
:SerialBT
is the object that manages Bluetooth communication. - Initialize Bluetooth: In
setup()
, we callSerialBT.begin("ESP32_BT_Classic")
to start the Bluetooth device with the name “ESP32_BT_Classic”. - Bidirectional communication: In
loop()
, the code checks if data is available on the serial port or Bluetooth and transmits it accordingly.
Connection from an External Device
To test this example, follow these steps:
- Upload the code to the ESP32: Connect your ESP32 to the computer and upload the previous code.
- Pair the device: On your phone or computer, search for Bluetooth devices and select “ESP32_BT_Classic”.
- Connect with a terminal application: Use a Bluetooth terminal application (such as 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
Remote Device Control
Bluetooth Classic is very useful for remote control applications (such as controlling robots or smart home systems).
Here is a basic example of how to control an LED connected to the ESP32 from a smartphone:
#include "BluetoothSerial.h"
BluetoothSerial SerialBT;
const int ledPin = 2; // LED pin
void setup() {
pinMode(ledPin, OUTPUT);
Serial.begin(115200);
SerialBT.begin("ESP32_LED_Control");
Serial.println("Ready to receive commands.");
}
void loop() {
if (SerialBT.available()) {
char command = SerialBT.read();
if (command == '1') {
digitalWrite(ledPin, HIGH); // Turn on LED
SerialBT.println("LED on");
} else if (command == '0') {
digitalWrite(ledPin, LOW); // Turn off LED
SerialBT.println("LED off");
}
}
}
Audio Streaming
Bluetooth Classic is widely used for audio streaming (such as in wireless headphones).
Although the ESP32 does not have dedicated hardware for high-quality audio streaming, it is possible to implement basic solutions using libraries like ESP32-A2DP.
#include "BluetoothA2DPSource.h"
BluetoothA2DPSource a2dp_source;
void setup() {
a2dp_source.start("ESP32_Audio"); // Bluetooth device name
}
void loop() {
// Here you can add logic to handle audio streaming
}