diff --git a/docs/library/ubluetooth.rst b/docs/library/ubluetooth.rst index e103932efd2f6..445b25c79b79a 100644 --- a/docs/library/ubluetooth.rst +++ b/docs/library/ubluetooth.rst @@ -204,7 +204,7 @@ Broadcaster Role (Advertiser) Observer Role (Scanner) ----------------------- -.. method:: BLE.gap_scan(duration_ms, [interval_us], [window_us]) +.. method:: BLE.gap_scan(duration_ms, [interval_us, [window_us, [active]]]) Run a scan operation lasting for the specified duration (in **milli**\ seconds). @@ -218,6 +218,10 @@ Observer Role (Scanner) interval and window are 1.28 seconds and 11.25 milliseconds respectively (background scanning). + Use *active* set to ``True`` to initiate an active scan instead of a passive + one (the default). Each discovered device will be automatically scanned and any + active scan response will be raised as an event as below. + For each scan result the ``_IRQ_SCAN_RESULT`` event will be raised, with event data ``(addr_type, addr, adv_type, rssi, adv_data)``. ``adv_type`` values correspond to the Bluetooth Specification: @@ -226,7 +230,7 @@ Observer Role (Scanner) * 0x01 - ADV_DIRECT_IND - connectable directed advertising * 0x02 - ADV_SCAN_IND - scannable undirected advertising * 0x03 - ADV_NONCONN_IND - non-connectable undirected advertising - * 0x04 - SCAN_RSP - scan response + * 0x04 - SCAN_RSP - active scan response When scanning is stopped (either due to the duration finishing or when explicitly stopped), the ``_IRQ_SCAN_DONE`` event will be raised. diff --git a/extmod/modbluetooth.c b/extmod/modbluetooth.c index 7bfb774782048..410d8a246b372 100644 --- a/extmod/modbluetooth.c +++ b/extmod/modbluetooth.c @@ -600,6 +600,7 @@ STATIC mp_obj_t bluetooth_ble_gap_scan(size_t n_args, const mp_obj_t *args) { mp_int_t duration_ms = 0; mp_int_t interval_us = 1280000; mp_int_t window_us = 11250; + mp_int_t active = 0; if (n_args > 1) { if (args[1] == mp_const_none) { // scan(None) --> stop scan. @@ -610,12 +611,15 @@ STATIC mp_obj_t bluetooth_ble_gap_scan(size_t n_args, const mp_obj_t *args) { interval_us = mp_obj_get_int(args[2]); if (n_args > 3) { window_us = mp_obj_get_int(args[3]); + if (n_args > 4) { + active = mp_obj_get_int(args[4]); + } } } } - return bluetooth_handle_errno(mp_bluetooth_gap_scan_start(duration_ms, interval_us, window_us)); + return bluetooth_handle_errno(mp_bluetooth_gap_scan_start(duration_ms, interval_us, window_us, active)); } -STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(bluetooth_ble_gap_scan_obj, 1, 4, bluetooth_ble_gap_scan); +STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(bluetooth_ble_gap_scan_obj, 1, 5, bluetooth_ble_gap_scan); #endif // MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE STATIC mp_obj_t bluetooth_ble_gap_disconnect(mp_obj_t self_in, mp_obj_t conn_handle_in) { diff --git a/extmod/modbluetooth.h b/extmod/modbluetooth.h index cdb86e5e63c44..259c289ddca21 100644 --- a/extmod/modbluetooth.h +++ b/extmod/modbluetooth.h @@ -215,7 +215,7 @@ int mp_bluetooth_gap_disconnect(uint16_t conn_handle); #if MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE // Start a discovery (scan). Set duration to zero to run continuously. -int mp_bluetooth_gap_scan_start(int32_t duration_ms, int32_t interval_us, int32_t window_us); +int mp_bluetooth_gap_scan_start(int32_t duration_ms, int32_t interval_us, int32_t window_us, int32_t active); // Stop discovery (if currently active). int mp_bluetooth_gap_scan_stop(void); diff --git a/extmod/nimble/modbluetooth_nimble.c b/extmod/nimble/modbluetooth_nimble.c index 843989b2b8986..2ff1b4c9fc51b 100644 --- a/extmod/nimble/modbluetooth_nimble.c +++ b/extmod/nimble/modbluetooth_nimble.c @@ -661,7 +661,7 @@ STATIC int gap_scan_cb(struct ble_gap_event *event, void *arg) { return 0; } -int mp_bluetooth_gap_scan_start(int32_t duration_ms, int32_t interval_us, int32_t window_us) { +int mp_bluetooth_gap_scan_start(int32_t duration_ms, int32_t interval_us, int32_t window_us, int32_t active) { if (!mp_bluetooth_is_active()) { return ERRNO_BLUETOOTH_NOT_ACTIVE; } @@ -673,7 +673,7 @@ int mp_bluetooth_gap_scan_start(int32_t duration_ms, int32_t interval_us, int32_ .window = MAX(BLE_HCI_SCAN_WINDOW_MIN, MIN(BLE_HCI_SCAN_WINDOW_MAX, window_us / BLE_HCI_SCAN_ITVL)), .filter_policy = BLE_HCI_CONN_FILT_NO_WL, .limited = 0, - .passive = 1, // TODO: Handle BLE_HCI_ADV_RPT_EVTYPE_SCAN_RSP in gap_scan_cb above. + .passive = !active, .filter_duplicates = 0, }; int err = ble_gap_disc(BLE_OWN_ADDR_PUBLIC, duration_ms, &discover_params, gap_scan_cb, NULL);