๐ต ESP32 Bluetooth Tutorial โ Classic SPP vs BLE (Bluetooth Low Energy)
In this tutorial, youโll learn how to use both Classic Bluetooth (SPP) and BLE (Bluetooth Low Energy) with your ESP32 to wirelessly communicate with a smartphone or computer.
Youโll understand the difference between SPP and BLE, when to use each, and how to build simple programs to send and receive data wirelessly using both methods.
๐ง Principle: What is Bluetooth?
Bluetooth is a wireless technology standard that allows devices to exchange data over short distances. ESP32 supports two Bluetooth protocols:
Bluetooth Type | Description | Use Case |
---|---|---|
๐ต Classic Bluetooth (SPP) | Works like a virtual serial port | Debugging, terminal communication |
๐ฆ Bluetooth Low Energy (BLE) | Efficient, low-power, event-driven | Sensors, mobile apps, fitness devices |
๐ง SPP vs BLE (Quick Comparison)
Feature | Classic Bluetooth (SPP) | BLE (Bluetooth Low Energy) |
---|---|---|
Power Consumption | ๐ Higher | โก Very low |
Communication Style | Stream-based (Serial-like) | Event-based (GATT Services/Characteristics) |
Supported on iPhone | โ No | โ Yes |
Easy to use for Serial | โ Yes | โ Requires GATT structure |
Best For | Terminals, debug, data logs | Mobile apps, sensors, low-power devices |
๐ Use SPP for simplicity and fast testing (on Android only), and use BLE for battery-friendly, cross-platform communication.
๐งช Part 1: Classic Bluetooth (SPP) Example โ Serial Communication
๐ฒ Use Case: Send and receive text with phone (Android only)
๐ง Circuit: None needed (optional: connect LED to GPIO 2)
#include "BluetoothSerial.h"
BluetoothSerial SerialBT;
void setup() {
Serial.begin(115200);
SerialBT.begin("ESP32_SPP_Device"); // Bluetooth name
pinMode(2, OUTPUT);
Serial.println("Classic Bluetooth SPP started!");
}
void loop() {
if (SerialBT.available()) {
String received = SerialBT.readStringUntil('\n');
Serial.print("Received: ");
Serial.println(received);
SerialBT.print("Echo: ");
SerialBT.println(received);
if (received == "ON") digitalWrite(2, HIGH);
if (received == "OFF") digitalWrite(2, LOW);
}
}
โ Test With:
- Android App: Serial Bluetooth Terminal
- Pair with
ESP32_SPP_Device
, then connect and send commands likeON
,OFF
๐งช Part 2: Bluetooth Low Energy (BLE) Example โ GATT Server
๐ฒ Use Case: Send data to iPhone or Android BLE app
๐ง Circuit: Optional LED on GPIO 2
#include <BLEDevice.h>
#include <BLEServer.h>
#include <BLEUtils.h>
#include <BLE2902.h>
BLECharacteristic* pCharacteristic;
bool deviceConnected = false;
class MyServerCallbacks : public BLEServerCallbacks {
void onConnect(BLEServer* pServer) {
deviceConnected = true;
Serial.println("BLE Client Connected");
}
void onDisconnect(BLEServer* pServer) {
deviceConnected = false;
Serial.println("BLE Client Disconnected");
}
};
void setup() {
Serial.begin(115200);
pinMode(2, OUTPUT);
BLEDevice::init("ESP32_BLE_Device");
BLEServer *pServer = BLEDevice::createServer();
pServer->setCallbacks(new MyServerCallbacks());
BLEService *pService = pServer->createService("180F"); // Battery Service UUID
pCharacteristic = pService->createCharacteristic(
"2A19", // Battery Level characteristic
BLECharacteristic::PROPERTY_READ |
BLECharacteristic::PROPERTY_NOTIFY |
BLECharacteristic::PROPERTY_WRITE
);
pCharacteristic->addDescriptor(new BLE2902());
pCharacteristic->setValue("Hello from ESP32!");
pService->start();
pServer->getAdvertising()->start();
Serial.println("BLE advertising started!");
}
void loop() {
if (deviceConnected) {
pCharacteristic->setValue("Updated at " + String(millis()));
pCharacteristic->notify();
delay(2000);
}
}
โ Test With:
- Android/iOS App: nRF Connect or LightBlue
- Connect to
ESP32_BLE_Device
, discover service180F
, read/write2A19
๐ How It Works
Function | Classic SPP | BLE |
---|---|---|
SerialBT.begin(name) |
Starts Bluetooth Serial | โ |
BLEDevice::init() |
โ | Initializes BLE stack |
SerialBT.readString() |
Reads from Bluetooth | โ |
pCharacteristic->setValue() |
โ | Sets value for BLE characteristic |
pCharacteristic->notify() |
โ | Pushes updated value to client |
๐ When to Use Which?
Goal | Use This |
---|---|
Quickly test Bluetooth with Android | โ Classic SPP |
Send sensor data to iPhone | โ BLE |
Create a mobile app with BLE scanner | โ BLE |
Serial-style debug monitor | โ SPP |
Build a low-power fitness band or beacon | โ BLE |