SDK  23.9.2
For IoT System Software Development
Public Member Functions | List of all members
BLERemoteService Class Reference

A model of a remote BLE service. More...

#include <BLERemoteService.hpp>

Public Member Functions

BLERemoteCharacteristicgetCharacteristic (const char *uuid)
 Get the remote characteristic object for the characteristic UUID. More...
 
BLERemoteCharacteristicgetCharacteristic (BLEUUID uuid)
 Get the characteristic object for the UUID. More...
 
BLERemoteCharacteristicgetCharacteristic (uint16_t uuid)
 
std::map< std::string, BLERemoteCharacteristic * > * getCharacteristics ()
 Retrieve a map of all the characteristics of this service. More...
 
std::map< uint16_t, BLERemoteCharacteristic * > * getCharacteristicsByHandle ()
 Retrieve a map of all the characteristics of this service. More...
 
void getCharacteristics (std::map< uint16_t, BLERemoteCharacteristic * > *pCharacteristicMap)
 This function is designed to get characteristics map when we have multiple characteristics with the same UUID.
 
BLEClientgetClient (void)
 Get the client associated with this service. More...
 
uint16_t getHandle ()
 
BLEUUID getUUID (void)
 
std::string getValue (BLEUUID characteristicUuid)
 Read the value of a characteristic associated with this service.
 
void setValue (BLEUUID characteristicUuid, std::string value)
 Set the value of a characteristic. More...
 
std::string toString (void)
 Create a string representation of this remote service. More...
 

Detailed Description

A model of a remote BLE service.

Member Function Documentation

◆ getCharacteristic() [1/2]

BLERemoteCharacteristic * BLERemoteService::getCharacteristic ( BLEUUID  uuid)

Get the characteristic object for the UUID.

Parameters
[in]uuidCharacteristic uuid.
Returns
Reference to the characteristic object.
Exceptions
BLEUuidNotFoundException
64  {
65 // Design
66 // ------
67 // We wish to retrieve the characteristic given its UUID. It is possible that we have not yet asked the
68 // device what characteristics it has in which case we have nothing to match against. If we have not
69 // asked the device about its characteristics, then we do that now. Once we get the results we can then
70 // examine the characteristics map to see if it has the characteristic we are looking for.
71  if (!m_haveCharacteristics) {
72  retrieveCharacteristics();
73  }
74  std::string v = uuid.toString();
75  for (auto &myPair : m_characteristicMap) {
76  if (myPair.first == v) {
77  return myPair.second;
78  }
79  }
80  // throw new BLEUuidNotFoundException(); // <-- we dont want exception here, which will cause app crash, we want to search if any characteristic can be found one after another
81  return nullptr;
82 } // getCharacteristic

◆ getCharacteristic() [2/2]

BLERemoteCharacteristic * BLERemoteService::getCharacteristic ( const char *  uuid)

Get the remote characteristic object for the characteristic UUID.

Parameters
[in]uuidRemote characteristic uuid.
Returns
Reference to the remote characteristic object.
Exceptions
BLEUuidNotFoundException
54  {
55  return getCharacteristic(BLEUUID(uuid));
56 } // getCharacteristic

◆ getCharacteristics()

std::map< std::string, BLERemoteCharacteristic * > * BLERemoteService::getCharacteristics ( )

Retrieve a map of all the characteristics of this service.

Returns
A map of all the characteristics of this service.
89  {
90  _debug_print_s(TAG, ">> getCharacteristics() for service", getUUID().toString().c_str());
91  // If is possible that we have not read the characteristics associated with the service so do that
92  // now. The request to retrieve the characteristics by calling "retrieveCharacteristics" is a blocking
93  // call and does not return until all the characteristics are available.
94  if (!m_haveCharacteristics) {
95  retrieveCharacteristics();
96  }
97  _debug_print_s(TAG, "<< getCharacteristics() for service", getUUID().toString().c_str());
98  return &m_characteristicMap;
99 } // getCharacteristics

◆ getCharacteristicsByHandle()

std::map< uint16_t, BLERemoteCharacteristic * > * BLERemoteService::getCharacteristicsByHandle ( )

Retrieve a map of all the characteristics of this service.

Returns
A map of all the characteristics of this service.
105  {
106  _debug_print_s(TAG, ">> getCharacteristicsByHandle() for service", getUUID().toString().c_str());
107  // If is possible that we have not read the characteristics associated with the service so do that
108  // now. The request to retrieve the characteristics by calling "retrieveCharacteristics" is a blocking
109  // call and does not return until all the characteristics are available.
110  if (!m_haveCharacteristics) {
111  retrieveCharacteristics();
112  }
113  _debug_print_s(TAG, "<< getCharacteristicsByHandle() for service", getUUID().toString().c_str());
114  return &m_characteristicMapByHandle;
115 } // getCharacteristicsByHandle

◆ getClient()

BLEClient * BLERemoteService::getClient ( void  )

Get the client associated with this service.

Returns
A reference to the client associated with this service.
129  {
130  return m_pClient;
131 } // getClient

◆ setValue()

void BLERemoteService::setValue ( BLEUUID  characteristicUuid,
std::string  value 
)

Set the value of a characteristic.

Parameters
[in]characteristicUuidThe characteristic to set.
[in]valueThe value to set.
Exceptions
BLEUuidNotFound
189  {
190  _debug_print_s(TAG, ">> setValue: uuid", characteristicUuid.toString().c_str());
191  getCharacteristic(characteristicUuid)->writeValue(value);
192  _debug_print(TAG, "<< setValue");
193 } // setValue

◆ toString()

std::string BLERemoteService::toString ( void  )

Create a string representation of this remote service.

Returns
A string representation of this remote service.
200  {
201  std::ostringstream ss;
202  ss << "Service: uuid: " + m_uuid.toString();
203  ss << ", start_handle: " << std::dec << m_startHandle << " 0x" << std::hex << m_startHandle <<
204  ", end_handle: " << std::dec << m_endHandle << " 0x" << std::hex << m_endHandle;
205  for (auto &myPair : m_characteristicMap) {
206  ss << "\n" << myPair.second->toString();
207  // myPair.second is the value
208  }
209  return ss.str();
210 } // toString

The documentation for this class was generated from the following files:
BLEUUID::toString
std::string toString()
Get a string representation of the UUID.
Definition: BLEUUID.cpp:317
BLEUUID
A model of a BLE UUID.
Definition: BLEUUID.hpp:41
BLERemoteService::getCharacteristic
BLERemoteCharacteristic * getCharacteristic(const char *uuid)
Get the remote characteristic object for the characteristic UUID.
Definition: BLERemoteService.cpp:54
BLERemoteService::toString
std::string toString(void)
Create a string representation of this remote service.
Definition: BLERemoteService.cpp:200