ESP32 Bluetooth Basics: BLE Advertising and Connections
The ESP32 supports both classic Bluetooth and Bluetooth Low Energy (BLE). This lesson focuses on BLE, which is ideal for IoT applications due to its low power consumption.
Key idea:
ESP32 BLE uses the NimBLE stack via the Arduino BLE library. It can act as a peripheral (advertising data) or central (scanning and connecting to other devices).
BLE Architecture
| Role | Description |
|---|---|
| Peripheral | Advertises data, accepts connections (e.g., sensor) |
| Central | Scans for peripherals, connects to them (e.g., phone app) |
| Service | Collection of related data (identified by UUID) |
| Characteristic | Individual data value within a service (read/write/notify) |
BLE Peripheral: Advertising Data
#include <BLEDevice.h>
#include <BLEServer.h>
#include <BLEUtils.h>
#include <BLE2902.h>
BLEServer* pServer = NULL;
BLECharacteristic* pCharacteristic = NULL;
bool deviceConnected = false;
bool oldDeviceConnected = false;
uint32_t value = 0;
#define SERVICE_UUID "4fafc201-1fb5-459e-8fcc-c5c9c331914b"
#define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8"
class MyServerCallbacks: public BLEServerCallbacks {
void onConnect(BLEServer* pServer) {
deviceConnected = true;
};
void onDisconnect(BLEServer* pServer) {
deviceConnected = false;
}
};
void setup() {
Serial.begin(115200);
// Create the BLE Device
BLEDevice::init("ESP32_BLE");
// Create the BLE Server
pServer = BLEDevice::createServer();
pServer->setCallbacks(new MyServerCallbacks());
// Create the BLE Service
BLEService *pService = pServer->createService(SERVICE_UUID);
// Create a BLE Characteristic
pCharacteristic = pService->createCharacteristic(
CHARACTERISTIC_UUID,
BLECharacteristic::PROPERTY_READ |
BLECharacteristic::PROPERTY_WRITE |
BLECharacteristic::PROPERTY_NOTIFY |
BLECharacteristic::PROPERTY_INDICATE
);
// Add descriptor for notifications
pCharacteristic->addDescriptor(new BLE2902());
// Start the service
pService->start();
// Start advertising
BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
pAdvertising->addServiceUUID(SERVICE_UUID);
pAdvertising->setScanResponse(true);
BLEDevice::startAdvertising();
Serial.println("Waiting for client connection...");
}
void loop() {
if (deviceConnected) {
pCharacteristic->setValue((uint8_t*)&value, 4);
pCharacteristic->notify();
value++;
delay(1000);
}
// Handle reconnect
if (!deviceConnected && oldDeviceConnected) {
delay(500);
pServer->startAdvertising();
Serial.println("Restarting advertising");
oldDeviceConnected = deviceConnected;
}
if (deviceConnected && !oldDeviceConnected) {
oldDeviceConnected = deviceConnected;
}
}BLE Central: Scanning for Devices
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEScan.h>
#include <BLEAdvertisedDevice.h>
int scanTime = 5; // In seconds
BLEScan* pBLEScan;
class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {
void onResult(BLEAdvertisedDevice advertisedDevice) {
Serial.printf("Advertised Device: %s \n", advertisedDevice.toString().c_str());
}
};
void setup() {
Serial.begin(115200);
Serial.println("Scanning...");
BLEDevice::init("");
pBLEScan = BLEDevice::getScan(); // Create new scan
pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
pBLEScan->setActiveScan(true); // Active scan uses more power
pBLEScan->setInterval(100);
pBLEScan->setWindow(99);
}
void loop() {
BLEScanResults foundDevices = pBLEScan->start(scanTime, false);
Serial.print("Devices found: ");
Serial.println(foundDevices.getCount());
Serial.println("Scan done!");
pBLEScan->clearResults();
delay(2000);
}Key Takeaways
- ESP32 supports BLE 5.0 via the NimBLE stack
- BLE devices can be peripherals (advertise) or centrals (scan/connect)
- Services and characteristics use UUIDs for identification
- Notifications allow server to push data to client
- BLE is ideal for battery-powered IoT sensors
Resources
- Espressif ESP32 BLE Documentation: https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/peripherals/bluedroid.html
- Video: ESP32 BLE Introduction - https://www.youtube.com/watch?v=9kRUSeeF3hY
- Arduino ESP32 BLE Examples: https://github.com/espressif/arduino-esp32/tree/master/libraries/BLE/examples