@@ -551,6 +551,66 @@ uint16_t NimBLEClient::getConnId() {
551
551
return m_conn_id;
552
552
} // getConnId
553
553
554
+ /* *
555
+ * @brief Clear the connection information for this client.
556
+ * @note This is designed to be used to reset the connection information after
557
+ * calling setConnection(), and should not be used to disconnect from a
558
+ * peer. To disconnect from a peer, use disconnect().
559
+ */
560
+ void NimBLEClient::clearConnection () {
561
+ m_conn_id = BLE_HS_CONN_HANDLE_NONE;
562
+ m_connEstablished = false ;
563
+ m_peerAddress = NimBLEAddress ();
564
+ } // clearConnection
565
+
566
+ /* *
567
+ * @brief Set the connection information for this client.
568
+ * @param [in] connInfo The connection information.
569
+ * @return True on success.
570
+ * @note Sets the connection established flag to true.
571
+ * @note If the client is already connected to a peer, this will return false.
572
+ * @note This is designed to be used when a connection is made outside of the
573
+ * NimBLEClient class, such as when a connection is made by the
574
+ * NimBLEServer class and the client is passed the connection id. This use
575
+ * enables the GATT Server to read the name of the device that has
576
+ * connected to it.
577
+ */
578
+ bool NimBLEClient::setConnection (NimBLEConnInfo &connInfo) {
579
+ if (isConnected () || m_connEstablished) {
580
+ NIMBLE_LOGE (LOG_TAG, " Already connected" );
581
+ return false ;
582
+ }
583
+
584
+ m_peerAddress = connInfo.getAddress ();
585
+ m_conn_id = connInfo.getConnHandle ();
586
+ m_connEstablished = true ;
587
+
588
+ return true ;
589
+ } // setConnection
590
+
591
+ /* *
592
+ * @brief Set the connection information for this client.
593
+ * @param [in] conn_id The connection id.
594
+ * @note Sets the connection established flag to true.
595
+ * @note This is designed to be used when a connection is made outside of the
596
+ * NimBLEClient class, such as when a connection is made by the
597
+ * NimBLEServer class and the client is passed the connection id. This use
598
+ * enables the GATT Server to read the name of the device that has
599
+ * connected to it.
600
+ * @note If the client is already connected to a peer, this will return false.
601
+ * @note This will look up the peer address using the connection id.
602
+ */
603
+ bool NimBLEClient::setConnection (uint16_t conn_id) {
604
+ // we weren't provided the peer address, look it up using ble_gap_conn_find
605
+ NimBLEConnInfo connInfo;
606
+ int rc = ble_gap_conn_find (m_conn_id, &connInfo.m_desc );
607
+ if (rc != 0 ) {
608
+ NIMBLE_LOGE (LOG_TAG, " Connection info not found" );
609
+ return false ;
610
+ }
611
+
612
+ return setConnection (connInfo);
613
+ } // setConnection
554
614
555
615
/* *
556
616
* @brief Retrieve the address of the peer.
0 commit comments