Add iterators to client remote attributes.

Add iterators for NimBLEScan: NimBLEadvertisedDevice, NimBLEClient: NimBLERemoteService, NimBLERemoteService: NimBLERemoteCharacteristic and NimBLERemoteCharacteristic: NimBLERemoteDescriptor

This is handy e.g. for showing every address of the advertised devices from a scan. To do so, first get a new scan and next:
```
for(auto pAdvertisedDevice: pBLEScan->getResults()) {
  Serial.printf("Address is %s\n", std::string(pAdvertisedDevice->getAddress()).c_str());
}
```
Of course any other property of the advertised device can be shown (or looked up, if that is your use case)

Also this is handy e.g. for showing every UUID in a peripheral. To do so, first connect to a peripheral and next:
```
for(auto pService: *pClient) {
  Serial.printf("Service UUID is %s\n", std::string(pService->getUUID()).c_str());
  for(auto pCharacteristic: *pService) {
    Serial.printf("Characteristic UUID is %s\n", std::string(pCharacteristic->getUUID()).c_str());
    for(auto pDescriptor: *pCharacteristic) {
      Serial.printf("Descriptor UUID is %s\n", std::string(pDescriptor->getUUID()).c_str());
    }
  }
}
```
Again of course any other property can be shown, or looked up.
This commit is contained in:
h2zero 2020-05-22 20:13:52 -06:00
parent 32e1022e67
commit 10e50a8791
8 changed files with 134 additions and 54 deletions

View file

