@@ -78,7 +78,7 @@ class PeriodicDemo : private mbed::NonCopyable<PeriodicDemo>, public ble::Gap::E
78
78
79
79
ble_error_t error = _ble.init (this , &PeriodicDemo::on_init_complete);
80
80
if (error) {
81
- print_error (error, " Error returned by BLE::init" );
81
+ print_error (error, " Error returned by BLE::init\r\n " );
82
82
return ;
83
83
}
84
84
@@ -94,13 +94,13 @@ class PeriodicDemo : private mbed::NonCopyable<PeriodicDemo>, public ble::Gap::E
94
94
void on_init_complete (BLE::InitializationCompleteCallbackContext *event)
95
95
{
96
96
if (event->error ) {
97
- print_error (event->error , " Error during the initialisation" );
97
+ print_error (event->error , " Error during the initialisation\r\n " );
98
98
return ;
99
99
}
100
100
101
101
if (!_gap.isFeatureSupported (ble::controller_supported_features_t ::LE_EXTENDED_ADVERTISING) ||
102
102
!_gap.isFeatureSupported (ble::controller_supported_features_t ::LE_PERIODIC_ADVERTISING)) {
103
- printf (" Periodic advertising not supported, cannot run example." );
103
+ printf (" Periodic advertising not supported, cannot run example.\r\n " );
104
104
return ;
105
105
}
106
106
@@ -125,8 +125,10 @@ class PeriodicDemo : private mbed::NonCopyable<PeriodicDemo>, public ble::Gap::E
125
125
_is_scanner = !_is_scanner;
126
126
127
127
if (_is_scanner) {
128
+ if (_adv_handle != ble::INVALID_ADVERTISING_HANDLE) _ble.gap ().stopAdvertising (_adv_handle);
128
129
_event_queue.call (this , &PeriodicDemo::scan);
129
130
} else {
131
+ _ble.gap ().stopScan ();
130
132
_event_queue.call (this , &PeriodicDemo::advertise);
131
133
}
132
134
}
@@ -135,20 +137,25 @@ class PeriodicDemo : private mbed::NonCopyable<PeriodicDemo>, public ble::Gap::E
135
137
/* * Set up and start advertising */
136
138
void advertise ()
137
139
{
140
+ ble_error_t error;
141
+
138
142
ble::AdvertisingParameters adv_params;
139
143
adv_params.setUseLegacyPDU (false );
140
144
141
- /* create the advertising set with its parameter */
142
- ble_error_t error = _gap.createAdvertisingSet (
143
- &_adv_handle,
144
- adv_params
145
- );
145
+ /* create the advertising set with its parameter if we haven't yet */
146
+ if (_adv_handle == ble::INVALID_ADVERTISING_HANDLE) {
147
+ error = _gap.createAdvertisingSet (
148
+ &_adv_handle,
149
+ adv_params
150
+ );
146
151
147
- if (error) {
148
- print_error (error, " Gap::createAdvertisingSet() failed" );
149
- return ;
152
+ if (error) {
153
+ print_error (error, " Gap::createAdvertisingSet() failed\r\n " );
154
+ return ;
155
+ }
150
156
}
151
157
158
+ _adv_data_builder.clear ();
152
159
_adv_data_builder.setFlags ();
153
160
_adv_data_builder.setName (DEVICE_NAME);
154
161
@@ -159,7 +166,7 @@ class PeriodicDemo : private mbed::NonCopyable<PeriodicDemo>, public ble::Gap::E
159
166
);
160
167
161
168
if (error) {
162
- print_error (error, " Gap::setAdvertisingPayload() failed" );
169
+ print_error (error, " Gap::setAdvertisingPayload() failed\r\n " );
163
170
return ;
164
171
}
165
172
@@ -170,12 +177,13 @@ class PeriodicDemo : private mbed::NonCopyable<PeriodicDemo>, public ble::Gap::E
170
177
ble::adv_duration_t random_duration (random_duration_ms);
171
178
172
179
error = _ble.gap ().startAdvertising (
173
- _adv_handle,
174
- random_duration
180
+ _adv_handle/* ,
181
+ random_duration*/
182
+ );
175
183
);
176
184
177
185
if (error) {
178
- print_error (error, " Gap::startAdvertising() failed" );
186
+ print_error (error, " Gap::startAdvertising() failed\r\n " );
179
187
return ;
180
188
}
181
189
@@ -190,7 +198,7 @@ class PeriodicDemo : private mbed::NonCopyable<PeriodicDemo>, public ble::Gap::E
190
198
ble_error_t error = _gap.startAdvertising (_adv_handle);
191
199
192
200
if (error) {
193
- print_error (error, " Gap::startAdvertising() failed" );
201
+ print_error (error, " Gap::startAdvertising() failed\r\n " );
194
202
return ;
195
203
}
196
204
@@ -201,7 +209,7 @@ class PeriodicDemo : private mbed::NonCopyable<PeriodicDemo>, public ble::Gap::E
201
209
);
202
210
203
211
if (error) {
204
- print_error (error, " Gap::setPeriodicAdvertisingParameters() failed" );
212
+ print_error (error, " Gap::setPeriodicAdvertisingParameters() failed\r\n " );
205
213
return ;
206
214
}
207
215
@@ -211,7 +219,7 @@ class PeriodicDemo : private mbed::NonCopyable<PeriodicDemo>, public ble::Gap::E
211
219
error = _gap.startPeriodicAdvertising (_adv_handle);
212
220
213
221
if (error) {
214
- print_error (error, " Gap::startPeriodicAdvertising() failed" );
222
+ print_error (error, " Gap::startPeriodicAdvertising() failed\r\n " );
215
223
return ;
216
224
}
217
225
@@ -230,7 +238,7 @@ class PeriodicDemo : private mbed::NonCopyable<PeriodicDemo>, public ble::Gap::E
230
238
);
231
239
232
240
if (error) {
233
- print_error (error, " AdvertisingDataBuilder::setFlags() failed" );
241
+ print_error (error, " AdvertisingDataBuilder::setFlags() failed\r\n " );
234
242
return ;
235
243
}
236
244
@@ -242,7 +250,7 @@ class PeriodicDemo : private mbed::NonCopyable<PeriodicDemo>, public ble::Gap::E
242
250
);
243
251
244
252
if (error) {
245
- print_error (error, " Gap::setPeriodicAdvertisingPayload() failed" );
253
+ print_error (error, " Gap::setPeriodicAdvertisingPayload() failed\r\n " );
246
254
return ;
247
255
}
248
256
}
@@ -257,7 +265,7 @@ class PeriodicDemo : private mbed::NonCopyable<PeriodicDemo>, public ble::Gap::E
257
265
);
258
266
259
267
if (error) {
260
- print_error (error, " Error caused by Gap::setScanParameters" );
268
+ print_error (error, " Error caused by Gap::setScanParameters\r\n " );
261
269
return ;
262
270
}
263
271
@@ -266,7 +274,7 @@ class PeriodicDemo : private mbed::NonCopyable<PeriodicDemo>, public ble::Gap::E
266
274
);
267
275
268
276
if (error) {
269
- print_error (error, " Error caused by Gap::startScan" );
277
+ print_error (error, " Error caused by Gap::startScan\r\n " );
270
278
return ;
271
279
}
272
280
@@ -280,7 +288,7 @@ class PeriodicDemo : private mbed::NonCopyable<PeriodicDemo>, public ble::Gap::E
280
288
ble_error_t error = _gap.startScan ();
281
289
282
290
if (error) {
283
- print_error (error, " Error caused by Gap::startScan" );
291
+ print_error (error, " Error caused by Gap::startScan\r\n " );
284
292
return ;
285
293
}
286
294
@@ -408,7 +416,7 @@ class PeriodicDemo : private mbed::NonCopyable<PeriodicDemo>, public ble::Gap::E
408
416
if (event.getStatus () == BLE_ERROR_NONE) {
409
417
printf (" Synced with periodic advertising\r\n " );
410
418
} else {
411
- _event_queue. call ( this , &PeriodicDemo::scan_periodic );
419
+ _sync_handle = event. getSyncHandle ( );
412
420
}
413
421
}
414
422
@@ -440,6 +448,8 @@ class PeriodicDemo : private mbed::NonCopyable<PeriodicDemo>, public ble::Gap::E
440
448
virtual void onPeriodicAdvertisingSyncLoss (
441
449
const ble::PeriodicAdvertisingSyncLoss &event
442
450
) {
451
+ printf (" Sync to periodic advertising lost\r\n " );
452
+ _sync_handle = ble::INVALID_ADVERTISING_HANDLE;
443
453
_event_queue.call (this , &PeriodicDemo::scan_periodic);
444
454
}
445
455
0 commit comments