@@ -63,6 +63,7 @@ NimBLEClient::NimBLEClient(const NimBLEAddress &peerAddress) : m_peerAddress(pee
6363 m_deleteCallbacks = false ;
6464 m_pTaskData = nullptr ;
6565 m_connEstablished = false ;
66+ m_lastErr = 0 ;
6667
6768 m_pConnParams.scan_itvl = 16 ; // Scan interval in 0.625ms units (NimBLE Default)
6869 m_pConnParams.scan_window = 16 ; // Scan window in 0.625ms units (NimBLE Default)
@@ -251,6 +252,8 @@ bool NimBLEClient::connect(const NimBLEAddress &address, bool deleteAttibutes) {
251252
252253 } while (rc == BLE_HS_EBUSY);
253254
255+ m_lastErr = rc;
256+
254257 if (rc != 0 ) {
255258 m_pTaskData = nullptr ;
256259 return false ;
@@ -273,6 +276,7 @@ bool NimBLEClient::connect(const NimBLEAddress &address, bool deleteAttibutes) {
273276 return false ;
274277
275278 } else if (taskData.rc != 0 ){
279+ m_lastErr = taskData.rc ;
276280 NIMBLE_LOGE (LOG_TAG, " Connection failed; status=%d %s" ,
277281 taskData.rc ,
278282 NimBLEUtils::returnCodeToString (taskData.rc ));
@@ -314,6 +318,7 @@ bool NimBLEClient::secureConnection() {
314318
315319 int rc = NimBLEDevice::startSecurity (m_conn_id);
316320 if (rc != 0 ){
321+ m_lastErr = rc;
317322 m_pTaskData = nullptr ;
318323 return false ;
319324 }
@@ -322,6 +327,7 @@ bool NimBLEClient::secureConnection() {
322327 } while (taskData.rc == (BLE_HS_ERR_HCI_BASE + BLE_ERR_PINKEY_MISSING) && retryCount--);
323328
324329 if (taskData.rc != 0 ){
330+ m_lastErr = taskData.rc ;
325331 return false ;
326332 }
327333
@@ -372,6 +378,7 @@ int NimBLEClient::disconnect(uint8_t reason) {
372378 }
373379
374380 NIMBLE_LOGD (LOG_TAG, " << disconnect()" );
381+ m_lastErr = rc;
375382 return rc;
376383} // disconnect
377384
@@ -512,6 +519,7 @@ int NimBLEClient::getRssi() {
512519 if (rc != 0 ) {
513520 NIMBLE_LOGE (LOG_TAG, " Failed to read RSSI error code: %d, %s" ,
514521 rc, NimBLEUtils::returnCodeToString (rc));
522+ m_lastErr = rc;
515523 return 0 ;
516524 }
517525
@@ -650,11 +658,13 @@ bool NimBLEClient::retrieveServices(const NimBLEUUID *uuid_filter) {
650658
651659 if (rc != 0 ) {
652660 NIMBLE_LOGE (LOG_TAG, " ble_gattc_disc_all_svcs: rc=%d %s" , rc, NimBLEUtils::returnCodeToString (rc));
661+ m_lastErr = rc;
653662 return false ;
654663 }
655664
656665 // wait until we have all the services
657666 ulTaskNotifyTake (pdTRUE, portMAX_DELAY);
667+ m_lastErr = taskData.rc ;
658668
659669 if (taskData.rc == 0 ){
660670 NIMBLE_LOGD (LOG_TAG, " << retrieveServices" );
@@ -1136,6 +1146,15 @@ std::string NimBLEClient::toString() {
11361146} // toString
11371147
11381148
1149+ /* *
1150+ * @brief Get the last error code reported by the NimBLE host
1151+ * @return int, the NimBLE error code.
1152+ */
1153+ int NimBLEClient::getLastError () {
1154+ return m_lastErr;
1155+ } // getLastError
1156+
1157+
11391158void NimBLEClientCallbacks::onConnect (NimBLEClient* pClient) {
11401159 NIMBLE_LOGD (" NimBLEClientCallbacks" , " onConnect: default" );
11411160}
0 commit comments