@ -348,6 +348,24 @@ int NimBLEClient::getRssi() {
} // getRssi } // getRssi
/**
* @brief Get iterator to the beginning of the vector of remote service pointers.
* @return An iterator to the beginning of the vector of remote service pointers.
*/
std::vector<NimBLERemoteService*>::iterator NimBLEClient::begin() {
return m_servicesVector.begin();
}
/**
* @brief Get iterator to the end of the vector of remote service pointers.
* @return An iterator to the end of the vector of remote service pointers.
*/
std::vector<NimBLERemoteService*>::iterator NimBLEClient::end() {
return m_servicesVector.end();
}
/** /**
* @brief Get the service BLE Remote Service instance corresponding to the uuid. * @brief Get the service BLE Remote Service instance corresponding to the uuid.
* @param [in] uuid The UUID of the service being sought. * @param [in] uuid The UUID of the service being sought.

View file

@ -43,6 +43,8 @@ public:
int getRssi(); // Get the RSSI of the remote BLE Server int getRssi(); // Get the RSSI of the remote BLE Server
std::vector<NimBLERemoteService*>* getServices(); // Get a vector of the services offered by the remote BLE Server std::vector<NimBLERemoteService*>* getServices(); // Get a vector of the services offered by the remote BLE Server
std::vector<NimBLERemoteService*>::iterator begin();
std::vector<NimBLERemoteService*>::iterator end();
NimBLERemoteService* getService(const char* uuid); // Get a reference to a specified service offered by the remote BLE server. NimBLERemoteService* getService(const char* uuid); // Get a reference to a specified service offered by the remote BLE server.
NimBLERemoteService* getService(const NimBLEUUID &uuid); // Get a reference to a specified service offered by the remote BLE server. NimBLERemoteService* getService(const NimBLEUUID &uuid); // Get a reference to a specified service offered by the remote BLE server.
std::string getValue(const NimBLEUUID &serviceUUID, const NimBLEUUID &characteristicUUID); // Get the value of a given characteristic at a given service. std::string getValue(const NimBLEUUID &serviceUUID, const NimBLEUUID &characteristicUUID); // Get the value of a given characteristic at a given service.

View file

@ -240,6 +240,24 @@ uint16_t NimBLERemoteCharacteristic::getDefHandle() {
} // getDefHandle } // getDefHandle
/**
* @brief Get iterator to the beginning of the vector of remote descriptor pointers.
* @return An iterator to the beginning of the vector of remote descriptor pointers.
*/
std::vector<NimBLERemoteDescriptor*>::iterator NimBLERemoteCharacteristic::begin() {
return m_descriptorVector.begin();
}
/**
* @brief Get iterator to the end of the vector of remote descriptor pointers.
* @return An iterator to the end of the vector of remote descriptor pointers.
*/
std::vector<NimBLERemoteDescriptor*>::iterator NimBLERemoteCharacteristic::end() {
return m_descriptorVector.end();
}
/** /**
* @brief Get the descriptor instance with the given UUID that belongs to this characteristic. * @brief Get the descriptor instance with the given UUID that belongs to this characteristic.
* @param [in] uuid The UUID of the descriptor to find. * @param [in] uuid The UUID of the descriptor to find.

View file

@ -48,6 +48,8 @@ public:
bool canRead(); bool canRead();
bool canWrite(); bool canWrite();
bool canWriteNoResponse(); bool canWriteNoResponse();
std::vector<NimBLERemoteDescriptor*>::iterator begin();
std::vector<NimBLERemoteDescriptor*>::iterator end();
NimBLERemoteDescriptor* getDescriptor(const NimBLEUUID &uuid); NimBLERemoteDescriptor* getDescriptor(const NimBLEUUID &uuid);
std::vector<NimBLERemoteDescriptor*>* getDescriptors(); std::vector<NimBLERemoteDescriptor*>* getDescriptors();
uint16_t getHandle(); uint16_t getHandle();

View file

@ -64,6 +64,24 @@ NimBLERemoteService::~NimBLERemoteService() {
} }
/**
* @brief Get iterator to the beginning of the vector of remote characteristic pointers.
* @return An iterator to the beginning of the vector of remote characteristic pointers.
*/
std::vector<NimBLERemoteCharacteristic*>::iterator NimBLERemoteService::begin() {
return m_characteristicVector.begin();
}
/**
* @brief Get iterator to the end of the vector of remote characteristic pointers.
* @return An iterator to the end of the vector of remote characteristic pointers.
*/
std::vector<NimBLERemoteCharacteristic*>::iterator NimBLERemoteService::end() {
return m_characteristicVector.end();
}
/** /**
* @brief Get the remote characteristic object for the characteristic UUID. * @brief Get the remote characteristic object for the characteristic UUID.
* @param [in] uuid Remote characteristic uuid. * @param [in] uuid Remote characteristic uuid.

View file

@ -39,6 +39,8 @@ public:
virtual ~NimBLERemoteService(); virtual ~NimBLERemoteService();
// Public methods // Public methods
std::vector<NimBLERemoteCharacteristic*>::iterator begin();
std::vector<NimBLERemoteCharacteristic*>::iterator end();
NimBLERemoteCharacteristic* getCharacteristic(const char* uuid); // Get the specified characteristic reference. NimBLERemoteCharacteristic* getCharacteristic(const char* uuid); // Get the specified characteristic reference.
NimBLERemoteCharacteristic* getCharacteristic(const NimBLEUUID &uuid); // Get the specified characteristic reference. NimBLERemoteCharacteristic* getCharacteristic(const NimBLEUUID &uuid); // Get the specified characteristic reference.
// BLERemoteCharacteristic* getCharacteristic(uint16_t uuid); // Get the specified characteristic reference. // BLERemoteCharacteristic* getCharacteristic(uint16_t uuid); // Get the specified characteristic reference.

View file

@ -404,6 +404,24 @@ NimBLEAdvertisedDevice NimBLEScanResults::getDevice(uint32_t i) {
} }
/**
* @brief Get iterator to the beginning of the vector of advertised device pointers.
* @return An iterator to the beginning of the vector of advertised device pointers.
*/
std::vector<NimBLEAdvertisedDevice*>::iterator NimBLEScanResults::begin() {
return m_advertisedDevicesVector.begin();
}
/**
* @brief Get iterator to the end of the vector of advertised device pointers.
* @return An iterator to the end of the vector of advertised device pointers.
*/
std::vector<NimBLEAdvertisedDevice*>::iterator NimBLEScanResults::end() {
return m_advertisedDevicesVector.end();
}
/** /**
* @brief Return a pointer to the specified device at the given address. * @brief Return a pointer to the specified device at the given address.
* If the address is not found a nullptr is returned. * If the address is not found a nullptr is returned.

View file

@ -44,6 +44,8 @@ public:
void dump(); void dump();
int getCount(); int getCount();
NimBLEAdvertisedDevice getDevice(uint32_t i); NimBLEAdvertisedDevice getDevice(uint32_t i);
std::vector<NimBLEAdvertisedDevice*>::iterator begin();
std::vector<NimBLEAdvertisedDevice*>::iterator end();
NimBLEAdvertisedDevice *getDevice(const NimBLEAddress &address); NimBLEAdvertisedDevice *getDevice(const NimBLEAddress &address);
private: private: