16
16
#include < sstream>
17
17
#include " BLEAdvertisedDevice.h"
18
18
#include " BLEUtils.h"
19
+ #if defined(ARDUINO_ARCH_ESP32) && defined(CONFIG_ARDUHAL_ESP_LOG)
19
20
#include " esp32-hal-log.h"
21
+ #define LOG_TAG " "
22
+ #else
23
+ #include " esp_log.h"
24
+ static const char * LOG_TAG=" BLEAdvertisedDevice" ;
25
+ #endif
20
26
21
27
BLEAdvertisedDevice::BLEAdvertisedDevice () {
22
28
m_adFlag = 0 ;
@@ -25,8 +31,7 @@ BLEAdvertisedDevice::BLEAdvertisedDevice() {
25
31
m_manufacturerData = " " ;
26
32
m_name = " " ;
27
33
m_rssi = -9999 ;
28
- m_serviceData = {};
29
- m_serviceDataUUIDs = {};
34
+ m_serviceData = " " ;
30
35
m_txPower = 0 ;
31
36
m_pScan = nullptr ;
32
37
@@ -102,66 +107,33 @@ BLEScan* BLEAdvertisedDevice::getScan() {
102
107
return m_pScan;
103
108
} // getScan
104
109
105
- /* *
106
- * @brief Get the number of service data.
107
- * @return Number of service data discovered.
108
- */
109
- int BLEAdvertisedDevice::getServiceDataCount () {
110
- if (m_haveServiceData)
111
- return m_serviceData.size ();
112
- else
113
- return 0 ;
114
-
115
- } // getServiceDataCount
116
110
117
111
/* *
118
112
* @brief Get the service data.
119
113
* @return The ServiceData of the advertised device.
120
114
*/
121
115
std::string BLEAdvertisedDevice::getServiceData () {
122
- return m_serviceData[ 0 ] ;
116
+ return m_serviceData;
123
117
} // getServiceData
124
118
125
- /* *
126
- * @brief Get the service data.
127
- * @return The ServiceData of the advertised device.
128
- */
129
- std::string BLEAdvertisedDevice::getServiceData (int i) {
130
- return m_serviceData[i];
131
- } // getServiceData
132
119
133
120
/* *
134
121
* @brief Get the service data UUID.
135
122
* @return The service data UUID.
136
123
*/
137
124
BLEUUID BLEAdvertisedDevice::getServiceDataUUID () {
138
- return m_serviceDataUUIDs[ 0 ] ;
125
+ return m_serviceDataUUID ;
139
126
} // getServiceDataUUID
140
127
141
- /* *
142
- * @brief Get the service data UUID.
143
- * @return The service data UUID.
144
- */
145
- BLEUUID BLEAdvertisedDevice::getServiceDataUUID (int i) {
146
- return m_serviceDataUUIDs[i];
147
- } // getServiceDataUUID
148
128
149
129
/* *
150
130
* @brief Get the Service UUID.
151
131
* @return The Service UUID of the advertised device.
152
132
*/
153
- BLEUUID BLEAdvertisedDevice::getServiceUUID () {
133
+ BLEUUID BLEAdvertisedDevice::getServiceUUID () { // TODO Remove it eventually, is no longer useful
154
134
return m_serviceUUIDs[0 ];
155
135
} // getServiceUUID
156
136
157
- /* *
158
- * @brief Get the Service UUID.
159
- * @return The Service UUID of the advertised device.
160
- */
161
- BLEUUID BLEAdvertisedDevice::getServiceUUID (int i) {
162
- return m_serviceUUIDs[i];
163
- } // getServiceUUID
164
-
165
137
/* *
166
138
* @brief Check advertised serviced for existence required UUID
167
139
* @return Return true if service is advertised
@@ -277,7 +249,7 @@ void BLEAdvertisedDevice::parseAdvertisement(uint8_t* payload, size_t total_len)
277
249
length--;
278
250
279
251
char * pHex = BLEUtils::buildHexData (nullptr , payload, length);
280
- log_d ( " Type: 0x%.2x (%s), length: %d, data: %s" ,
252
+ ESP_LOGD (LOG_TAG, " Type: 0x%.2x (%s), length: %d, data: %s" ,
281
253
ad_type, BLEUtils::advTypeToString (ad_type), length, pHex);
282
254
free (pHex);
283
255
@@ -336,7 +308,7 @@ void BLEAdvertisedDevice::parseAdvertisement(uint8_t* payload, size_t total_len)
336
308
337
309
case ESP_BLE_AD_TYPE_SERVICE_DATA: { // Adv Data Type: 0x16 (Service Data) - 2 byte UUID
338
310
if (length < 2 ) {
339
- log_e ( " Length too small for ESP_BLE_AD_TYPE_SERVICE_DATA" );
311
+ ESP_LOGE (LOG_TAG, " Length too small for ESP_BLE_AD_TYPE_SERVICE_DATA" );
340
312
break ;
341
313
}
342
314
uint16_t uuid = *(uint16_t *)payload;
@@ -349,7 +321,7 @@ void BLEAdvertisedDevice::parseAdvertisement(uint8_t* payload, size_t total_len)
349
321
350
322
case ESP_BLE_AD_TYPE_32SERVICE_DATA: { // Adv Data Type: 0x20 (Service Data) - 4 byte UUID
351
323
if (length < 4 ) {
352
- log_e ( " Length too small for ESP_BLE_AD_TYPE_32SERVICE_DATA" );
324
+ ESP_LOGE (LOG_TAG, " Length too small for ESP_BLE_AD_TYPE_32SERVICE_DATA" );
353
325
break ;
354
326
}
355
327
uint32_t uuid = *(uint32_t *) payload;
@@ -362,7 +334,7 @@ void BLEAdvertisedDevice::parseAdvertisement(uint8_t* payload, size_t total_len)
362
334
363
335
case ESP_BLE_AD_TYPE_128SERVICE_DATA: { // Adv Data Type: 0x21 (Service Data) - 16 byte UUID
364
336
if (length < 16 ) {
365
- log_e ( " Length too small for ESP_BLE_AD_TYPE_128SERVICE_DATA" );
337
+ ESP_LOGE (LOG_TAG, " Length too small for ESP_BLE_AD_TYPE_128SERVICE_DATA" );
366
338
break ;
367
339
}
368
340
@@ -374,7 +346,7 @@ void BLEAdvertisedDevice::parseAdvertisement(uint8_t* payload, size_t total_len)
374
346
} // ESP_BLE_AD_TYPE_32SERVICE_DATA
375
347
376
348
default : {
377
- log_d ( " Unhandled type: adType: %d - 0x%.2x" , ad_type, ad_type);
349
+ ESP_LOGD (LOG_TAG, " Unhandled type: adType: %d - 0x%.2x" , ad_type, ad_type);
378
350
break ;
379
351
}
380
352
} // switch
@@ -414,7 +386,7 @@ void BLEAdvertisedDevice::setAdFlag(uint8_t adFlag) {
414
386
void BLEAdvertisedDevice::setAppearance (uint16_t appearance) {
415
387
m_appearance = appearance;
416
388
m_haveAppearance = true ;
417
- log_d ( " - appearance: %d" , m_appearance);
389
+ ESP_LOGD (LOG_TAG, " - appearance: %d" , m_appearance);
418
390
} // setAppearance
419
391
420
392
@@ -426,7 +398,7 @@ void BLEAdvertisedDevice::setManufacturerData(std::string manufacturerData) {
426
398
m_manufacturerData = manufacturerData;
427
399
m_haveManufacturerData = true ;
428
400
char * pHex = BLEUtils::buildHexData (nullptr , (uint8_t *) m_manufacturerData.data (), (uint8_t ) m_manufacturerData.length ());
429
- log_d ( " - manufacturer data: %s" , pHex);
401
+ ESP_LOGD (LOG_TAG, " - manufacturer data: %s" , pHex);
430
402
free (pHex);
431
403
} // setManufacturerData
432
404
@@ -438,7 +410,7 @@ void BLEAdvertisedDevice::setManufacturerData(std::string manufacturerData) {
438
410
void BLEAdvertisedDevice::setName (std::string name) {
439
411
m_name = name;
440
412
m_haveName = true ;
441
- log_d ( " - setName(): name: %s" , m_name.c_str ());
413
+ ESP_LOGD (LOG_TAG, " - setName(): name: %s" , m_name.c_str ());
442
414
} // setName
443
415
444
416
@@ -449,7 +421,7 @@ void BLEAdvertisedDevice::setName(std::string name) {
449
421
void BLEAdvertisedDevice::setRSSI (int rssi) {
450
422
m_rssi = rssi;
451
423
m_haveRSSI = true ;
452
- log_d ( " - setRSSI(): rssi: %d" , m_rssi);
424
+ ESP_LOGD (LOG_TAG, " - setRSSI(): rssi: %d" , m_rssi);
453
425
} // setRSSI
454
426
455
427
@@ -478,7 +450,7 @@ void BLEAdvertisedDevice::setServiceUUID(const char* serviceUUID) {
478
450
void BLEAdvertisedDevice::setServiceUUID (BLEUUID serviceUUID) {
479
451
m_serviceUUIDs.push_back (serviceUUID);
480
452
m_haveServiceUUID = true ;
481
- log_d ( " - addServiceUUID(): serviceUUID: %s" , serviceUUID.toString ().c_str ());
453
+ ESP_LOGD (LOG_TAG, " - addServiceUUID(): serviceUUID: %s" , serviceUUID.toString ().c_str ());
482
454
} // setServiceUUID
483
455
484
456
@@ -488,7 +460,7 @@ void BLEAdvertisedDevice::setServiceUUID(BLEUUID serviceUUID) {
488
460
*/
489
461
void BLEAdvertisedDevice::setServiceData (std::string serviceData) {
490
462
m_haveServiceData = true ; // Set the flag that indicates we have service data.
491
- m_serviceData. push_back ( serviceData); // Save the service data that we received.
463
+ m_serviceData = serviceData; // Save the service data that we received.
492
464
} // setServiceData
493
465
494
466
@@ -498,8 +470,7 @@ void BLEAdvertisedDevice::setServiceData(std::string serviceData) {
498
470
*/
499
471
void BLEAdvertisedDevice::setServiceDataUUID (BLEUUID uuid) {
500
472
m_haveServiceData = true ; // Set the flag that indicates we have service data.
501
- m_serviceDataUUIDs.push_back (uuid);
502
- log_d (" - addServiceDataUUID(): serviceDataUUID: %s" , uuid.toString ().c_str ());
473
+ m_serviceDataUUID = uuid;
503
474
} // setServiceDataUUID
504
475
505
476
@@ -510,7 +481,7 @@ void BLEAdvertisedDevice::setServiceDataUUID(BLEUUID uuid) {
510
481
void BLEAdvertisedDevice::setTXPower (int8_t txPower) {
511
482
m_txPower = txPower;
512
483
m_haveTXPower = true ;
513
- log_d ( " - txPower: %d" , m_txPower);
484
+ ESP_LOGD (LOG_TAG, " - txPower: %d" , m_txPower);
514
485
} // setTXPower
515
486
516
487
@@ -519,31 +490,23 @@ void BLEAdvertisedDevice::setTXPower(int8_t txPower) {
519
490
* @return A string representation of this device.
520
491
*/
521
492
std::string BLEAdvertisedDevice::toString () {
522
- std::string res = " Name: " + getName () + " , Address: " + getAddress ().toString ();
493
+ std::stringstream ss;
494
+ ss << " Name: " << getName () << " , Address: " << getAddress ().toString ();
523
495
if (haveAppearance ()) {
524
- char val[6 ];
525
- snprintf (val, sizeof (val), " %d" , getAppearance ());
526
- res += " , appearance: " ;
527
- res += val;
496
+ ss << " , appearance: " << getAppearance ();
528
497
}
529
498
if (haveManufacturerData ()) {
530
499
char *pHex = BLEUtils::buildHexData (nullptr , (uint8_t *)getManufacturerData ().data (), getManufacturerData ().length ());
531
- res += " , manufacturer data: " ;
532
- res += pHex;
500
+ ss << " , manufacturer data: " << pHex;
533
501
free (pHex);
534
502
}
535
503
if (haveServiceUUID ()) {
536
- for (int i=0 ; i < m_serviceUUIDs.size (); i++) {
537
- res += " , serviceUUID: " + getServiceUUID (i).toString ();
538
- }
504
+ ss << " , serviceUUID: " << getServiceUUID ().toString ();
539
505
}
540
506
if (haveTXPower ()) {
541
- char val[4 ];
542
- snprintf (val, sizeof (val), " %d" , getTXPower ());
543
- res += " , txPower: " ;
544
- res += val;
507
+ ss << " , txPower: " << (int )getTXPower ();
545
508
}
546
- return res ;
509
+ return ss. str () ;
547
510
} // toString
548
511
549
512
uint8_t * BLEAdvertisedDevice::getPayload () {
@@ -562,5 +525,8 @@ size_t BLEAdvertisedDevice::getPayloadLength() {
562
525
return m_payloadLength;
563
526
}
564
527
528
+ void BLEAdvertisedDeviceCallbacks::onResult (BLEAdvertisedDevice dev) {}
529
+ void BLEAdvertisedDeviceCallbacks::onResult (BLEAdvertisedDevice* dev) {}
530
+
565
531
#endif /* CONFIG_BT_ENABLED */
566
532
0 commit comments