77 * This allows you to use familiar methods like print(), println(),
88 * read(), and available() over BLE, similar to how you would use Serial.
99 *
10- * This example connects to the NimBLE_Stream_Server example.
10+ * This example connects to the NimBLE_Stream_Server example using the Nordic UART
11+ * Service (NUS) with separate TX and RX characteristics.
1112 *
1213 * Created: November 2025
1314 * Author: NimBLE-Arduino Contributors
1617#include < Arduino.h>
1718#include < NimBLEDevice.h>
1819
19- // Service and Characteristic UUIDs (must match the server)
20- #define SERVICE_UUID " 6E400001-B5A3-F393-E0A9-E50E24DCCA9E"
21- #define CHARACTERISTIC_UUID " 6E400002-B5A3-F393-E0A9-E50E24DCCA9E"
20+ // Nordic UART Service (NUS) UUIDs (must match the server)
21+ #define SERVICE_UUID " 6E400001-B5A3-F393-E0A9-E50E24DCCA9E"
22+ #define TX_CHAR_UUID " 6E400003-B5A3-F393-E0A9-E50E24DCCA9E" // Server TX: client subscribes here
23+ #define RX_CHAR_UUID " 6E400002-B5A3-F393-E0A9-E50E24DCCA9E" // Server RX: client writes here
2224
2325// Create the stream client instance
2426NimBLEStreamClient bleStream;
@@ -116,7 +118,7 @@ bool connectToServer() {
116118
117119 Serial.println (" Connected! Discovering services..." );
118120
119- // Get the service and characteristic
121+ // Get the service
120122 NimBLERemoteService* pRemoteService = pClient->getService (SERVICE_UUID );
121123 if (!pRemoteService) {
122124 Serial.println (" Failed to find our service UUID" );
@@ -125,19 +127,30 @@ bool connectToServer() {
125127 }
126128 Serial.println (" Found the stream service" );
127129
128- NimBLERemoteCharacteristic* pRemoteCharacteristic = pRemoteService->getCharacteristic (CHARACTERISTIC_UUID );
129- if (!pRemoteCharacteristic) {
130- Serial.println (" Failed to find our characteristic UUID" );
130+ // Get the TX characteristic (server sends notifications here; client subscribes for RX)
131+ NimBLERemoteCharacteristic* pTxChar = pRemoteService->getCharacteristic (TX_CHAR_UUID );
132+ if (!pTxChar) {
133+ Serial.println (" Failed to find TX characteristic" );
131134 pClient->disconnect ();
132135 return false ;
133136 }
134- Serial.println (" Found the stream characteristic" );
137+ Serial.println (" Found the TX characteristic" );
138+
139+ // Get the RX characteristic (server receives writes here; client writes for TX)
140+ NimBLERemoteCharacteristic* pRxChar = pRemoteService->getCharacteristic (RX_CHAR_UUID );
141+ if (!pRxChar) {
142+ Serial.println (" Failed to find RX characteristic" );
143+ pClient->disconnect ();
144+ return false ;
145+ }
146+ Serial.println (" Found the RX characteristic" );
135147
136148 /* *
137- * Initialize the stream client with the remote characteristic
138- * subscribeNotify=true means we'll receive notifications in the RX buffer
149+ * Initialize the stream client with separate TX and RX characteristics:
150+ * - pRxChar: the characteristic we write to (our TX = server's RX, UUID 6E400002)
151+ * - pTxChar: the characteristic we subscribe to (our RX = server's TX, UUID 6E400003)
139152 */
140- if (!bleStream.begin (pRemoteCharacteristic, true )) {
153+ if (!bleStream.begin (pRxChar, pTxChar )) {
141154 Serial.println (" Failed to initialize BLE stream!" );
142155 pClient->disconnect ();
143156 return false ;
0 commit comments