33 *
44 * Demonstrates using NimBLEStreamServer to create a BLE GATT server
55 * that behaves like a serial port using the Arduino Stream interface.
6- *
7- * This allows you to use familiar methods like print(), println(),
6+ *
7+ * This allows you to use familiar methods like print(), println(),
88 * read(), and available() over BLE, similar to how you would use Serial.
99 *
1010 * Created: November 2025
1313
1414#include < Arduino.h>
1515#include < NimBLEDevice.h>
16- #include < NimBLEStream.h>
1716
1817// Create the stream server instance
1918NimBLEStreamServer bleStream;
@@ -48,8 +47,8 @@ void setup() {
4847 /* * Initialize NimBLE and set the device name */
4948 NimBLEDevice::init (" NimBLE-Stream" );
5049
51- /* *
52- * Create the BLE server and set callbacks
50+ /* *
51+ * Create the BLE server and set callbacks
5352 * Note: The stream will create its own service and characteristic
5453 */
5554 NimBLEServer* pServer = NimBLEDevice::createServer ();
@@ -58,26 +57,22 @@ void setup() {
5857 /* *
5958 * Initialize the stream server with:
6059 * - Service UUID
61- * - Characteristic UUID
62- * - canWrite: true (allows clients to write data to us)
60+ * - Characteristic UUID
61+ * - txBufSize: 1024 bytes for outgoing data (notifications)
62+ * - rxBufSize: 1024 bytes for incoming data (writes)
6363 * - secure: false (no encryption required - set to true for secure connections)
6464 */
65- if (!bleStream.init (NimBLEUUID (SERVICE_UUID),
66- NimBLEUUID (CHARACTERISTIC_UUID),
67- true , // canWrite - allow receiving data
68- false )) // secure
65+ if (!bleStream.begin (NimBLEUUID (SERVICE_UUID),
66+ NimBLEUUID (CHARACTERISTIC_UUID),
67+ 1024 , // txBufSize
68+ 1024 , // rxBufSize
69+ false )) // secure
6970 {
7071 Serial.println (" Failed to initialize BLE stream!" );
7172 return ;
7273 }
7374
74- /* * Start the stream (begins internal buffers and tasks) */
75- if (!bleStream.begin ()) {
76- Serial.println (" Failed to start BLE stream!" );
77- return ;
78- }
79-
80- /* *
75+ /* *
8176 * Create advertising instance and add service UUID
8277 * This makes the stream service discoverable
8378 */
@@ -94,32 +89,31 @@ void setup() {
9489
9590void loop () {
9691 // Check if a client is subscribed (connected and listening)
97- if (bleStream.hasSubscriber ()) {
98-
92+ if (bleStream.ready ()) {
9993 // Send a message every 2 seconds using Stream methods
10094 static unsigned long lastSend = 0 ;
10195 if (millis () - lastSend > 2000 ) {
10296 lastSend = millis ();
103-
97+
10498 // Using familiar Serial-like methods!
10599 bleStream.print (" Hello from BLE Server! Time: " );
106100 bleStream.println (millis ());
107-
101+
108102 // You can also use printf
109103 bleStream.printf (" Free heap: %d bytes\n " , ESP.getFreeHeap ());
110-
104+
111105 Serial.println (" Sent data to client via BLE stream" );
112106 }
113107
114108 // Check if we received any data from the client
115109 if (bleStream.available ()) {
116110 Serial.print (" Received from client: " );
117-
111+
118112 // Read all available data (just like Serial.read())
119113 while (bleStream.available ()) {
120114 char c = bleStream.read ();
121- Serial.write (c); // Echo to Serial
122- bleStream.write (c); // Echo back to BLE client
115+ Serial.write (c); // Echo to Serial
116+ bleStream.write (c); // Echo back to BLE client
123117 }
124118 Serial.println ();
125119 }
0 commit comments