Skip to content

Commit 5af0a57

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

9 files changed

Lines changed: 763 additions & 613 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_Echo/NimBLE_Stream_Echo.ino

Lines changed: 12 additions & 9 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

@@ -26,11 +25,15 @@ void setup() {
2625
NimBLEDevice::createServer();
2726

2827
// 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();
28+
if (!bleStream.begin(NimBLEUUID(uint16_t(0xc0de)), // Service UUID
29+
NimBLEUUID(uint16_t(0xfeed)), // Characteristic UUID
30+
1024, // txBufSize
31+
1024, // rxBufSize
32+
true, // canWrite
33+
false)) { // secure
34+
Serial.println("Failed to initialize BLE stream");
35+
return;
36+
}
3437

3538
// Start advertising
3639
NimBLEDevice::getAdvertising()->start();
@@ -39,12 +42,12 @@ void setup() {
3942

4043
void loop() {
4144
// Echo any received data back to the client
42-
if (bleStream.hasSubscriber() && bleStream.available()) {
45+
if (bleStream.ready() && bleStream.available()) {
4346
Serial.print("Echo: ");
4447
while (bleStream.available()) {
4548
char c = bleStream.read();
4649
Serial.write(c);
47-
bleStream.write(c); // Echo back
50+
bleStream.write(c); // Echo back
4851
}
4952
Serial.println();
5053
}

examples/NimBLE_Stream_Server/NimBLE_Stream_Server.ino

Lines changed: 19 additions & 25 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
60+
* - Characteristic UUID
6261
* - canWrite: true (allows clients to write data to us)
6362
* - secure: false (no encryption required - set to true for secure connections)
6463
*/
65-
if (!bleStream.init(NimBLEUUID(SERVICE_UUID),
66-
NimBLEUUID(CHARACTERISTIC_UUID),
67-
true, // canWrite - allow receiving data
68-
false)) // secure
64+
if (!bleStream.begin(NimBLEUUID(SERVICE_UUID),
65+
NimBLEUUID(CHARACTERISTIC_UUID),
66+
1024, // txBufSize
67+
1024, // rxBufSize
68+
true, // canWrite - allow receiving data
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/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)