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

Advertisement data set by the programmer to be published by the BLE server. More...

#include <BLEAdvertising.hpp>

Public Types

enum  { MAX_LEN = 31 }
 

Public Member Functions

void setAppearance (uint16_t appearance)
 Set the appearance. More...
 
void setCompleteServices (BLEUUID uuid)
 Set the complete services. More...
 
void setFlags (uint8_t)
 Set the advertisement flags. More...
 
void setManufacturerData (std::string data)
 Set manufacturer specific data. More...
 
void setName (std::string name)
 Set the name. More...
 
void setPartialServices (BLEUUID uuid)
 Set the partial services. More...
 
void setServiceData (BLEUUID uuid, std::string data)
 Set the service data (UUID + data) More...
 
void setShortName (std::string name)
 Set the short name. More...
 
void addData (std::string data)
 Add data to the payload to be advertised. More...
 
std::string getPayload ()
 Retrieve the payload that is to be advertised. More...
 

Detailed Description

Advertisement data set by the programmer to be published by the BLE server.

Member Function Documentation

◆ addData()

void BLEAdvertisementData::addData ( std::string  data)

Add data to the payload to be advertised.

Parameters
[in]dataThe data to be added to the payload.
74  {
75  if ((m_payload.length() + data.length()) > MAX_LEN) {
76  return;
77  }
78  m_payload.append(data);
79 } // addData

◆ getPayload()

std::string BLEAdvertisementData::getPayload ( )

Retrieve the payload that is to be advertised.

Returns
The payload that is to be advertised.
276  {
277  return m_payload;
278 } // getPayload

◆ setAppearance()

void BLEAdvertisementData::setAppearance ( uint16_t  appearance)

Set the appearance.

Parameters
[in]appearanceThe appearance code value.

See also: https://www.bluetooth.com/specifications/gatt/viewer?attributeXmlFile=org.bluetooth.characteristic.gap.appearance.xml

89  {
90  char cdata[2];
91  cdata[0] = 3;
92  cdata[1] = BLE_AD_TYPE_APPEARANCE; // 0x19
93  addData(std::string(cdata, 2) + std::string((char*) &appearance, 2));
94 } // setAppearance

◆ setCompleteServices()

void BLEAdvertisementData::setCompleteServices ( BLEUUID  uuid)

Set the complete services.

Parameters
[in]uuidThe single service to advertise.
101  {
102  char cdata[2];
103  switch (uuid.bitSize()) {
104  case 16: {
105  // [Len] [0x02] [LL] [HH]
106  cdata[0] = 3;
107  cdata[1] = BLE_AD_TYPE_16SRV_CMPL; // 0x03
108  addData(std::string(cdata, 2) + std::string((char*) &uuid.raw16, 2));
109  break;
110  }
111 
112  case 32: {
113  // [Len] [0x04] [LL] [LL] [HH] [HH]
114  cdata[0] = 5;
115  cdata[1] = BLE_AD_TYPE_32SRV_CMPL; // 0x05
116  addData(std::string(cdata, 2) + std::string((char*) &uuid.raw32, 4));
117  break;
118  }
119 
120  case 128: {
121  // [Len] [0x04] [0] [1] ... [15]
122  cdata[0] = 17;
123  cdata[1] = BLE_AD_TYPE_128SRV_CMPL; // 0x07
124  addData(std::string(cdata, 2) + std::string((char*) uuid.raw128, 16));
125  break;
126  }
127 
128  default:
129  return;
130  }
131 } // setCompleteServices

◆ setFlags()

void BLEAdvertisementData::setFlags ( uint8_t  flag)

Set the advertisement flags.

Parameters
[in]Theflags to be set in the advertisement.
  • BLE_ADV_FLAG_LIMIT_DISC
  • BLE_ADV_FLAG_GEN_DISC
  • BLE_ADV_FLAG_BREDR_NOT_SPT
  • BLE_ADV_FLAG_DMT_CONTROLLER_SPT
  • BLE_ADV_FLAG_DMT_HOST_SPT
  • BLE_ADV_FLAG_NON_LIMIT_DISC
145  {
146  char cdata[3];
147  cdata[0] = 2;
148  cdata[1] = BLE_AD_TYPE_FLAG; // 0x01
149  cdata[2] = flag;
150  addData(std::string(cdata, 3));
151 } // setFlag

◆ setManufacturerData()

void BLEAdvertisementData::setManufacturerData ( std::string  data)

Set manufacturer specific data.

