@@ -253,7 +253,7 @@ BLEAdvertising::BLEAdvertising(void)
253253 _hdl = BLE_GAP_ADV_SET_HANDLE_NOT_SET;
254254 _type = BLE_GAP_ADV_TYPE_CONNECTABLE_SCANNABLE_UNDIRECTED;
255255 _start_if_disconnect = true ;
256- _runnning = false ;
256+ _running = false ;
257257
258258 _conn_mask = 0 ;
259259
@@ -321,7 +321,7 @@ void BLEAdvertising::setPeerAddress(const ble_gap_addr_t& peer_addr) {
321321
322322bool BLEAdvertising::isRunning (void )
323323{
324- return _runnning ;
324+ return _running ;
325325}
326326
327327bool BLEAdvertising::setBeacon (BLEBeacon& beacon)
@@ -339,8 +339,7 @@ void BLEAdvertising::restartOnDisconnect(bool enable)
339339 _start_if_disconnect = enable;
340340}
341341
342- bool BLEAdvertising::_start (uint16_t interval, uint16_t timeout)
343- {
342+ bool BLEAdvertising::_start (uint16_t interval, uint16_t timeout) {
344343 // ADV Params
345344 ble_gap_adv_params_t adv_para = {
346345 .properties = { .type = _type, .anonymous = 0 },
@@ -369,17 +368,24 @@ bool BLEAdvertising::_start(uint16_t interval, uint16_t timeout)
369368 default : break ;
370369 }
371370
371+ // stop first if current running since we may change advertising data/params
372+ if (_running) {
373+ sd_ble_gap_adv_stop (_hdl);
374+ }
375+
372376 // gap_adv long-live is required by SD v6
373- static ble_gap_adv_data_t gap_adv = {
374- .adv_data = { .p_data = _data, .len = _count },
375- .scan_rsp_data = { .p_data = Bluefruit.ScanResponse .getData (), .len = Bluefruit.ScanResponse .count () }
376- };
377+ static ble_gap_adv_data_t gap_adv;
378+ gap_adv.adv_data .p_data = _data;
379+ gap_adv.adv_data .len = _count;
380+ gap_adv.scan_rsp_data .p_data = Bluefruit.ScanResponse .getData ();
381+ gap_adv.scan_rsp_data .len = Bluefruit.ScanResponse .count ();
382+
377383 VERIFY_STATUS ( sd_ble_gap_adv_set_configure (&_hdl, &gap_adv, &adv_para), false );
378384 VERIFY_STATUS ( sd_ble_gap_tx_power_set (BLE_GAP_TX_POWER_ROLE_ADV, _hdl, Bluefruit.getTxPower () ), false );
379385 VERIFY_STATUS ( sd_ble_gap_adv_start (_hdl, CONN_CFG_PERIPHERAL), false );
380386
381387 Bluefruit._startConnLed (); // start blinking
382- _runnning = true ;
388+ _running = true ;
383389 _active_interval = interval;
384390
385391 _left_timeout -= min16 (_left_timeout, timeout);
@@ -403,7 +409,7 @@ bool BLEAdvertising::stop(void)
403409{
404410 VERIFY_STATUS ( sd_ble_gap_adv_stop (_hdl), false );
405411
406- _runnning = false ;
412+ _running = false ;
407413 Bluefruit._stopConnLed (); // stop blinking
408414
409415 return true ;
@@ -425,7 +431,7 @@ void BLEAdvertising::_eventHandler(ble_evt_t* evt)
425431 {
426432 bitSet (_conn_mask, conn_hdl);
427433
428- _runnning = false ;
434+ _running = false ;
429435 }
430436 }
431437 break ;
@@ -436,14 +442,14 @@ void BLEAdvertising::_eventHandler(ble_evt_t* evt)
436442 bitClear (_conn_mask, conn_hdl);
437443
438444 // Auto start if enabled and not connected to any central
439- if ( !_runnning && _start_if_disconnect ) start (_stop_timeout);
445+ if ( !_running && _start_if_disconnect ) start (_stop_timeout);
440446 }
441447 break ;
442448
443449 case BLE_GAP_EVT_ADV_SET_TERMINATED:
444450 if (evt->evt .gap_evt .params .adv_set_terminated .reason == BLE_GAP_EVT_ADV_SET_TERMINATED_REASON_TIMEOUT)
445451 {
446- _runnning = false ;
452+ _running = false ;
447453
448454 // If still advertising, it is only in slow mode --> blink normal
449455 Bluefruit.setConnLedInterval (CFG_ADV_BLINKY_INTERVAL);
0 commit comments