Build the server
Our ESP32 measures temperature and keeps it in a GATT server: a small table of values. The phone is the GATT client: it asks for values. In connection terms (GAP) the ESP32 is the peripheral and the phone the central.
The server keeps its data in rows called attributes. Each row has a handle (its row number), a type given by a UUID, and a value. createService adds the first row: service 0x181A, Environmental Sensing, a number the Bluetooth SIG assigns.
createCharacteristic adds 0x2A6E, Temperature. It adds two rows: a declaration saying what clients may do (READ and NOTIFY), and the value itself. BLE2902 adds a third, the CCCD descriptor 0x2902, where a client switches notifications on.
The SIG fixes the format: a signed 16-bit number in hundredths of a degree. loop() turns 23.45 °C into 2345, which is 0x0929. Bluetooth sends the low byte first, so b holds 29 then 09. setValue copies them into the value row.
Get connected
setup() ended with startAdvertising. Every advertising interval, between 20 ms and 10.24 s, the ESP32 sends a short advert on channels 37, 38 and 39. It carries the name Thermo and service 0x181A. The phone scans and hears it.
The user taps Connect. The phone answers an advert with a connect request, and the two now meet on a fixed rhythm: a connection event every 7.5 ms to 4 s. Then the phone discovers the table, learning each handle.
Exchange data
The phone sends an ATT (Attribute Protocol) read request for handle 0x0012. The server answers with the value bytes, 29 09. The phone puts them back together: 0x0929 is 2345, so 23.45 °C. It works, but the phone would have to keep asking.
To hear about changes, the phone writes 0x0001 into the CCCD, low byte first: 01 00. Bit 0 means notify me. The server answers with a write response, and from now on it may push the value.
2 seconds later the sensor reads 23.50 °C: 2350, or 0x092E, bytes 2E 09. setValue stores them and notify() sends them to the phone unasked. A notification gets no reply, so it is quick but unconfirmed.
An ATT message holds at most ATT_MTU bytes, 23 unless both sides agree on more. A notification spends 3 on its header: 1 opcode byte, 2 handle bytes. That leaves 20 for the value; ours needs 2. For a receipt, use indicate.
What happens
;; step 1/10 · roles
GATT Generic Attribute Profile: how BLE devices share data
server ESP32: holds the table, answers, can push values
client phone: sends requests, receives answers and pushes
GAP ESP32 = peripheral (advertises), phone = central (connects)
Board ESP32-DevKitC, Arduino-ESP32 core, its BLE library
// ESP32-DevKitC, Arduino-ESP32 core: a BLE temperature sensor
#include <BLEDevice.h>
#include <BLEServer.h>
#include <BLE2902.h>
#define ESS BLEUUID((uint16_t)0x181A) // Environmental Sensing
#define TEMP BLEUUID((uint16_t)0x2A6E) // Temperature
#define PROPS (BLECharacteristic::PROPERTY_READ | \
BLECharacteristic::PROPERTY_NOTIFY)
BLEServer *srv;
BLEService *s;
BLECharacteristic *c;
float readTempC() { return 23.45; } // stand-in: your sensor driver
void setup() {
BLEDevice::init("Thermo");
srv = BLEDevice::createServer();
s = srv->createService(ESS);
c = s->createCharacteristic(
TEMP, PROPS);
c->addDescriptor(new BLE2902()); // the CCCD, 0x2902
s->start();
BLEDevice::getAdvertising()->addServiceUUID(ESS);
BLEDevice::startAdvertising();
}
void loop() {
int16_t v = lround(readTempC() * 100); // 23.45 C -> 2345
uint8_t b[2] = { (uint8_t)v, (uint8_t)(v >> 8) }; // low byte first
c->setValue(b, 2);
c->notify(); // sent only if a client subscribed
delay(2000);
}
The handles 0x0010 to 0x0013 are made up for the picture. The BLE stack hands out real ones when the sketch runs, and the phone learns them during discovery. The service row's type is 0x2800 (Primary Service) and the declaration's is 0x2803. notify() only sends when a client has written 01 00 into the CCCD. With the newer NimBLE stack the library adds the CCCD by itself; with Bluedroid you add BLE2902 as here.