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

@ -36,28 +36,30 @@ class NimBLEAdvertisedDevice;
*/ */
class NimBLEClient { class NimBLEClient {
public: public:
bool connect(NimBLEAdvertisedDevice* device, bool refreshServices = true); bool connect(NimBLEAdvertisedDevice* device, bool refreshServices = true);
bool connect(const NimBLEAddress &address, uint8_t type = BLE_ADDR_PUBLIC, bool refreshServices = true); // Connect to the remote BLE Server bool connect(const NimBLEAddress &address, uint8_t type = BLE_ADDR_PUBLIC, bool refreshServices = true); // Connect to the remote BLE Server
int disconnect(uint8_t reason = BLE_ERR_REM_USER_CONN_TERM); // Disconnect from the remote BLE Server int disconnect(uint8_t reason = BLE_ERR_REM_USER_CONN_TERM); // Disconnect from the remote BLE Server
NimBLEAddress getPeerAddress(); // Get the address of the remote BLE Server NimBLEAddress getPeerAddress(); // Get the address of the remote BLE Server
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
NimBLERemoteService* getService(const char* uuid); // Get a reference to a specified service offered by the remote BLE server. std::vector<NimBLERemoteService*>::iterator begin();
NimBLERemoteService* getService(const NimBLEUUID &uuid); // Get a reference to a specified service offered by the remote BLE server. std::vector<NimBLERemoteService*>::iterator end();
std::string getValue(const NimBLEUUID &serviceUUID, const NimBLEUUID &characteristicUUID); // Get the value of a given characteristic at a given service. NimBLERemoteService* getService(const char* uuid); // Get a reference to a specified service offered by the remote BLE server.
bool setValue(const NimBLEUUID &serviceUUID, const NimBLEUUID &characteristicUUID, const std::string &value); // Set the value of a given characteristic at a given service. NimBLERemoteService* getService(const NimBLEUUID &uuid); // Get a reference to a specified service offered by the remote BLE server.
bool isConnected(); // Return true if we are connected. std::string getValue(const NimBLEUUID &serviceUUID, const NimBLEUUID &characteristicUUID); // Get the value of a given characteristic at a given service.
void setClientCallbacks(NimBLEClientCallbacks *pClientCallbacks, bool deleteCallbacks = true); bool setValue(const NimBLEUUID &serviceUUID, const NimBLEUUID &characteristicUUID, const std::string &value); // Set the value of a given characteristic at a given service.
std::string toString(); // Return a string representation of this client. bool isConnected(); // Return true if we are connected.
uint16_t getConnId(); void setClientCallbacks(NimBLEClientCallbacks *pClientCallbacks, bool deleteCallbacks = true);
uint16_t getMTU(); std::string toString(); // Return a string representation of this client.
bool secureConnection(); uint16_t getConnId();
void setConnectTimeout(uint8_t timeout); uint16_t getMTU();
void setConnectionParams(uint16_t minInterval, uint16_t maxInterval, bool secureConnection();
void setConnectTimeout(uint8_t timeout);
void setConnectionParams(uint16_t minInterval, uint16_t maxInterval,
uint16_t latency, uint16_t timeout, uint16_t latency, uint16_t timeout,
uint16_t scanInterval=16, uint16_t scanWindow=16); // NimBLE default scan settings uint16_t scanInterval=16, uint16_t scanWindow=16); // NimBLE default scan settings
void updateConnParams(uint16_t minInterval, uint16_t maxInterval, void updateConnParams(uint16_t minInterval, uint16_t maxInterval,
uint16_t latency, uint16_t timeout); uint16_t latency, uint16_t timeout);

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

@ -42,29 +42,31 @@ public:
~NimBLERemoteCharacteristic(); ~NimBLERemoteCharacteristic();
// Public member functions // Public member functions
bool canBroadcast(); bool canBroadcast();
bool canIndicate(); bool canIndicate();
bool canNotify(); bool canNotify();
bool canRead(); bool canRead();
bool canWrite(); bool canWrite();
bool canWriteNoResponse(); bool canWriteNoResponse();
NimBLERemoteDescriptor* getDescriptor(const NimBLEUUID &uuid); std::vector<NimBLERemoteDescriptor*>::iterator begin();
std::vector<NimBLERemoteDescriptor*>* getDescriptors(); std::vector<NimBLERemoteDescriptor*>::iterator end();
uint16_t getHandle(); NimBLERemoteDescriptor* getDescriptor(const NimBLEUUID &uuid);
uint16_t getDefHandle(); std::vector<NimBLERemoteDescriptor*>* getDescriptors();
NimBLEUUID getUUID(); uint16_t getHandle();
std::string readValue(); uint16_t getDefHandle();
uint8_t readUInt8(); NimBLEUUID getUUID();
uint16_t readUInt16(); std::string readValue();
uint32_t readUInt32(); uint8_t readUInt8();
bool registerForNotify(notify_callback _callback, bool notifications = true, bool response = true); uint16_t readUInt16();
bool writeValue(const uint8_t* data, size_t length, bool response = false); uint32_t readUInt32();
bool writeValue(const std::string &newValue, bool response = false); bool registerForNotify(notify_callback _callback, bool notifications = true, bool response = true);
bool writeValue(uint8_t newValue, bool response = false); bool writeValue(const uint8_t* data, size_t length, bool response = false);
std::string toString(); bool writeValue(const std::string &newValue, bool response = false);
uint8_t* readRawData(); bool writeValue(uint8_t newValue, bool response = false);
size_t getDataLength(); std::string toString();
NimBLERemoteService* getRemoteService(); uint8_t* readRawData();
size_t getDataLength();
NimBLERemoteService* getRemoteService();
private: private:

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,18 +39,20 @@ public:
virtual ~NimBLERemoteService(); virtual ~NimBLERemoteService();
// Public methods // Public methods
NimBLERemoteCharacteristic* getCharacteristic(const char* uuid); // Get the specified characteristic reference. std::vector<NimBLERemoteCharacteristic*>::iterator begin();
NimBLERemoteCharacteristic* getCharacteristic(const NimBLEUUID &uuid); // Get the specified characteristic reference. std::vector<NimBLERemoteCharacteristic*>::iterator end();
NimBLERemoteCharacteristic* getCharacteristic(const char* 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.
std::vector<NimBLERemoteCharacteristic*>* getCharacteristics(); std::vector<NimBLERemoteCharacteristic*>* getCharacteristics();
// void getCharacteristics(std::map<uint16_t, BLERemoteCharacteristic*>* pCharacteristicMap); // void getCharacteristics(std::map<uint16_t, BLERemoteCharacteristic*>* pCharacteristicMap);
NimBLEClient* getClient(void); // Get a reference to the client associated with this service. NimBLEClient* getClient(void); // Get a reference to the client associated with this service.
uint16_t getHandle(); // Get the handle of this service. uint16_t getHandle(); // Get the handle of this service.
NimBLEUUID getUUID(void); // Get the UUID of this service. NimBLEUUID getUUID(void); // Get the UUID of this service.
std::string getValue(const NimBLEUUID &characteristicUuid); // Get the value of a characteristic. std::string getValue(const NimBLEUUID &characteristicUuid); // Get the value of a characteristic.
bool setValue(const NimBLEUUID &characteristicUuid, const std::string &value); // Set the value of a characteristic. bool setValue(const NimBLEUUID &characteristicUuid, const std::string &value); // Set the value of a characteristic.
std::string toString(void); std::string toString(void);
private: private:
// Private constructor ... never meant to be created by a user application. // Private constructor ... never meant to be created by a user application.

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

@ -41,10 +41,12 @@ class NimBLEAddress;
*/ */
class NimBLEScanResults { class NimBLEScanResults {
public: public:
void dump(); void dump();
int getCount(); int getCount();
NimBLEAdvertisedDevice getDevice(uint32_t i); NimBLEAdvertisedDevice getDevice(uint32_t i);
NimBLEAdvertisedDevice *getDevice(const NimBLEAddress &address); std::vector<NimBLEAdvertisedDevice*>::iterator begin();
std::vector<NimBLEAdvertisedDevice*>::iterator end();
NimBLEAdvertisedDevice *getDevice(const NimBLEAddress &address);
private: private:
friend NimBLEScan; friend NimBLEScan;