Skip to content

Commit 75d3f9c

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

8 files changed

Lines changed: 462 additions & 394 deletions

File tree

examples/NimBLE_Stream_Client/NimBLE_Stream_Client.ino

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -124,14 +124,6 @@ bool connectToServer() {
124124
return false;
125125
}
126126

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-
135127
Serial.println("BLE Stream initialized successfully!");
136128
connected = true;
137129
return true;

examples/NimBLE_Stream_Echo/NimBLE_Stream_Echo.ino

Lines changed: 9 additions & 8 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
*
@@ -26,11 +26,12 @@ void setup() {
2626
NimBLEDevice::createServer();
2727

2828
// 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();
29+
bleStream.init(NimBLEUUID(uint16_t(0xc0de)), // Service UUID
30+
NimBLEUUID(uint16_t(0xfeed)), // Characteristic UUID
31+
1024, // txBufSize
32+
1024, // rxBufSize
33+
true, // canWrite
34+
false); // secure
3435

3536
// Start advertising
3637
NimBLEDevice::getAdvertising()->start();
@@ -39,12 +40,12 @@ void setup() {
3940

4041
void loop() {
4142
// Echo any received data back to the client
42-
if (bleStream.hasSubscriber() && bleStream.available()) {
43+
if (bleStream.ready() && bleStream.available()) {
4344
Serial.print("Echo: ");
4445
while (bleStream.available()) {
4546
char c = bleStream.read();
4647
Serial.write(c);
47-
bleStream.write(c); // Echo back
48+
bleStream.write(c); // Echo back
4849
}
4950
Serial.println();
5051
}

examples/NimBLE_Stream_Server/NimBLE_Stream_Server.ino

Lines changed: 16 additions & 21 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
@@ -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

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

examples/STREAM_EXAMPLES.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,6 @@ Both server and client support configuration before calling `begin()`:
110110
```cpp
111111
bleStream.setTxBufSize(2048); // TX buffer size
112112
bleStream.setRxBufSize(2048); // RX buffer size
113-
bleStream.setTxTaskStackSize(4096); // Task stack size
114-
bleStream.setTxTaskPriority(1); // Task priority
115113
```
116114

117115
## Compatibility

0 commit comments

Comments
 (0)