Skip to content

Commit bac0753

Browse files
h2zerowakwak-koba
andcommitted
Set peer address of client when creating the instance.
* Adds new client connect method that will connect to the address already set. Co-authored-by: wakwak-koba <[email protected]>
1 parent 4a79560 commit bac0753

File tree

4 files changed

+28
-3
lines changed

4 files changed

+28
-3
lines changed

src/NimBLEClient.cpp

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,10 @@ size_t NimBLEClient::deleteService(const NimBLEUUID &uuid) {
127127
} // deleteServices
128128

129129

130+
bool NimBLEClient::connect(bool refreshServices) {
131+
return connect(m_peerAddress, 0, refreshServices);
132+
}
133+
130134
/**
131135
* @brief Connect to an advertising device.
132136
* @param [in] device The device to connect to.
@@ -348,7 +352,23 @@ uint16_t NimBLEClient::getConnId() {
348352
*/
349353
NimBLEAddress NimBLEClient::getPeerAddress() {
350354
return m_peerAddress;
351-
} // getAddress
355+
} // getPeerAddress
356+
357+
358+
/**
359+
* @brief Set the peer address.
360+
* @param [in] address The address of the peer that this client is
361+
* connected or should connect to.
362+
*/
363+
void NimBLEClient::setPeerAddress(const NimBLEAddress &address) {
364+
if(isConnected()) {
365+
NIMBLE_LOGE(LOG_TAG, "Cannot set peer address while connected");
366+
return;
367+
}
368+
369+
m_peerAddress = address;
370+
NIMBLE_LOGE(LOG_TAG, "Peer address set: %s", std::string(m_peerAddress).c_str());
371+
} // setPeerAddress
352372

353373

354374
/**

src/NimBLEClient.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,10 @@ class NimBLEClient {
4141
bool connect(NimBLEAdvertisedDevice* device, bool refreshServices = true);
4242
bool connect(const NimBLEAddress &address, uint8_t type = BLE_ADDR_PUBLIC,
4343
bool refreshServices = true);
44+
bool connect(bool refreshServices = true);
4445
int disconnect(uint8_t reason = BLE_ERR_REM_USER_CONN_TERM);
4546
NimBLEAddress getPeerAddress();
47+
void setPeerAddress(const NimBLEAddress &address);
4648
int getRssi();
4749
std::vector<NimBLERemoteService*>* getServices(bool refresh = false);
4850
std::vector<NimBLERemoteService*>::iterator begin();

src/NimBLEDevice.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,13 +140,16 @@ void NimBLEDevice::stopAdvertising() {
140140
* @return A reference to the new client object.
141141
*/
142142
#if defined(CONFIG_BT_NIMBLE_ROLE_CENTRAL)
143-
/* STATIC */ NimBLEClient* NimBLEDevice::createClient() {
143+
/* STATIC */ NimBLEClient* NimBLEDevice::createClient(NimBLEAddress peerAddress) {
144144
if(m_cList.size() >= NIMBLE_MAX_CONNECTIONS) {
145145
NIMBLE_LOGW("Number of clients exceeds Max connections. Max=(%d)",
146146
NIMBLE_MAX_CONNECTIONS);
147147
}
148148

149149
NimBLEClient* pClient = new NimBLEClient();
150+
if(peerAddress != NimBLEAddress("")) {
151+
pClient->setPeerAddress(peerAddress);
152+
}
150153
m_cList.push_back(pClient);
151154

152155
return pClient;

src/NimBLEDevice.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ class NimBLEDevice {
130130
#endif
131131

132132
#if defined( CONFIG_BT_NIMBLE_ROLE_CENTRAL)
133-
static NimBLEClient* createClient();
133+
static NimBLEClient* createClient(NimBLEAddress peerAddress = NimBLEAddress(""));
134134
static bool deleteClient(NimBLEClient* pClient);
135135
static NimBLEClient* getClientByID(uint16_t conn_id);
136136
static NimBLEClient* getClientByPeerAddress(const NimBLEAddress &peer_addr);

0 commit comments

Comments
 (0)