|
| 1 | +/** |
| 2 | + * NimBLE_Stream_Client Example: |
| 3 | + * |
| 4 | + * Demonstrates using NimBLEStreamClient to connect to a BLE GATT server |
| 5 | + * and communicate using the Arduino Stream interface. |
| 6 | + * |
| 7 | + * This allows you to use familiar methods like print(), println(), |
| 8 | + * read(), and available() over BLE, similar to how you would use Serial. |
| 9 | + * |
| 10 | + * This example connects to the NimBLE_Stream_Server example. |
| 11 | + * |
| 12 | + * Created: November 2025 |
| 13 | + * Author: NimBLE-Arduino Contributors |
| 14 | + */ |
| 15 | + |
| 16 | +#include <Arduino.h> |
| 17 | +#include <NimBLEDevice.h> |
| 18 | +#include <NimBLEStream.h> |
| 19 | + |
| 20 | +// Service and Characteristic UUIDs (must match the server) |
| 21 | +#define SERVICE_UUID "6E400001-B5A3-F393-E0A9-E50E24DCCA9E" |
| 22 | +#define CHARACTERISTIC_UUID "6E400002-B5A3-F393-E0A9-E50E24DCCA9E" |
| 23 | + |
| 24 | +// Create the stream client instance |
| 25 | +NimBLEStreamClient bleStream; |
| 26 | + |
| 27 | +// Connection state variables |
| 28 | +static bool doConnect = false; |
| 29 | +static bool connected = false; |
| 30 | +static const NimBLEAdvertisedDevice* pServerDevice = nullptr; |
| 31 | +static NimBLEClient* pClient = nullptr; |
| 32 | + |
| 33 | +/** Scan callbacks to find the server */ |
| 34 | +class ScanCallbacks : public NimBLEScanCallbacks { |
| 35 | + void onResult(const NimBLEAdvertisedDevice* advertisedDevice) override { |
| 36 | + Serial.printf("Advertised Device: %s\n", advertisedDevice->toString().c_str()); |
| 37 | + |
| 38 | + // Check if this device advertises our service |
| 39 | + if (advertisedDevice->isAdvertisingService(NimBLEUUID(SERVICE_UUID))) { |
| 40 | + Serial.println("Found our stream server!"); |
| 41 | + pServerDevice = advertisedDevice; |
| 42 | + NimBLEDevice::getScan()->stop(); |
| 43 | + doConnect = true; |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + void onScanEnd(const NimBLEScanResults& results, int reason) override { |
| 48 | + Serial.println("Scan ended"); |
| 49 | + if (!doConnect && !connected) { |
| 50 | + Serial.println("Server not found, restarting scan..."); |
| 51 | + NimBLEDevice::getScan()->start(5, false, true); |
| 52 | + } |
| 53 | + } |
| 54 | +} scanCallbacks; |
| 55 | + |
| 56 | +/** Client callbacks for connection/disconnection events */ |
| 57 | +class ClientCallbacks : public NimBLEClientCallbacks { |
| 58 | + void onConnect(NimBLEClient* pClient) override { |
| 59 | + Serial.println("Connected to server"); |
| 60 | + // Update connection parameters for better throughput |
| 61 | + pClient->updateConnParams(12, 24, 0, 200); |
| 62 | + } |
| 63 | + |
| 64 | + void onDisconnect(NimBLEClient* pClient, int reason) override { |
| 65 | + Serial.printf("Disconnected from server, reason: %d\n", reason); |
| 66 | + connected = false; |
| 67 | + bleStream.deinit(); |
| 68 | + |
| 69 | + // Restart scanning |
| 70 | + Serial.println("Restarting scan..."); |
| 71 | + NimBLEDevice::getScan()->start(5, false, true); |
| 72 | + } |
| 73 | +} clientCallbacks; |
| 74 | + |
| 75 | +/** Connect to the BLE Server and set up the stream */ |
| 76 | +bool connectToServer() { |
| 77 | + Serial.printf("Connecting to: %s\n", pServerDevice->getAddress().toString().c_str()); |
| 78 | + |
| 79 | + // Create or reuse a client |
| 80 | + pClient = NimBLEDevice::getClientByPeerAddress(pServerDevice->getAddress()); |
| 81 | + if (!pClient) { |
| 82 | + pClient = NimBLEDevice::createClient(); |
| 83 | + if (!pClient) { |
| 84 | + Serial.println("Failed to create client"); |
| 85 | + return false; |
| 86 | + } |
| 87 | + pClient->setClientCallbacks(&clientCallbacks, false); |
| 88 | + pClient->setConnectionParams(12, 24, 0, 200); |
| 89 | + pClient->setConnectTimeout(5000); |
| 90 | + } |
| 91 | + |
| 92 | + // Connect to the remote BLE Server |
| 93 | + if (!pClient->connect(pServerDevice)) { |
| 94 | + Serial.println("Failed to connect to server"); |
| 95 | + return false; |
| 96 | + } |
| 97 | + |
| 98 | + Serial.println("Connected! Discovering services..."); |
| 99 | + |
| 100 | + // Get the service and characteristic |
| 101 | + NimBLERemoteService* pRemoteService = pClient->getService(SERVICE_UUID); |
| 102 | + if (!pRemoteService) { |
| 103 | + Serial.println("Failed to find our service UUID"); |
| 104 | + pClient->disconnect(); |
| 105 | + return false; |
| 106 | + } |
| 107 | + Serial.println("Found the stream service"); |
| 108 | + |
| 109 | + NimBLERemoteCharacteristic* pRemoteCharacteristic = pRemoteService->getCharacteristic(CHARACTERISTIC_UUID); |
| 110 | + if (!pRemoteCharacteristic) { |
| 111 | + Serial.println("Failed to find our characteristic UUID"); |
| 112 | + pClient->disconnect(); |
| 113 | + return false; |
| 114 | + } |
| 115 | + Serial.println("Found the stream characteristic"); |
| 116 | + |
| 117 | + /** |
| 118 | + * Initialize the stream client with the remote characteristic |
| 119 | + * subscribeNotify=true means we'll receive notifications in the RX buffer |
| 120 | + */ |
| 121 | + if (!bleStream.init(pRemoteCharacteristic, true)) { |
| 122 | + Serial.println("Failed to initialize BLE stream!"); |
| 123 | + pClient->disconnect(); |
| 124 | + return false; |
| 125 | + } |
| 126 | + |
| 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 | + |
| 135 | + Serial.println("BLE Stream initialized successfully!"); |
| 136 | + connected = true; |
| 137 | + return true; |
| 138 | +} |
| 139 | + |
| 140 | +void setup() { |
| 141 | + Serial.begin(115200); |
| 142 | + Serial.println("Starting NimBLE Stream Client"); |
| 143 | + |
| 144 | + /** Initialize NimBLE */ |
| 145 | + NimBLEDevice::init("NimBLE-StreamClient"); |
| 146 | + |
| 147 | + /** |
| 148 | + * Create the BLE scan instance and set callbacks |
| 149 | + * Configure scan parameters |
| 150 | + */ |
| 151 | + NimBLEScan* pScan = NimBLEDevice::getScan(); |
| 152 | + pScan->setScanCallbacks(&scanCallbacks, false); |
| 153 | + pScan->setInterval(100); |
| 154 | + pScan->setWindow(99); |
| 155 | + pScan->setActiveScan(true); |
| 156 | + |
| 157 | + /** Start scanning for the server */ |
| 158 | + Serial.println("Scanning for BLE Stream Server..."); |
| 159 | + pScan->start(5, false, true); |
| 160 | +} |
| 161 | + |
| 162 | +void loop() { |
| 163 | + // If we found a server, try to connect |
| 164 | + if (doConnect) { |
| 165 | + doConnect = false; |
| 166 | + if (connectToServer()) { |
| 167 | + Serial.println("Stream ready for communication!"); |
| 168 | + } else { |
| 169 | + Serial.println("Failed to connect to server"); |
| 170 | + // Scan will restart automatically |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + // If we're connected, demonstrate the stream interface |
| 175 | + if (connected && bleStream) { |
| 176 | + |
| 177 | + // Check if we received any data from the server |
| 178 | + if (bleStream.available()) { |
| 179 | + Serial.print("Received from server: "); |
| 180 | + |
| 181 | + // Read all available data (just like Serial.read()) |
| 182 | + while (bleStream.available()) { |
| 183 | + char c = bleStream.read(); |
| 184 | + Serial.write(c); |
| 185 | + } |
| 186 | + Serial.println(); |
| 187 | + } |
| 188 | + |
| 189 | + // Send a message every 5 seconds using Stream methods |
| 190 | + static unsigned long lastSend = 0; |
| 191 | + if (millis() - lastSend > 5000) { |
| 192 | + lastSend = millis(); |
| 193 | + |
| 194 | + // Using familiar Serial-like methods! |
| 195 | + bleStream.print("Hello from client! Uptime: "); |
| 196 | + bleStream.print(millis() / 1000); |
| 197 | + bleStream.println(" seconds"); |
| 198 | + |
| 199 | + Serial.println("Sent data to server via BLE stream"); |
| 200 | + } |
| 201 | + |
| 202 | + // You can also read from Serial and send over BLE |
| 203 | + if (Serial.available()) { |
| 204 | + Serial.println("Reading from Serial and sending via BLE:"); |
| 205 | + while (Serial.available()) { |
| 206 | + char c = Serial.read(); |
| 207 | + Serial.write(c); // Echo locally |
| 208 | + bleStream.write(c); // Send via BLE |
| 209 | + } |
| 210 | + Serial.println(); |
| 211 | + } |
| 212 | + } |
| 213 | + |
| 214 | + delay(10); |
| 215 | +} |
0 commit comments