@@ -35,20 +35,6 @@ BLEAdvertising::BLEAdvertising() {
35
35
m_advData.p_service_uuid = nullptr ;
36
36
m_advData.flag = (ESP_BLE_ADV_FLAG_GEN_DISC | ESP_BLE_ADV_FLAG_BREDR_NOT_SPT);
37
37
38
- m_advDataScanResponse.set_scan_rsp = true ;
39
- m_advDataScanResponse.include_name = false ;
40
- m_advDataScanResponse.include_txpower = false ;
41
- m_advDataScanResponse.min_interval = 0x20 ;
42
- m_advDataScanResponse.max_interval = 0x40 ;
43
- m_advDataScanResponse.appearance = 0x00 ;
44
- m_advDataScanResponse.manufacturer_len = 0 ;
45
- m_advDataScanResponse.p_manufacturer_data = nullptr ;
46
- m_advDataScanResponse.service_data_len = 0 ;
47
- m_advDataScanResponse.p_service_data = nullptr ;
48
- m_advDataScanResponse.service_uuid_len = 0 ;
49
- m_advDataScanResponse.p_service_uuid = nullptr ;
50
- m_advDataScanResponse.flag = (ESP_BLE_ADV_FLAG_GEN_DISC | ESP_BLE_ADV_FLAG_BREDR_NOT_SPT);
51
-
52
38
m_advParams.adv_int_min = 0x20 ;
53
39
m_advParams.adv_int_max = 0x40 ;
54
40
m_advParams.adv_type = ADV_TYPE_IND;
@@ -58,6 +44,24 @@ BLEAdvertising::BLEAdvertising() {
58
44
} // BLEAdvertising
59
45
60
46
47
+ /* *
48
+ * @brief Add a service uuid to exposed list of services.
49
+ * @param [in] serviceUUID The UUID of the service to expose.
50
+ */
51
+ void BLEAdvertising::addServiceUUID (BLEUUID serviceUUID) {
52
+ m_serviceUUIDs.push_back (serviceUUID);
53
+ } // addServiceUUID
54
+
55
+
56
+ /* *
57
+ * @brief Add a service uuid to exposed list of services.
58
+ * @param [in] serviceUUID The string representation of the service to expose.
59
+ */
60
+ void BLEAdvertising::addServiceUUID (const char * serviceUUID) {
61
+ addServiceUUID (BLEUUID (serviceUUID));
62
+ } // addServiceUUID
63
+
64
+
61
65
/* *
62
66
* @brief Set the device appearance in the advertising data.
63
67
* The appearance attribute is of type 0x19. The codes for distinct appearances can be found here:
@@ -70,38 +74,6 @@ void BLEAdvertising::setAppearance(uint16_t appearance) {
70
74
} // setAppearance
71
75
72
76
73
- /* *
74
- * @brief Set the service UUID.
75
- * We maintain a class member called m_advData (esp_ble_adv_data_t) that is passed to the
76
- * ESP-IDF advertising functions. In this method, we see two fields within that structure
77
- * namely service_uuid_len and p_service_uuid to be the information supplied in the passed
78
- * in service uuid.
79
- * @param [in] uuid The UUID of the service.
80
- * @return N/A.
81
- */
82
- void BLEAdvertising::setServiceUUID (const char * serviceUUID) {
83
- return setServiceUUID (BLEUUID (serviceUUID));
84
- }
85
- /* *
86
- * @brief Set the service UUID.
87
- * We maintain a class member called m_advData (esp_ble_adv_data_t) that is passed to the
88
- * ESP-IDF advertising functions. In this method, we see two fields within that structure
89
- * namely service_uuid_len and p_service_uuid to be the information supplied in the passed
90
- * in service uuid.
91
- * @param [in] uuid The UUID of the service.
92
- * @return N/A.
93
- */
94
- void BLEAdvertising::setServiceUUID (BLEUUID serviceUUID) {
95
- ESP_LOGD (LOG_TAG, " >> setServiceUUID - %s" , serviceUUID.toString ().c_str ());
96
- m_serviceUUID = serviceUUID; // Save the new service UUID
97
- m_serviceUUID128 = serviceUUID.to128 ();
98
-
99
- m_advDataScanResponse.service_uuid_len = 16 ;
100
- m_advDataScanResponse.p_service_uuid = reinterpret_cast <uint8_t *>(&m_serviceUUID128.getNative ()->uuid .uuid128 );
101
<
57AE
td data-grid-cell-id="diff-afe08a1d244f18a25298e69ad8717d594997c1384299071286c7452063190250-101-76-1" data-selected="false" role="gridcell" style="background-color:var(--diffBlob-deletionNum-bgColor, var(--diffBlob-deletion-bgColor-num));text-align:center" tabindex="-1" valign="top" class="focusable-grid-cell diff-line-number position-relative left-side">
- ESP_LOGD (LOG_TAG, " << setServiceUUID" );
102
- } // setServiceUUID
103
-
104
-
105
77
/* *
106
78
* @brief Start advertising.
107
79
* Start advertising.
@@ -110,30 +82,49 @@ void BLEAdvertising::setServiceUUID(BLEUUID serviceUUID) {
110
82
void BLEAdvertising::start () {
111
83
ESP_LOGD (LOG_TAG, " >> start" );
112
84
113
- if (m_advDataScanResponse.service_uuid_len > 0 ) {
114
- uint8_t hexData[16 *2 +1 ];
115
- BLEUtils::buildHexData (hexData, m_advDataScanResponse.p_service_uuid , m_advDataScanResponse.service_uuid_len );
116
- ESP_LOGD (LOG_TAG, " - Service: service_uuid_len=%d, p_service_uuid=0x%x (data=%s)" ,
117
- m_advDataScanResponse.service_uuid_len ,
118
- (uint32_t )m_advDataScanResponse.p_service_uuid ,
119
- (m_advDataScanResponse.service_uuid_len > 0 ?(char *)hexData:" N/A" )
120
- );
121
- } // We have a service to advertise
85
+ // We have a vector of service UUIDs that we wish to advertise. In order to use the
86
+ // ESP-IDF framework, these must be supplied in a contiguous array of their 128bit (16 byte)
87
+ // representations. If we have 1 or more services to advertise then we allocate enough
88
+ // storage to host them and then copy them in one at a time into the contiguous storage.
89
+ int numServices = m_serviceUUIDs.size ();
90
+ if (numServices > 0 ) {
91
+ m_advData.service_uuid_len = 16 *numServices;
92
+ m_advData.p_service_uuid = new uint8_t [m_advData.service_uuid_len ];
93
+ uint8_t * p = m_advData.p_service_uuid ;
94
+ for (int i=0 ; i<numServices; i++) {
95
+ ESP_LOGD (LOG_TAG, " - advertising service: %s" , m_serviceUUIDs[i].toString ().c_str ());
96
+ BLEUUID serviceUUID128 = m_serviceUUIDs[i].to128 ();
97
+ memcpy (p, serviceUUID128.getNative ()->uuid .uuid128 , 16 );
98
+ p+=16 ;
99
+ }
100
+ } else {
101
+ m_advData.service_uuid_len = 0 ;
102
+ ESP_LOGD (LOG_TAG, " - no services advertised" );
103
+ }
122
104
123
105
124
106
// Set the configuration for advertising.
107
+ m_advData.set_scan_rsp = false ;
125
108
esp_err_t errRc = ::esp_ble_gap_config_adv_data (&m_advData);
126
109
if (errRc != ESP_OK) {
127
110
ESP_LOGE (LOG_TAG, " << esp_ble_gap_config_adv_data: rc=%d %s" , errRc, GeneralUtils::errorToString (errRc));
128
111
return ;
129
112
}
130
113
131
- errRc = ::esp_ble_gap_config_adv_data (&m_advDataScanResponse);
114
+ m_advData.set_scan_rsp = true ;
115
+ errRc = ::esp_ble_gap_config_adv_data (&m_advData);
132
116
if (errRc != ESP_OK) {
133
117
ESP_LOGE (LOG_TAG, " << esp_ble_gap_config_adv_data (Scan response): rc=%d %s" , errRc, GeneralUtils::errorToString (errRc));
134
118
return ;
135
119
}
136
120
121
+ // If we had services to advertise then we previously allocated some storage for them.
122
+ // Here we release that storage.
123
+ if (m_advData.service_uuid_len > 0 ) {
124
+ delete[] m_advData.p_service_uuid ;
125
+ m_advData.p_service_uuid = nullptr ;
126
+ }
127
+
137
128
// Start advertising.
138
129
errRc = ::esp_ble_gap_start_advertising (&m_advParams);
139
130
if (errRc != ESP_OK) {
@@ -158,4 +149,6 @@ void BLEAdvertising::stop() {
158
149
}
159
150
ESP_LOGD (LOG_TAG, " << stop" );
160
151
} // stop
152
+
153
+
161
154
#endif /* CONFIG_BT_ENABLED */
0 commit comments