33 *
44 * Demonstrates using NimBLEStreamClient to connect to a BLE GATT server
55 * and communicate 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 * This example connects to the NimBLE_Stream_Server example.
@@ -33,7 +33,7 @@ static NimBLEClient* pClient = nullptr;
3333class ScanCallbacks : public NimBLEScanCallbacks {
3434 void onResult (const NimBLEAdvertisedDevice* advertisedDevice) override {
3535 Serial.printf (" Advertised Device: %s\n " , advertisedDevice->toString ().c_str ());
36-
36+
3737 // Check if this device advertises our service
3838 if (advertisedDevice->isAdvertisingService (NimBLEUUID (SERVICE_UUID ))) {
3939 Serial.println (" Found our stream server!" );
@@ -64,7 +64,7 @@ class ClientCallbacks : public NimBLEClientCallbacks {
6464 Serial.printf (" Disconnected from server, reason: %d\n " , reason);
6565 connected = false ;
6666 bleStream.end ();
67-
67+
6868 // Restart scanning
6969 Serial.println (" Restarting scan..." );
7070 NimBLEDevice::getScan ()->start (5 , false , true );
@@ -93,7 +93,7 @@ bool connectToServer() {
9393 Serial.println (" Failed to connect to server" );
9494 return false ;
9595 }
96-
96+
9797 Serial.println (" Connected! Discovering services..." );
9898
9999 // Get the service and characteristic
@@ -135,7 +135,7 @@ void setup() {
135135 /* * Initialize NimBLE */
136136 NimBLEDevice::init (" NimBLE-StreamClient" );
137137
138- /* *
138+ /* *
139139 * Create the BLE scan instance and set callbacks
140140 * Configure scan parameters
141141 */
@@ -157,18 +157,18 @@ void loop() {
157157 if (connectToServer ()) {
158158 Serial.println (" Stream ready for communication!" );
159159 } else {
160- Serial.println (" Failed to connect to server" );
161- // Scan will restart automatically
160+ Serial.println (" Failed to connect to server, restarting scan..." );
161+ pServerDevice = nullptr ;
162+ NimBLEDevice::getScan ()->start (5 , false , true );
162163 }
163164 }
164165
165166 // If we're connected, demonstrate the stream interface
166167 if (connected && bleStream) {
167-
168168 // Check if we received any data from the server
169169 if (bleStream.available ()) {
170170 Serial.print (" Received from server: " );
171-
171+
172172 // Read all available data (just like Serial.read())
173173 while (bleStream.available ()) {
174174 char c = bleStream.read ();
@@ -181,12 +181,12 @@ void loop() {
181181 static unsigned long lastSend = 0 ;
182182 if (millis () - lastSend > 5000 ) {
183183 lastSend = millis ();
184-
184+
185185 // Using familiar Serial-like methods!
186186 bleStream.print (" Hello from client! Uptime: " );
187187 bleStream.print (millis () / 1000 );
188188 bleStream.println (" seconds" );
189-
189+
190190 Serial.println (" Sent data to server via BLE stream" );
191191 }
192192
@@ -195,8 +195,8 @@ void loop() {
195195 Serial.println (" Reading from Serial and sending via BLE:" );
196196 while (Serial.available ()) {
197197 char c = Serial.read ();
198- Serial.write (c); // Echo locally
199- bleStream.write (c); // Send via BLE
198+ Serial.write (c); // Echo locally
199+ bleStream.write (c); // Send via BLE
200200 }
201201 Serial.println ();
202202 }
0 commit comments