stop/startAdvertising do not work well for advertising interval to be shorten

I’m testing with ARMmbed/mbed-os-example/BLE_Advertising/source/main.cpp.
Working with Mbed Studio, mbed os 6 and nRF52840 DK.

When the advertising interval is changed to 100ms and _event_queue.call_every() is set to 100ms, it works well. Using the nRF Sniffer, most of advertising interval are seemd nearly 100ms, like this figure.

Attempting the advertising interval to be shorter than GAP_ADV_PARAMS_INTERVAL_MIN_NONCON (100ms), I modified _event_queue.call_every() handler.

        _event_queue.call_every(
            25ms,
            [this]() {
                update_battery_level();
                _ble.gap().stopAdvertising(ble::LEGACY_ADVERTISING_HANDLE);
                _ble.gap().startAdvertising(ble::LEGACY_ADVERTISING_HANDLE);
            }
        );

I was assuming before achieving an advertising interval 100ms, the advertising is stopped and a new advertising start at 25ms. But stopAdvertisng() adn startAdvertising() do not work effectively. The nRF Sniffer shows many of advertising interval are still about 100ms.

Any misunderstanding or mistake are there?
What should I do any more modification?

I’m assuming you mean this source code: https://github.com/ARMmbed/mbed-os-example-ble/blob/master/BLE_Advertising/source/main.cpp

Within this example, the advertising interval is defined here:

ble::AdvertisingParameters adv_parameters(
            /* you cannot connect to this device, you can only read its advertising data,
             * scannable means that the device has extra advertising data that the peer can receive if it
             * "scans" it which means it is using active scanning (it sends a scan request) */
            ble::advertising_type_t::SCANNABLE_UNDIRECTED,
            ble::adv_interval_t(ble::millisecond_t(1000))
        );

So if you want to change this to 100ms, or less change here ble::adv_interval_t(ble::millisecond_t(100))

I’m not sure stopping and starting advertising is required here. Usually you start advertising and leave it started as the battery level is updated within this function: _adv_data_builder.setServiceData(GattService::UUID_BATTERY_SERVICE, {&_battery_level, 1});

Thanks, Gerriko

Yes, I 've changed the advertising interval:

ble::adv_interval_t(ble::millisecond_t(100))

I understand that the minimum interval for Non-connectable property limited by GAP_ADV_PARAMS_INTERVAL_MIN_NONCON (100ms). I need to set a interval shorter than 100ms, so i’m trying to use stopping and starting.

Is there another idea?

No that’s a default parameter, should you not specify an advertising interval.
https://os.mbed.com/docs/mbed-os/v6.15/feature-i2c-doxy/class_gap_advertising_params.html#a1c0f0bc52e6a87281be7d16733a6c393

You can specify a much lower value. It was only the other day I used a 20ms interval and it worked fine.

Does not this code work?

(GapAdvertisingParams.h)

  140         } else if (_advType == ADV_NON_CONNECTABLE_UNDIRECTED) {
  141             /* Min interval is slightly larger than in other modes. */
  142             if (_interval < GAP_ADV_PARAMS_INTERVAL_MIN_NONCON) {
  143                 _interval = GAP_ADV_PARAMS_INTERVAL_MIN_NONCON;
  144             }
  145             if (_interval > GAP_ADV_PARAMS_INTERVAL_MAX) {
  146                 _interval = GAP_ADV_PARAMS_INTERVAL_MAX;
  147             }
  148         } else {

Cause of this, the interval cannot be shorten.

Ah, maybe this is where the confusion lies… and I wasn’t paying attention. Where did you find GapAdvertisingParams.h? If I search online it directs me to ARMmbed/ble which is out of date. All BLE libraries are already included within the mbedOS/connectivity folder (v6.15)

I also had sent the wrong link. The function to use is AdvertisingParameters

and not GapAdvertisingParams

Thanks, Gerriko

I comfirmed AdvertisingParameters.h. There is similar normalize functionality.

  600         if (_advType == advertising_type_t::NON_CONNECTABLE_UNDIRECTED) {
  601             _minInterval = adv_interval_t(std::max(_minInterval.value(), GAP_ADV_PARAMS_INTERVAL_MIN_NONCON));
  602             _maxInterval = adv_interval_t(std::max(_maxInterval.value(), GAP_ADV_PARAMS_INTERVAL_MIN_NONCON));
  603         }

Yes, that appears to be the case with NON_CONNECTABLE_UNDIRECTED. I’ve never investigated why that option differs from say SCANNABLE_UNDIRECTED, which I tend to use.

Got me puzzled…

EDIT.
So I had to find out… and came across this useful diagram online.

source: https://www.researchgate.net/figure/Non-connectable-and-scannable-undirected-advertisement-detection-procedure_fig1_323898620

From additional my investigation, I’ve made a new thread;
When the timing for advertising to be active - Mbed OS - Arm Mbed OS support forum

Thanks.

I also had sent the wrong link.

I’m not sure stopping and starting advertising is required here. . .