Parameters
[in]dataManufacturer data.
159  {
160  _debug_print("BLEAdvertisementData", ">> setManufacturerData");
161  char cdata[2];
162  cdata[0] = data.length() + 1;
163  cdata[1] = BLE_AD_TYPE_MANUFACTURER_SPECIFIC; // 0xff
164  addData(std::string(cdata, 2) + data);
165  _debug_print("BLEAdvertisementData", "<< setManufacturerData");
166 } // setManufacturerData

◆ setName()

void BLEAdvertisementData::setName ( std::string  name)

Set the name.

Parameters
[in]Thecomplete name of the device.
173  {
174  _debug_print_s("BLEAdvertisementData", ">> setName", name.c_str());
175  char cdata[2];
176  cdata[0] = name.length() + 1;
177  cdata[1] = BLE_AD_TYPE_NAME_CMPL; // 0x09
178  addData(std::string(cdata, 2) + name);
179  _debug_print("BLEAdvertisementData", "<< setName");
180 } // setName

◆ setPartialServices()

void BLEAdvertisementData::setPartialServices ( BLEUUID  uuid)

Set the partial services.

Parameters
[in]uuidThe single service to advertise.
187  {
188  char cdata[2];
189  switch (uuid.bitSize()) {
190  case 16: {
191  // [Len] [0x02] [LL] [HH]
192  cdata[0] = 3;
193  cdata[1] = BLE_AD_TYPE_16SRV_PART; // 0x02
194  addData(std::string(cdata, 2) + std::string((char *) &uuid.raw16, 2));
195  break;
196  }
197 
198  case 32: {
199  // [Len] [0x04] [LL] [LL] [HH] [HH]
200  cdata[0] = 5;
201  cdata[1] = BLE_AD_TYPE_32SRV_PART; // 0x04
202  addData(std::string(cdata, 2) + std::string((char *) &uuid.raw32, 4));
203  break;
204  }
205 
206  case 128: {
207  // [Len] [0x04] [0] [1] ... [15]
208  cdata[0] = 17;
209  cdata[1] = BLE_AD_TYPE_128SRV_PART; // 0x06
210  addData(std::string(cdata, 2) + std::string((char *) uuid.raw128, 16));
211  break;
212  }
213 
214  default:
215  return;
216  }
217 } // setPartialServices

◆ setServiceData()

void BLEAdvertisementData::setServiceData ( BLEUUID  uuid,
std::string  data 
)

Set the service data (UUID + data)

Parameters
[in]uuidThe UUID to set with the service data. Size of UUID will be used.
[in]dataThe data to be associated with the service data advert.
225  {
226  char cdata[2];
227  switch (uuid.bitSize()) {
228  case 16: {
229  // [Len] [0x16] [UUID16] data
230  cdata[0] = data.length() + 3;
231  cdata[1] = BLE_AD_TYPE_SERVICE_DATA; // 0x16
232  addData(std::string(cdata, 2) + std::string((char*) &uuid.raw16, 2) + data);
233  break;
234  }
235 
236  case 32: {
237  // [Len] [0x20] [UUID32] data
238  cdata[0] = data.length() + 5;
239  cdata[1] = BLE_AD_TYPE_32SERVICE_DATA; // 0x20
240  addData(std::string(cdata, 2) + std::string((char*) &uuid.raw32, 4) + data);
241  break;
242  }
243 
244  case 128: {
245  // [Len] [0x21] [UUID128] data
246  cdata[0] = data.length() + 17;
247  cdata[1] = BLE_AD_TYPE_128SERVICE_DATA; // 0x21
248  addData(std::string(cdata, 2) + std::string((char*) uuid.raw128, 16) + data);
249  break;
250  }
251 
252  default:
253  return;
254  }
255 } // setServiceData

◆ setShortName()

void BLEAdvertisementData::setShortName ( std::string  name)

Set the short name.

Parameters
[in]Theshort name of the device.
262  {
263  _debug_print_s("BLEAdvertisementData", ">> setShortName", name.c_str());
264  char cdata[2];
265  cdata[0] = name.length() + 1;
266  cdata[1] = BLE_AD_TYPE_NAME_SHORT; // 0x08
267  addData(std::string(cdata, 2) + name);
268  _debug_print("BLEAdvertisementData", "<< setShortName");
269 } // setShortName

The documentation for this class was generated from the following files:
BLEAdvertisementData::addData
void addData(std::string data)
Add data to the payload to be advertised.
Definition: BLEAdvertising.cpp:74
BLEUUID::bitSize
uint8_t bitSize()
Get the number of bits in this uuid.
Definition: BLEUUID.cpp:179