NimBLEClient/ Add getCharacteristic() by handle.

* Add getCharacteristic() by handle.

Add a convenience method for getting a characteristic by the 16-bit
handle.
This commit is contained in:
Guo-Rong Koh 2020-09-15 22:09:30 -06:00 committed by h2zero
parent d9e11ee630
commit a331cb05e9
2 changed files with 27 additions and 0 deletions

View file

@ -641,6 +641,31 @@ bool NimBLEClient::setValue(const NimBLEUUID &serviceUUID, const NimBLEUUID &cha
} // setValue } // setValue
/**
* @brief Get the remote characteristic with the specified handle.
* @param [in] handle The handle of the desired characteristic.
* @returns The matching remote characteristic, nullptr otherwise.
*/
NimBLERemoteCharacteristic* NimBLEClient::getCharacteristic(const uint16_t handle)
{
NimBLERemoteService *pService = nullptr;
for(auto it = m_servicesVector.begin(); it != m_servicesVector.end(); ++it) {
if ((*it)->getStartHandle() <= handle && handle <= (*it)->getEndHandle()) {
pService = *it;
break;
}
}
if (pService != nullptr) {
for (auto it = pService->begin(); it != pService->end(); ++it) {
if ((*it)->getHandle() == handle) {
return *it;
}
}
}
return nullptr;
}
/** /**
* @brief Get the current mtu of this connection. * @brief Get the current mtu of this connection.

View file

@ -30,6 +30,7 @@
#include <string> #include <string>
class NimBLERemoteService; class NimBLERemoteService;
class NimBLERemoteCharacteristic;
class NimBLEClientCallbacks; class NimBLEClientCallbacks;
class NimBLEAdvertisedDevice; class NimBLEAdvertisedDevice;
@ -55,6 +56,7 @@ public:
std::string getValue(const NimBLEUUID &serviceUUID, const NimBLEUUID &characteristicUUID); std::string getValue(const NimBLEUUID &serviceUUID, const NimBLEUUID &characteristicUUID);
bool setValue(const NimBLEUUID &serviceUUID, const NimBLEUUID &characteristicUUID, bool setValue(const NimBLEUUID &serviceUUID, const NimBLEUUID &characteristicUUID,
const std::string &value); const std::string &value);
NimBLERemoteCharacteristic* getCharacteristic(const uint16_t handle);
bool isConnected(); bool isConnected();
void setClientCallbacks(NimBLEClientCallbacks *pClientCallbacks, void setClientCallbacks(NimBLEClientCallbacks *pClientCallbacks,
bool deleteCallbacks = true); bool deleteCallbacks = true);