@@ -128,20 +128,22 @@ class BLEProcess : private mbed::NonCopyable<BLEProcess> {
128
128
printf (" Ble instance initialized\r\n " );
129
129
130
130
Gap &gap = _ble_interface.gap ();
131
- ble_error_t error = gap.setAdvertisingPayload (make_advertising_data ());
132
- if (error) {
133
- printf (" Error %u during gap.setAdvertisingPayload\r\n " , error);
131
+ gap.onConnection (this , &BLEProcess::when_connection);
132
+ gap.onDisconnection (this , &BLEProcess::when_disconnection);
133
+
134
+ if (!set_advertising_parameters ()) {
134
135
return ;
135
136
}
136
137
137
- gap.setAdvertisingParams (make_advertising_params ());
138
-
139
- gap.onConnection (this , &BLEProcess::when_connection);
140
- gap.onDisconnection (this , &BLEProcess::when_disconnection);
138
+ if (!set_advertising_data ()) {
139
+ return ;
140
+ }
141
141
142
- start_advertising ();
142
+ if (!start_advertising ()) {
143
+ return ;
144
+ }
143
145
144
- if (_post_init_cb) {
146
+ if (_post_init_cb) {
145
147
_post_init_cb (_ble_interface, _event_queue);
146
148
}
147
149
}
@@ -157,45 +159,59 @@ class BLEProcess : private mbed::NonCopyable<BLEProcess> {
157
159
start_advertising ();
158
160
}
159
161
160
- void start_advertising (void )
162
+ bool start_advertising (void )
161
163
{
162
- ble_error_t error = _ble_interface.gap ().startAdvertising ();
164
+ Gap &gap = _ble_interface.gap ();
165
+
166
+ /* Start advertising the set */
167
+ ble_error_t error = gap.startAdvertising (ble::LEGACY_ADVERTISING_HANDLE);
168
+
163
169
if (error) {
164
170
printf (" Error %u during gap.startAdvertising.\r\n " , error);
165
- return ;
171
+ return false ;
166
172
} else {
167
173
printf (" Advertising started.\r\n " );
174
+ return true ;
168
175
}
169
176
}
170
177
171
- static GapAdvertisingData make_advertising_data ( void )
178
+ bool set_advertising_parameters ( )
172
179
{
173
- static const uint8_t device_name[] = " GattServer" ;
174
- GapAdvertisingData advertising_data;
180
+ Gap &gap = _ble_interface.gap ();
175
181
176
- // add advertising flags
177
- advertising_data.addFlags (
178
- GapAdvertisingData::LE_GENERAL_DISCOVERABLE |
179
- GapAdvertisingData::BREDR_NOT_SUPPORTED
182
+ ble_error_t error = gap.setAdvertisingParameters (
183
+ ble::LEGACY_ADVERTISING_HANDLE,
184
+ ble::AdvertisingParameters ()
180
185
);
181
186
182
- // add device name
183
- advertising_data.addData (
184
- GapAdvertisingData::COMPLETE_LOCAL_NAME,
185
- device_name,
186
- sizeof (device_name)
187
- );
187
+ if (error) {
188
+ printf (" Gap::setAdvertisingParameters() failed with error %d" , error);
189
+ return false ;
190
+ }
188
191
189
- return advertising_data ;
192
+ return true ;
190
193
}
191
194
192
- static GapAdvertisingParams make_advertising_params ( void )
195
+ bool set_advertising_data ( )
193
196
{
194
- return GapAdvertisingParams (
195
- /* type */ GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED,
196
- /* interval */ GapAdvertisingParams::MSEC_TO_ADVERTISEMENT_DURATION_UNITS (500 ),
197
- /* timeout */ 0
197
+ Gap &gap = _ble_interface.gap ();
198
+
199
+ /* Use the simple builder to construct the payload; it fails at runtime
200
+ * if there is not enough space left in the buffer */
201
+ ble_error_t error = gap.setAdvertisingPayload (
202
+ ble::LEGACY_ADVERTISING_HANDLE,
203
+ ble::AdvertisingDataSimpleBuilder<ble::LEGACY_ADVERTISING_MAX_SIZE>()
204
+ .setFlags ()
205
+ .setName (" GattServer" )
206
+ .getAdvertisingData ()
198
207
);
208
+
209
+ if (error) {
210
+ printf (" Gap::setAdvertisingPayload() failed with error %d" , error);
211
+ return false ;
212
+ }
213
+
214
+ return true ;
199
215
}
200
216
201
217
events::EventQueue &_event_queue;
0 commit comments