Skip to content

Commit fd05e5e

Browse files
committed
Make stream class MCU and OS agnostic
1 parent 853f812 commit fd05e5e

11 files changed

Lines changed: 788 additions & 620 deletions

File tree

examples/NimBLE_Stream_Client/NimBLE_Stream_Client.ino

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515

1616
#include <Arduino.h>
1717
#include <NimBLEDevice.h>
18-
#include <NimBLEStream.h>
1918

2019
// Service and Characteristic UUIDs (must match the server)
2120
#define SERVICE_UUID "6E400001-B5A3-F393-E0A9-E50E24DCCA9E"
@@ -64,7 +63,7 @@ class ClientCallbacks : public NimBLEClientCallbacks {
6463
void onDisconnect(NimBLEClient* pClient, int reason) override {
6564
Serial.printf("Disconnected from server, reason: %d\n", reason);
6665
connected = false;
67-
bleStream.deinit();
66+
bleStream.end();
6867

6968
// Restart scanning
7069
Serial.println("Restarting scan...");
@@ -118,20 +117,12 @@ bool connectToServer() {
118117
* Initialize the stream client with the remote characteristic
119118
* subscribeNotify=true means we'll receive notifications in the RX buffer
120119
*/
121-
if (!bleStream.init(pRemoteCharacteristic, true)) {
120+
if (!bleStream.begin(pRemoteCharacteristic, true)) {
122121
Serial.println("Failed to initialize BLE stream!");
123122
pClient->disconnect();
124123
return false;
125124
}
126125

127-
/** Start the stream (begins internal buffers and tasks) */
128-
if (!bleStream.begin()) {
129-
Serial.println("Failed to start BLE stream!");
130-
bleStream.deinit();
131-
pClient->disconnect();
132-
return false;
133-
}
134-
135126
Serial.println("BLE Stream initialized successfully!");
136127
connected = true;
137128
return true;

examples/NimBLE_Stream_Client/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ This example demonstrates how to use the `NimBLEStreamClient` class to connect t
77
- Uses Arduino Stream interface (print, println, read, available, etc.)
88
- Automatic server discovery and connection
99
- Bidirectional communication
10-
- Buffered TX/RX using FreeRTOS ring buffers
10+
- Buffered TX/RX using ring buffers
1111
- Automatic reconnection on disconnect
1212
- Similar usage to Serial communication
1313

examples/NimBLE_Stream_Echo/NimBLE_Stream_Echo.ino

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* A minimal example demonstrating NimBLEStreamServer.
55
* Echoes back any data received from BLE clients.
6-
*
6+
*
77
* This is the simplest way to use the NimBLE Stream interface,
88
* showing just the essential setup and read/write operations.
99
*
@@ -13,7 +13,6 @@
1313

1414
#include <Arduino.h>
1515
#include <NimBLEDevice.h>
16-
#include <NimBLEStream.h>
1716

1817
NimBLEStreamServer bleStream;
1918

@@ -25,12 +24,22 @@ void setup() {
2524
NimBLEDevice::init("BLE-Echo");
2625
NimBLEDevice::createServer();
2726

28-
// Initialize stream with default UUIDs, allow writes
29-
bleStream.init(NimBLEUUID(uint16_t(0xc0de)), // Service UUID
30-
NimBLEUUID(uint16_t(0xfeed)), // Characteristic UUID
31-
true, // canWrite
32-
false); // secure
33-
bleStream.begin();
27+
/**
28+
* Initialize the stream server with:
29+
* - Service UUID
30+
* - Characteristic UUID
31+
* - txBufSize: 1024 bytes for outgoing data (notifications)
32+
* - rxBufSize: 1024 bytes for incoming data (writes)
33+
* - secure: false (no encryption required - set to true for secure connections)
34+
*/
35+
if (!bleStream.begin(NimBLEUUID(uint16_t(0xc0de)), // Service UUID
36+
NimBLEUUID(uint16_t(0xfeed)), // Characteristic UUID
37+
1024, // txBufSize
38+
1024, // rxBufSize
39+
false)) { // secure
40+
Serial.println("Failed to initialize BLE stream");
41+
return;
42+
}
3443

3544
// Start advertising
3645
NimBLEDevice::getAdvertising()->start();
@@ -39,12 +48,12 @@ void setup() {
3948

4049
void loop() {
4150
// Echo any received data back to the client
42-
if (bleStream.hasSubscriber() && bleStream.available()) {
51+
if (bleStream.ready() && bleStream.available()) {
4352
Serial.print("Echo: ");
4453
while (bleStream.available()) {
4554
char c = bleStream.read();
4655
Serial.write(c);
47-
bleStream.write(c); // Echo back
56+
bleStream.write(c); // Echo back
4857
}
4958
Serial.println();
5059
}

examples/NimBLE_Stream_Server/NimBLE_Stream_Server.ino

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
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
@@ -13,7 +13,6 @@
1313

1414
#include <Arduino.h>
1515
#include <NimBLEDevice.h>
16-
#include <NimBLEStream.h>
1716

1817
// Create the stream server instance
1918
NimBLEStreamServer 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

9590
void 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
}

examples/NimBLE_Stream_Server/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ This example demonstrates how to use the `NimBLEStreamServer` class to create a
77
- Uses Arduino Stream interface (print, println, read, available, etc.)
88
- Automatic connection management
99
- Bidirectional communication
10-
- Buffered TX/RX using FreeRTOS ring buffers
10+
- Buffered TX/RX using ring buffers
1111
- Similar usage to Serial communication
1212

1313
## How it Works

examples/STREAM_EXAMPLES.md

Lines changed: 0 additions & 145 deletions
This file was deleted.

src/NimBLEDevice.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ class NimBLEDevice {
245245
# endif
246246

247247
# ifdef ESP_PLATFORM
248-
# if NIMBLE_CPP_SCAN_DUPL_ENABLED
248+
# if NIMBLE_CPP_SCAN_DUPL_ENABLED
249249
static uint16_t m_scanDuplicateSize;
250250
static uint8_t m_scanFilterMode;
251251
static uint16_t m_scanDuplicateResetTime;
@@ -306,6 +306,7 @@ class NimBLEDevice {
306306

307307
# if MYNEWT_VAL(BLE_ROLE_CENTRAL) || MYNEWT_VAL(BLE_ROLE_PERIPHERAL)
308308
# include "NimBLEConnInfo.h"
309+
# include "NimBLEStream.h"
309310
# endif
310311

311312
# include "NimBLEAddress.h"

0 commit comments

Comments
 (0)