°CESP32b[0]b[1]Service 10Char decl 11Value 12CCCD 13373839-- °C1s = srv->createService(ESS);2c = s->createCharacteristic(3 TEMP, PROPS);4c->addDescriptor(new BLE2902());5BLEDevice::startAdvertising();6c->setValue(b, 2);7c->notify();opcodehandlevalue23.45 °C290929 09ADVADVADVCONNECT0A 12 000B 29 0912 13 00 01 001323.50 °C2E092E 091B 12 00 2E 09
Back: the ESP32 board with its GATT table, the advertising channels and the phoneFront: the sketch's key lines and the bytes of the current ATT messageTokens: readings, bytes, adverts and ATT messagesAn ESP32 runs a GATT server: a table holding the Environmental Sensing service (0x181A) with one Temperature characteristic (0x2A6E): a declaration, the value and a CCCD. 23.45 °C is stored as 2345, sent low byte first as 29 09. The phone finds the board by its adverts on channels 37, 38 and 39, connects, reads the value, writes 01 00 into the CCCD, and then receives each new value as a notification (2E 09 for 23.50 °C).

Build the server

  1. 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.

Get connected

Exchange data

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.