BLESerial allows serial communication over a BLE connection .
It implements the Nordic UART Service (NUS) as a server on a micro controller, providing commands like:
BLESerial ble;
ble.println("Hello");
n=ble.available();
ble.read(buffer,n);
It attempts to adapt for maximum throughput, low power usage or long distance communication.
The library provides a server implementation as it is designed to work with programs such as:
There are similar implementations from other authors (senseshift, afpineda).
A throughput of more than 100k bytes/s and a latency of 10..20 ms was achieved.
Installation occurs through the Arduino library manager.
- NimBLE-Arduino
- RingBuffer (provided)
Minimal example demonstrating setup, polling versus task (ESP32) mode, command parsing, and date transmission and receiving:
#include <Arduino.h>
#include "BLESerial.h"
#include "Linereader.h"
BLESerial ble;
LineReader<128> lr;
char line[128];
const char helpmsg[] = "Commands: ?=help, stats, echo <text>";
void setup() {
Serial.begin(115200);
while (!Serial) { /* wait for USB serial */ }
// Security::None | JustWorks | PasskeyDisplay
// Mode::Fast | LowPower | LongRange | Balanced
ble.begin(BLESerial::Mode::Fast, "BLESerialDevice", BLESerial::Security::None);
#ifdef ARDUINO_ARCH_ESP32
ble.setPumpMode(BLESerial::PumpMode::Task); // background TX pump
#endif
Serial.println("BLESerial demo started.");
}
void loop() {
#ifndef ARDUINO_ARCH_ESP32
ble.update(); // required in Polling mode
#endif
// Parse incoming lines from BLE
if (lr.poll(ble, line, sizeof(line))) {
if (strcasecmp(line, "?") == 0) {
ble.println(helpmsg);
Serial.println(helpmsg);
} else if (strcasecmp(line, "stats") == 0) {
ble.printStats(); // shows on Serial port
} else if (strncasecmp(line, "echo ", 5) == 0) {
ble.println(line + 5);
} else {
ble.println("Unknown command. Type ? for help.");
}
}
}- BLESerial_minimal (simple echo program)
- BLESerial_demo (simple program listed above)
- BLESerial_comprehensive (generates data for performance measurements)
Urs Utzinger 2025
ChatGPT (OpenAI)
See LICENSE.