Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 2 additions & 11 deletions examples/NimBLE_Stream_Client/NimBLE_Stream_Client.ino
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

#include <Arduino.h>
#include <NimBLEDevice.h>
#include <NimBLEStream.h>

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

// Restart scanning
Serial.println("Restarting scan...");
Expand Down Expand Up @@ -118,20 +117,12 @@ bool connectToServer() {
* Initialize the stream client with the remote characteristic
* subscribeNotify=true means we'll receive notifications in the RX buffer
*/
if (!bleStream.init(pRemoteCharacteristic, true)) {
if (!bleStream.begin(pRemoteCharacteristic, true)) {
Serial.println("Failed to initialize BLE stream!");
pClient->disconnect();
return false;
}

/** Start the stream (begins internal buffers and tasks) */
if (!bleStream.begin()) {
Serial.println("Failed to start BLE stream!");
bleStream.deinit();
pClient->disconnect();
return false;
}

Serial.println("BLE Stream initialized successfully!");
connected = true;
return true;
Expand Down
2 changes: 1 addition & 1 deletion examples/NimBLE_Stream_Client/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ This example demonstrates how to use the `NimBLEStreamClient` class to connect t
- Uses Arduino Stream interface (print, println, read, available, etc.)
- Automatic server discovery and connection
- Bidirectional communication
- Buffered TX/RX using FreeRTOS ring buffers
- Buffered TX/RX using ring buffers
- Automatic reconnection on disconnect
- Similar usage to Serial communication

Expand Down
29 changes: 19 additions & 10 deletions examples/NimBLE_Stream_Echo/NimBLE_Stream_Echo.ino
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*
* A minimal example demonstrating NimBLEStreamServer.
* Echoes back any data received from BLE clients.
*
*
* This is the simplest way to use the NimBLE Stream interface,
* showing just the essential setup and read/write operations.
*
Expand All @@ -13,7 +13,6 @@

#include <Arduino.h>
#include <NimBLEDevice.h>
#include <NimBLEStream.h>

NimBLEStreamServer bleStream;

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

// Initialize stream with default UUIDs, allow writes
bleStream.init(NimBLEUUID(uint16_t(0xc0de)), // Service UUID
NimBLEUUID(uint16_t(0xfeed)), // Characteristic UUID
true, // canWrite
false); // secure
bleStream.begin();
/**
* Initialize the stream server with:
* - Service UUID
* - Characteristic UUID
* - txBufSize: 1024 bytes for outgoing data (notifications)
* - rxBufSize: 1024 bytes for incoming data (writes)
* - secure: false (no encryption required - set to true for secure connections)
*/
if (!bleStream.begin(NimBLEUUID(uint16_t(0xc0de)), // Service UUID
NimBLEUUID(uint16_t(0xfeed)), // Characteristic UUID
1024, // txBufSize
1024, // rxBufSize
false)) { // secure
Serial.println("Failed to initialize BLE stream");
return;
}

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

void loop() {
// Echo any received data back to the client
if (bleStream.hasSubscriber() && bleStream.available()) {
if (bleStream.ready() && bleStream.available()) {
Serial.print("Echo: ");
while (bleStream.available()) {
char c = bleStream.read();
Serial.write(c);
bleStream.write(c); // Echo back
bleStream.write(c); // Echo back
}
Serial.println();
}
Expand Down
46 changes: 20 additions & 26 deletions examples/NimBLE_Stream_Server/NimBLE_Stream_Server.ino
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
*
* Demonstrates using NimBLEStreamServer to create a BLE GATT server
* that behaves like a serial port using the Arduino Stream interface.
*
* This allows you to use familiar methods like print(), println(),
*
* This allows you to use familiar methods like print(), println(),
* read(), and available() over BLE, similar to how you would use Serial.
*
* Created: November 2025
Expand All @@ -13,7 +13,6 @@

#include <Arduino.h>
#include <NimBLEDevice.h>
#include <NimBLEStream.h>

// Create the stream server instance
NimBLEStreamServer bleStream;
Expand Down Expand Up @@ -48,8 +47,8 @@ void setup() {
/** Initialize NimBLE and set the device name */
NimBLEDevice::init("NimBLE-Stream");

/**
* Create the BLE server and set callbacks
/**
* Create the BLE server and set callbacks
* Note: The stream will create its own service and characteristic
*/
NimBLEServer* pServer = NimBLEDevice::createServer();
Expand All @@ -58,26 +57,22 @@ void setup() {
/**
* Initialize the stream server with:
* - Service UUID
* - Characteristic UUID
* - canWrite: true (allows clients to write data to us)
* - Characteristic UUID
* - txBufSize: 1024 bytes for outgoing data (notifications)
* - rxBufSize: 1024 bytes for incoming data (writes)
* - secure: false (no encryption required - set to true for secure connections)
*/
if (!bleStream.init(NimBLEUUID(SERVICE_UUID),
NimBLEUUID(CHARACTERISTIC_UUID),
true, // canWrite - allow receiving data
false)) // secure
if (!bleStream.begin(NimBLEUUID(SERVICE_UUID),
NimBLEUUID(CHARACTERISTIC_UUID),
1024, // txBufSize
1024, // rxBufSize
false)) // secure
{
Serial.println("Failed to initialize BLE stream!");
return;
}

/** Start the stream (begins internal buffers and tasks) */
if (!bleStream.begin()) {
Serial.println("Failed to start BLE stream!");
return;
}

/**
/**
* Create advertising instance and add service UUID
* This makes the stream service discoverable
*/
Expand All @@ -94,32 +89,31 @@ void setup() {

void loop() {
// Check if a client is subscribed (connected and listening)
if (bleStream.hasSubscriber()) {

if (bleStream.ready()) {
// Send a message every 2 seconds using Stream methods
static unsigned long lastSend = 0;
if (millis() - lastSend > 2000) {
lastSend = millis();

// Using familiar Serial-like methods!
bleStream.print("Hello from BLE Server! Time: ");
bleStream.println(millis());

// You can also use printf
bleStream.printf("Free heap: %d bytes\n", ESP.getFreeHeap());

Serial.println("Sent data to client via BLE stream");
}

// Check if we received any data from the client
if (bleStream.available()) {
Serial.print("Received from client: ");

// Read all available data (just like Serial.read())
while (bleStream.available()) {
char c = bleStream.read();
Serial.write(c); // Echo to Serial
bleStream.write(c); // Echo back to BLE client
Serial.write(c); // Echo to Serial
bleStream.write(c); // Echo back to BLE client
}
Serial.println();
}
Expand Down
2 changes: 1 addition & 1 deletion examples/NimBLE_Stream_Server/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ This example demonstrates how to use the `NimBLEStreamServer` class to create a
- Uses Arduino Stream interface (print, println, read, available, etc.)
- Automatic connection management
- Bidirectional communication
- Buffered TX/RX using FreeRTOS ring buffers
- Buffered TX/RX using ring buffers
- Similar usage to Serial communication

## How it Works
Expand Down
145 changes: 0 additions & 145 deletions examples/STREAM_EXAMPLES.md

This file was deleted.

3 changes: 2 additions & 1 deletion src/NimBLEDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ class NimBLEDevice {
# endif

# ifdef ESP_PLATFORM
# if NIMBLE_CPP_SCAN_DUPL_ENABLED
# if NIMBLE_CPP_SCAN_DUPL_ENABLED
static uint16_t m_scanDuplicateSize;
static uint8_t m_scanFilterMode;
static uint16_t m_scanDuplicateResetTime;
Expand Down Expand Up @@ -306,6 +306,7 @@ class NimBLEDevice {

# if MYNEWT_VAL(BLE_ROLE_CENTRAL) || MYNEWT_VAL(BLE_ROLE_PERIPHERAL)
# include "NimBLEConnInfo.h"
# include "NimBLEStream.h"
# endif

# include "NimBLEAddress.h"
Expand Down
Loading
Loading