BLE on the ESP32, in two roles
The ESP32 can be both at the same time:
- BLE Central (scanner): looks for nearby devices, reads advertising packets, connects as a client
- BLE Peripheral (server): publishes services and characteristics, accepts connections
Scenario 1: a beacon scanner
A sketch that lists every BLE device in range every 10 seconds (useful for presence detection in home automation):
#include <BLEDevice.h>
#include <BLEScan.h>
#include <BLEAdvertisedDevice.h>
BLEScan* scanner;
class Listener: public BLEAdvertisedDeviceCallbacks {
void onResult(BLEAdvertisedDevice d) {
Serial.printf("MAC: %s | Name: %s | RSSI: %d\n",
d.getAddress().toString().c_str(),
d.haveName() ? d.getName().c_str() : "-",
d.getRSSI());
}
};
void setup() {
Serial.begin(115200);
BLEDevice::init("");
scanner = BLEDevice::getScan();
scanner->setAdvertisedDeviceCallbacks(new Listener());
scanner->setActiveScan(true);
}
void loop() {
scanner->start(10, false); // 10 seconds
scanner->clearResults();
delay(1000);
}
Rough positioning from RSSI
The RSSI (Received Signal Strength Indicator) gives a rough idea of how far away the beacon is:
- -30 dBm → touching it / very close
- -60 dBm → 1-3 metres
- -80 dBm → 5-10 metres
- -95 dBm → at the edge of coverage
For Home Assistant: publish the MAC address and RSSI over MQTT, and bermuda or esphome-bluetooth-proxy will work out the approximate room-level position.
Scenario 2: a custom BLE server
Let us expose a characteristic that a client (a phone app, say) can read and write:
#include <BLEDevice.h>
#include <BLEServer.h>
#include <BLEUtils.h>
#include <BLE2902.h>
#define SERVICE_UUID "12345678-1234-1234-1234-1234567890ab"
#define CHARACTERISTIC_UUID "abcd1234-1234-1234-1234-1234567890ab"
BLECharacteristic* pChar;
void setup() {
BLEDevice::init("ESP32-Sensor");
BLEServer *pServer = BLEDevice::createServer();
BLEService *pService = pServer->createService(SERVICE_UUID);
pChar = pService->createCharacteristic(
CHARACTERISTIC_UUID,
BLECharacteristic::PROPERTY_READ |
BLECharacteristic::PROPERTY_NOTIFY
);
pChar->addDescriptor(new BLE2902());
pChar->setValue("22.5");
pService->start();
pServer->getAdvertising()->start();
}
void loop() {
float t = 22.0 + random(-50, 50) / 10.0;
String s = String(t, 2);
pChar->setValue(s.c_str());
pChar->notify();
delay(2000);
}
On a phone, use nRF Connect (free on Android and iOS): it connects to ESP32-Sensor, shows the characteristic and receives the values as they are pushed.
Sniffing Apple iBeacons
iBeacons use a standard format inside the manufacturer data. Here is how to pull out the UUID, major and minor:
std::string md = d.getManufacturerData();
if (md.length() >= 23 && md[0] == 0x4C && md[1] == 0x00
&& md[2] == 0x02 && md[3] == 0x15) {
// This is an iBeacon
// bytes 4-19 = UUID, 20-21 = major, 22-23 = minor
}
BLE power consumption
Keeping the scanner running costs around 80 mA. For battery-powered projects, alternate short scans (5 s) with deep sleep (60 s): that averages about 10 mA, and an 18650 cell lasts 12-15 days.
BLE versus Bluetooth Classic
On the original ESP32 both are supported, but they take up a lot of flash. On the ESP32-S3 and ESP32-C3 only BLE is available (no Bluetooth Classic): for thermal printers or A2DP speakers you need an original ESP32.
Bluetooth Proxy for Home Assistant
ESPHome offers a ready-made Bluetooth Proxy firmware: the ESP32 acts as a BLE hub that forwards every nearby packet to Home Assistant over Wi-Fi. It extends BLE devices (Xiaomi thermometers, SwitchBot) across the whole house using one or more ESP32s dotted around.