Skip to the content.

๐Ÿ”ต 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:


๐Ÿงช 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:


๐Ÿ” 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

๐Ÿ“š Useful Resources