esp-nimble-cpp

2.5.0
esp-nimble-cpp is a high-performance, thread-safe C++ wrapper for the Apache NimBLE Bluetooth Low Energy stack, specifically optimized for the ESP32 series. It provides a resource-efficient alternative to the default Bluedroid-based library, significantly reducing RAM and Flash consumption while maintaining API compatibility with popular ESP32 BLE libraries.

Features

  • Thread-safe implementation allowing characteristic and descriptor updates from any application thread.

  • Significant resource optimization, consuming approximately 100kB less RAM and 50% less Flash than Bluedroid-based alternatives.

  • Support for Bluetooth 5.0 features including Extended Advertising, 2M PHY, and Coded PHY.

  • Comprehensive C++ class wrappers for BLE Device, Server, Client, Scan, and Advertising management.

  • Native support for a wide range of Espressif targets including ESP32, S3, C2, C3, C5, C6, C61, H2, and P4.

  • Synchronous and asynchronous connection options for BLE clients to prevent blocking application tasks.

  • Automated 0x2902 descriptor management for handling client notification and indication subscriptions.

  • Advanced security implementation supporting ‘Just Works’, Passkey Entry, and Numeric Comparison authentication.

  • L2CAP infrastructure support for implementing connection-oriented channels.

  • Customizable logging system with configurable verbosity levels and color-coded debug output via Kconfig.

  • Dynamic attribute management allowing for the removal of services and characteristics at runtime.

  • Efficient memory handling using the NimBLEAttValue class to minimize heap reallocations.

  • Support for directed advertising and customizable discoverability modes.

  • Integrated whitelist and bond management APIs for device filtering and security persistence.

  • Configurable MTU exchange and Data Length Extension (DLE) support for optimized throughput.

  • Compatibility layer for existing nkolban/esp32-snippets BLE codebases to ease migration.

The esp-nimble-cpp library is architected as an object-oriented C++ abstraction layer over the Apache NimBLE stack, which is the preferred BLE host for resource-constrained ESP32 applications. The design follows a singleton pattern for the core device management via NimBLEDevice, while utilizing a callback-driven architecture to handle asynchronous BLE events. By wrapping the underlying C-based NimBLE API, the library provides a structured hierarchy of Services, Characteristics, and Descriptors that mirror the GATT profile structure.

At its core, the library manages the complex state machine of the BLE stack, including scanning, advertising, and connection management. It introduces the NimBLEAttValue class to handle attribute data efficiently, reducing the overhead typically associated with string-based buffers. The library is designed to be thread-safe, utilizing FreeRTOS primitives to ensure that GATT operations can be performed safely across different system tasks without corrupting the stack state.

Core Components:

  • NimBLEDevice: The central management hub for initializing the stack, managing security, and creating clients or servers.
  • NimBLEServer: Manages the local GATT database and handles connections from remote central devices.
  • NimBLEClient: Facilitates connections to remote peripherals, service discovery, and attribute manipulation.
  • NimBLEScan: Handles the discovery of nearby advertising devices with configurable filters.
  • NimBLEAdvertising: Manages the broadcasting of advertisement and scan response data.

Use Cases

This library is ideal for:

  • IoT Sensor Nodes: Implementing low-power peripherals that transmit sensor data to smartphones or gateways while requiring a minimal memory footprint.
  • Human Interface Devices (HID): Creating BLE keyboards, mice, or game controllers using the specialized NimBLEHIDDevice class.
  • Industrial Gateways: Developing central devices that must maintain multiple simultaneous connections to various BLE peripherals.
  • Battery-Powered Wearables: Applications where reducing Flash and RAM usage directly translates to better power efficiency and more room for application logic.

Getting Started

To begin development, include NimBLEDevice.h in your project. Initialization is performed by calling NimBLEDevice::init("Device Name") within your app_main or setup function. For ESP-IDF users, the library requires Bluetooth to be enabled in menuconfig, with the ‘Bluetooth host’ set to NimBLE. Developers migrating from older libraries should consult the 1.x to 2.x Migration Guide and the New User Guide for detailed implementation patterns and API changes. Comprehensive class documentation is available at the official API reference.

Related Projects (21)

View All 21 Projects →
M5_Crystal

M5_Crystal

M5_Crystal is a modular firmware for M5Stack devices like the M5StickCPlus2 and Cardputer, providing a suite of tools for Wi-Fi, NFC, and Bluetooth interaction. It utilizes the M5Unified and M5GFX libraries for hardware abstraction and is built on a custom wrapper system designed for rapid feature development.

freertos h2zero-esp-nimble-cpplittlefsu8g2
GhostBLE

GhostBLE

GhostBLE is a portable BLE privacy scanner and wardriving application designed for the M5Stack ecosystem, including the Cardputer and StickC series. Leveraging the NimBLE-Arduino library on the ESP32 platform, it provides real-time analysis of Bluetooth Low Energy devices, privacy posture heuristics, and GPS-tagged logging for security research and education.

freertos nimbleh2zero-esp-nimble-cpp

POSEIDON

A keyboard-focused pentesting firmware for the M5Stack Cardputer-Adv (ESP32-S3) offering over 90 features across WiFi, BLE, sub-GHz, and LoRa. It utilizes FreeRTOS and the NimBLE stack to coordinate complex wireless attacks, including 5 GHz deauthentication and Zigbee sniffing via a companion ESP32-C5 node.

freertos h2zero-esp-nimble-cppnimblelwip

Flock-Detector 3.0

Flock-Detector 3.0 is a specialized surveillance detection system designed for the Seeed Studio XIAO ESP32-S3 to identify and log WiFi and BLE signatures from ALPR cameras and gunshot detectors. The firmware utilizes FreeRTOS for dual-core task management, pairing WiFi promiscuous mode and NimBLE-based BLE scanning with real-time GPS tagging. It features a weighted confidence scoring engine, persistent session storage via LittleFS, and an OLED-based interface for field use.

freertos h2zero-esp-nimble-cpplittlefs
ESP-HACK FW

ESP-HACK FW

ESP-HACK is a versatile wireless research and pentesting firmware for the ESP32 platform, targeting Sub-GHz, Bluetooth, and Infrared protocols. It utilizes the Arduino framework and NimBLE stack to provide tools for signal analysis, protocol exploration, and GPIO integrations. The project is designed for use with custom hardware including CC1101 transceivers and OLED displays.

freertos h2zero-esp-nimble-cppnimble

HijelHID BLEKeyboard

A comprehensive Bluetooth Low Energy (BLE) HID keyboard library for the ESP32, built on the NimBLE-Arduino stack for high efficiency. It supports standard keyboard layouts, media keys, and advanced power management features including light and deep sleep. The library is designed for creating physical or emulated HID devices compatible with iOS, Android, macOS, Windows, and Linux.

freertos h2zero-esp-nimble-cppnimble

Arduino Serial BLE

A library for ESP32 Arduino that implements the Nordic UART Service (NUS) to provide a HardwareSerial-compatible BLE interface. It supports both the standard ESP32 BLE stack and the memory-efficient NimBLE-Arduino library, while offering optional integration with the Embedded Template Library (ETL) for deterministic memory management.

freertos nimbleh2zero-esp-nimble-cpp
Gaggimate

Gaggimate

Gaggimate is an ESP32-based smart control upgrade for Gaggia espresso machines, providing a custom UI for temperature monitoring and pump control. It leverages the FreeRTOS-based ESP32 platform using LVGL and TFT_eSPI for its display interface, and NimBLE-Arduino for wireless connectivity.

freertos h2zero-esp-nimble-cpplvgltft-espi