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 <wakwak-koba@users.noreply.github.com>
This commit is contained in:
h2zero 2020-07-30 10:04:34 -06:00 committed by h2zero
parent f3e0d66853
commit 33d0a732a6
4 changed files with 28 additions and 3 deletions

View file

@ -127,6 +127,10 @@ size_t NimBLEClient::deleteService(const NimBLEUUID &uuid) {
} // deleteServices
bool NimBLEClient::connect(bool refreshServices) {
return connect(m_peerAddress, 0, refreshServices);
}
/**
* @brief Connect to an advertising device.
* @param [in] device The device to connect to.
@ -348,7 +352,23 @@ uint16_t NimBLEClient::getConnId() {
*/
NimBLEAddress NimBLEClient::getPeerAddress() {
return m_peerAddress;
} // getAddress
} // getPeerAddress
/**
* @brief Set the peer address.
* @param [in] address The address of the peer that this client is
* connected or should connect to.
*/
void NimBLEClient::setPeerAddress(const NimBLEAddress &address) {
if(isConnected()) {
NIMBLE_LOGE(LOG_TAG, "Cannot set peer address while connected");
return;
}
m_peerAddress = address;
NIMBLE_LOGE(LOG_TAG, "Peer address set: %s", std::string(m_peerAddress).c_str());
} // setPeerAddress
/**

View file

@ -41,8 +41,10 @@ public:
bool connect(NimBLEAdvertisedDevice* device, bool refreshServices = true);
bool connect(const NimBLEAddress &address, uint8_t type = BLE_ADDR_PUBLIC,
bool refreshServices = true);
bool connect(bool refreshServices = true);
int disconnect(uint8_t reason = BLE_ERR_REM_USER_CONN_TERM);
NimBLEAddress getPeerAddress();
void setPeerAddress(const NimBLEAddress &address);
int getRssi();
std::vector<NimBLERemoteService*>* getServices(bool refresh = false);
std::vector<NimBLERemoteService*>::iterator begin();

View file

@ -140,13 +140,16 @@ void NimBLEDevice::stopAdvertising() {
* @return A reference to the new client object.
*/
#if defined(CONFIG_BT_NIMBLE_ROLE_CENTRAL)
/* STATIC */ NimBLEClient* NimBLEDevice::createClient() {
/* STATIC */ NimBLEClient* NimBLEDevice::createClient(NimBLEAddress peerAddress) {
if(m_cList.size() >= NIMBLE_MAX_CONNECTIONS) {
NIMBLE_LOGW("Number of clients exceeds Max connections. Max=(%d)",
NIMBLE_MAX_CONNECTIONS);
}
NimBLEClient* pClient = new NimBLEClient();
if(peerAddress != NimBLEAddress("")) {
pClient->setPeerAddress(peerAddress);
}
m_cList.push_back(pClient);
return pClient;

View file

@ -130,7 +130,7 @@ public:
#endif
#if defined( CONFIG_BT_NIMBLE_ROLE_CENTRAL)
static NimBLEClient* createClient();
static NimBLEClient* createClient(NimBLEAddress peerAddress = NimBLEAddress(""));
static bool deleteClient(NimBLEClient* pClient);
static NimBLEClient* getClientByID(uint16_t conn_id);
static NimBLEClient* getClientByPeerAddress(const NimBLEAddress &peer_addr);