Phone nearby

BLE GATT Client (Gateway)

ESP32 acts as a BLE gateway, scanning and connecting to sensors or wearables, reading data and reporting it.

Phone nearby Device connects out ESP32

Official path: examples/bluetooth/bluedroid/ble/gatt_client

01 Overview

This example demonstrates the ESP32 as a BLE GATT client (Central): scanning for BLE peripherals, filtering by name or service UUID, establishing a connection, discovering services, enabling notifications, and continuously receiving data.

Typical use case is an IoT gateway: the ESP32 collects data from multiple low-power sensors (e.g., temperature/humidity, heart rate, fitness bands) and uploads it to the cloud via Wi-Fi or Ethernet.

Unlike the GATT Server example, the ESP32 actively initiates connections rather than waiting to be connected. If your product requires a phone to connect directly to the ESP32, use Server mode instead.

The example is based on the Bluedroid stack, suitable for complex GATT operations. For low-power or simpler connections, consider NimBLE or ESP-BLE-MESH.

This example does not cover specific sensor protocols; it provides a generic GATT client framework. Actual products must customize service discovery and data parsing according to the sensor's datasheet.

02 Hardware

  • RF layout: Same antenna matching and PCB layout requirements as the Server example to ensure BLE sensitivity.
  • Antenna: Use PCB or external antenna, keep clearance area, avoid metal shielding.
  • Coexistence: If Wi-Fi and BLE are used simultaneously, evaluate coexistence performance; use external coexistence pins or software arbitration if needed.
  • Power: Multi-peripheral connections cause current spikes; ensure sufficient decoupling capacitors and regulator headroom.
  • Connectors: For external sensors, check I2C/UART level matching; see schematic.
  • Crystal: Use a 32.768kHz crystal for low-power modes, otherwise Bluetooth clock may drift.
  • Debug interface: Keep UART0 for logging to facilitate field debugging.

03 Software flow

1. Initialize NVS, Wi-Fi (if uplink needed), and the Bluedroid stack.

2. Configure scan parameters (window, interval, filters) and scan for target devices by name or UUID.

3. Stop scanning when target found, initiate connection (with timeout).

4. After connection, discover primary services, then characteristics and descriptors.

5. Enable notifications (write to CCCD) and register callbacks to receive data.

6. In the main loop, process received data, parse, and report (e.g., MQTT).

7. Monitor connection state; on disconnect, reconnect with policy (exponential backoff).

8. Handle pairing/bonding requests, save keys for fast reconnection.

04 Core points

  • Strict filters: On site, multiple identical devices may exist; filter by MAC or unique UUID to avoid connecting to the wrong one.
  • MTU negotiation: Default MTU is small; for large data transfers, negotiate a larger MTU (e.g., 247 bytes).
  • Coexistence issues: Wi-Fi and BLE simultaneously may cause packet loss; test and optimize coexistence strategy.
  • Power management: Gateways are usually mains-powered, but if battery-powered, optimize scan interval and connection parameters.
  • Security: If data is sensitive, enable pairing and encryption to prevent eavesdropping.
  • Customization: Can be extended to multi-connection, data caching, OTA, etc.

中文

手机近场

BLE GATT 客户端(网关)

ESP32 作为 BLE 网关,扫描并连接传感器或手环,读取数据并上报。

手机近场 去连别人 ESP32

官方路径: examples/bluetooth/bluedroid/ble/gatt_client

01 项目概述

本示例演示 ESP32 作为 BLE GATT 客户端(Central)的工作流程:扫描周围的 BLE 外设,按名称或服务 UUID 过滤,建立连接,发现服务,使能通知,然后持续接收数据。

典型应用是物联网网关:ESP32 收集多个低功耗传感器(如温湿度计、心率带、手环)的数据,再通过 Wi-Fi 或以太网上传到云端。

与 GATT Server 示例相反,本示例中 ESP32 主动发起连接,而不是等待被连接。如果产品需要手机直接连接 ESP32,则应使用 Server 模式。

示例代码基于 Bluedroid 协议栈,适合需要复杂 GATT 操作的场景。若追求低功耗或简单连接,可考虑 NimBLE 或 ESP-BLE-MESH。

本示例不涉及具体传感器协议,只提供通用 GATT 客户端框架,实际产品需根据传感器手册定制服务发现和数据解析。

02 项目硬件描述

  • 射频布局: 与 Server 示例相同的天线匹配和 PCB 布局要求,确保 BLE 灵敏度。
  • 天线: 使用 PCB 天线或外置天线,注意净空区,避免金属遮挡。
  • 共存: 若同时使用 Wi-Fi 和 BLE,需评估共存性能,必要时使用外部共存引脚或软件仲裁。
  • 电源: 多从机连接时电流波动大,需足够的去耦电容和稳压余量。
  • 连接器: 如需外接传感器,注意 I2C/UART 电平匹配,见原理图。
  • 晶振: 使用 32.768kHz 晶振以支持低功耗模式,否则蓝牙时钟可能漂移。
  • 调试接口: 保留 UART0 日志输出,便于现场调试。

03 项目软件流程描述

1. 初始化 NVS、Wi-Fi(若需上报)和 Bluedroid 协议栈。

2. 配置扫描参数(窗口、间隔、过滤条件),按名称或 UUID 扫描目标设备。

3. 扫描到目标后停止扫描,发起连接(可设置连接超时)。

4. 连接成功后,发现主要服务(GATT Discover Services),再发现特征和描述符。

5. 使能通知(写入 CCCD),注册回调接收数据。

6. 主循环中处理接收到的数据,解析并上报(如 MQTT)。

7. 监控连接状态,若断开则按策略重连(指数退避)。

8. 处理配对/绑定请求,保存密钥以快速重连。

04 项目核心点

  • 过滤条件必须严格: 现场可能有多个同型号设备,需按 MAC 地址或唯一 UUID 过滤,避免连错。
  • MTU 协商: 默认 MTU 较小,若传输大数据需协商更大 MTU(如 247 字节)。
  • 共存问题: Wi-Fi 和 BLE 同时工作可能导致丢包,需测试并优化共存策略。
  • 功耗管理: 网关通常常供电,但若电池供电,需优化扫描间隔和连接参数。
  • 安全: 若数据敏感,需启用配对和加密,防止窃听。
  • 可定制: 可扩展为多连接、数据缓存、OTA 升级等。