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
@@ -48,8 +48,8 @@ void setup() {
4848 /* * Initialize NimBLE and set the device name */
4949 NimBLEDevice::init (" NimBLE-Stream" );
5050
51- /* *
52- * Create the BLE server and set callbacks
51+ /* *
52+ * Create the BLE server and set callbacks
5353 * Note: The stream will create its own service and characteristic
5454 */
5555 NimBLEServer* pServer = NimBLEDevice::createServer ();
@@ -58,26 +58,22 @@ void setup() {
5858 /* *
5959 * Initialize the stream server with:
6060 * - Service UUID
61- * - Characteristic UUID
61+ * - Characteristic UUID
6262 * - canWrite: true (allows clients to write data to us)
6363 * - secure: false (no encryption required - set to true for secure connections)
6464 */
65- if (!bleStream.init (NimBLEUUID (SERVICE_UUID),
65+ if (!bleStream.init (NimBLEUUID (SERVICE_UUID),
6666 NimBLEUUID (CHARACTERISTIC_UUID),
67+ 1024 , // txBufSize
68+ 1024 , // rxBufSize
6769 true , // canWrite - allow receiving data
6870 false )) // secure
6971 {
7072 Serial.println (" Failed to initialize BLE stream!" );
7173 return ;
7274 }
7375
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- /* *
76+ /* *
8177 * Create advertising instance and add service UUID
8278 * This makes the stream service discoverable
8379 */
@@ -94,32 +90,31 @@ void setup() {
9490
9591void loop () {
9692 // Check if a client is subscribed (connected and listening)
97- if (bleStream.hasSubscriber ()) {
98-
93+ if (bleStream.ready ()) {
9994 // Send a message every 2 seconds using Stream methods
10095 static unsigned long lastSend = 0 ;
10196 if (millis () - lastSend > 2000 ) {
10297 lastSend = millis ();
103-
98+
10499 // Using familiar Serial-like methods!
105100 bleStream.print (" Hello from BLE Server! Time: " );
106101 bleStream.println (millis ());
107-
102+
108103 // You can also use printf
109104 bleStream.printf (" Free heap: %d bytes\n " , ESP.getFreeHeap ());
110-
105+
111106 Serial.println (" Sent data to client via BLE stream" );
112107 }
113108
114109 // Check if we received any data from the client
115110 if (bleStream.available ()) {
116111 Serial.print (" Received from client: " );
117-
112+
118113 // Read all available data (just like Serial.read())
119114 while (bleStream.available ()) {
120115 char c = bleStream.read ();
121- Serial.write (c); // Echo to Serial
122- bleStream.write (c); // Echo back to BLE client
116+ Serial.write (c); // Echo to Serial
117+ bleStream.write (c); // Echo back to BLE client
123118 }
124119 Serial.println ();
125120 }
0 commit comments