diff --git a/pkg/nimble/scanner/include/nimble_scanner.h b/pkg/nimble/scanner/include/nimble_scanner.h index e896c00fe7..fca8aa4b5c 100644 --- a/pkg/nimble/scanner/include/nimble_scanner.h +++ b/pkg/nimble/scanner/include/nimble_scanner.h @@ -68,6 +68,9 @@ int nimble_scanner_init(const struct ble_gap_disc_params *params, /** * @brief Start scanning using timing parameters configured on initialization + * + * @note Scanning will run for ever unless stopped or unless a different + * scan duration is set with @ref nimble_scanner_set_scan_duration */ int nimble_scanner_start(void); @@ -84,6 +87,15 @@ void nimble_scanner_stop(void); */ int nimble_scanner_status(void); +/** + * @brief Set the duration for the scanning procedure. + * + * If there is an active scanning process, it will be restarted. + * + * @param[in] duration_ms duration of scanning procedure in ms + */ +void nimble_scanner_set_scan_duration(int32_t duration_ms); + #ifdef __cplusplus } #endif diff --git a/pkg/nimble/scanner/nimble_scanner.c b/pkg/nimble/scanner/nimble_scanner.c index c81e7b75e6..b1e6ca8e37 100644 --- a/pkg/nimble/scanner/nimble_scanner.c +++ b/pkg/nimble/scanner/nimble_scanner.c @@ -32,6 +32,9 @@ static nimble_scanner_cb _disc_cb = NULL; static struct ble_gap_disc_params _scan_params = { 0 }; +/* duration of the scanning procedure */ +static int32_t _scan_duration = BLE_HS_FOREVER; + static int _on_scan_evt(struct ble_gap_event *event, void *arg) { /* only interested in the DISC event */ @@ -39,6 +42,9 @@ static int _on_scan_evt(struct ble_gap_event *event, void *arg) _disc_cb(event->disc.event_type, &event->disc.addr, event->disc.rssi, event->disc.data, (size_t)event->disc.length_data); } + else if (event->type == BLE_GAP_EVENT_DISC_COMPLETE) { + DEBUG("[scanner] scan cycle completed\n"); + } else { /* this should never happen */ DEBUG("[scanner] unknown event triggered (%i)\n", (int)event->type); @@ -51,7 +57,7 @@ static int _on_scan_evt(struct ble_gap_event *event, void *arg) int nimble_scanner_start(void) { if (ble_gap_disc_active() == 0) { - int res = ble_gap_disc(nimble_riot_own_addr_type, BLE_HS_FOREVER, + int res = ble_gap_disc(nimble_riot_own_addr_type, _scan_duration, &_scan_params, _on_scan_evt, NULL); if (res != 0) { DEBUG("[scanner] err: start failed (%i)\n", res); @@ -79,6 +85,15 @@ int nimble_scanner_status(void) : NIMBLE_SCANNER_STOPPED; } +void nimble_scanner_set_scan_duration(int32_t duration_ms) +{ + _scan_duration = duration_ms; + if (ble_gap_disc_active()) { + nimble_scanner_stop(); + nimble_scanner_start(); + } +} + int nimble_scanner_init(const struct ble_gap_disc_params *params, nimble_scanner_cb disc_cb) {