Phone nearby
BLE GATT Server
A phone finds and controls the device. Starting point for local setup.
Phone nearby
Phone connects in
ESP32
Official path: examples/bluetooth/bluedroid/ble/gatt_server
01 Overview
The device acts as a GATT server and advertises over BLE. A phone, as a GATT client, can connect and read/write characteristics for local control.
Solves device configuration in router-less environments, e.g., smart lights, locks, sensors. The phone app connects directly without internet.
Use for app-based local setup, device debugging, or private protocol control. Not for public internet control due to BLE's short range.
Espressif recommends the service-table example for production to simplify code and improve maintainability.
This example is a foundation for learning BLE GATT: services, characteristics, notifications.
02 Hardware
- Module: BLE-capable ESP32 module, e.g., ESP32-WROOM-32. Note: ESP32-S2 lacks BLE.
- Button: Common to add a pairing button to trigger advertising or enter pairing mode.
- Status LED: Indicates connection or advertising state for debugging.
- Power: Advertising consumes energy; battery products should advertise only on button press.
- Antenna: Keep antenna area clear of metal for optimal range.
- Connectors: Reserve I2C/SPI for external sensors if needed.
03 Software flow
1. Initialize NVS, controller, and Bluedroid stack.
2. Create GATT service, characteristics, and CCCD; register callbacks.
3. Start advertising with name and UUID.
4. Wait for phone connection; handle connect/disconnect events.
5. Notifications only after client writes CCCD.
6. Handle read/write requests and execute control logic.
7. Change device name and UUIDs before shipping to avoid conflicts.
04 Core points
- Notify is not on by default: Client must write CCCD to enable.
- Demo UUIDs cannot ship: Use custom UUIDs to avoid clashes.
- Custom work: Protocol, bonding, advertising interval, Wi-Fi provisioning combo.
- Power: Advertising interval affects consumption; trade-off needed.
- Security: Enable pairing and bonding if encryption is required.