extmod/modbluetooth: Allow config of scan interval/window.
This adds two additional optional kwargs to `gap_scan()`: - `interval_us`: How long between scans. - `window_us`: How long to scan for during a scan. The default with NimBLE is a 11.25ms window with a 1.28s interval. Changing these parameters is important for detecting low-frequency advertisements (e.g. beacons). Note: these params are in microseconds, not milliseconds in order to allow the 625us granularity offered by the spec.
This commit is contained in:
parent
06ae818f93
commit
b65cc387cd
@ -501,23 +501,26 @@ STATIC mp_obj_t bluetooth_ble_gap_connect(size_t n_args, const mp_obj_t *args) {
|
|||||||
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(bluetooth_ble_gap_connect_obj, 3, 4, bluetooth_ble_gap_connect);
|
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(bluetooth_ble_gap_connect_obj, 3, 4, bluetooth_ble_gap_connect);
|
||||||
|
|
||||||
STATIC mp_obj_t bluetooth_ble_gap_scan(size_t n_args, const mp_obj_t *args) {
|
STATIC mp_obj_t bluetooth_ble_gap_scan(size_t n_args, const mp_obj_t *args) {
|
||||||
if (n_args == 2 && args[1] == mp_const_none) {
|
// Default is indefinite scan, with the NimBLE "background scan" interval and window.
|
||||||
int err = mp_bluetooth_gap_scan_stop();
|
mp_int_t duration_ms = 0;
|
||||||
return bluetooth_handle_errno(err);
|
mp_int_t interval_us = 1280000;
|
||||||
} else {
|
mp_int_t window_us = 11250;
|
||||||
mp_int_t duration_ms = 0;
|
if (n_args > 1) {
|
||||||
if (n_args == 2) {
|
if (args[1] == mp_const_none) {
|
||||||
if (!mp_obj_is_int(args[1])) {
|
// scan(None) --> stop scan.
|
||||||
mp_raise_ValueError("invalid duration");
|
return bluetooth_handle_errno(mp_bluetooth_gap_scan_stop());
|
||||||
}
|
}
|
||||||
duration_ms = mp_obj_get_int(args[1]);
|
duration_ms = mp_obj_get_int(args[1]);
|
||||||
|
if (n_args > 2) {
|
||||||
|
interval_us = mp_obj_get_int(args[2]);
|
||||||
|
if (n_args > 3) {
|
||||||
|
window_us = mp_obj_get_int(args[3]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int err = mp_bluetooth_gap_scan_start(duration_ms);
|
|
||||||
return bluetooth_handle_errno(err);
|
|
||||||
}
|
}
|
||||||
|
return bluetooth_handle_errno(mp_bluetooth_gap_scan_start(duration_ms, interval_us, window_us));
|
||||||
}
|
}
|
||||||
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(bluetooth_ble_gap_scan_obj, 1, 2, bluetooth_ble_gap_scan);
|
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(bluetooth_ble_gap_scan_obj, 1, 4, bluetooth_ble_gap_scan);
|
||||||
#endif // MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE
|
#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) {
|
STATIC mp_obj_t bluetooth_ble_gap_disconnect(mp_obj_t self_in, mp_obj_t conn_handle_in) {
|
||||||
|
@ -186,7 +186,7 @@ int mp_bluetooth_gap_disconnect(uint16_t conn_handle);
|
|||||||
|
|
||||||
#if MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE
|
#if MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE
|
||||||
// Start a discovery (scan). Set duration to zero to run continuously.
|
// Start a discovery (scan). Set duration to zero to run continuously.
|
||||||
int mp_bluetooth_gap_scan_start(int32_t duration_ms);
|
int mp_bluetooth_gap_scan_start(int32_t duration_ms, int32_t interval_us, int32_t window_us);
|
||||||
|
|
||||||
// Stop discovery (if currently active).
|
// Stop discovery (if currently active).
|
||||||
int mp_bluetooth_gap_scan_stop(void);
|
int mp_bluetooth_gap_scan_stop(void);
|
||||||
|
@ -614,16 +614,16 @@ STATIC int gap_scan_cb(struct ble_gap_event *event, void *arg) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int mp_bluetooth_gap_scan_start(int32_t duration_ms) {
|
int mp_bluetooth_gap_scan_start(int32_t duration_ms, int32_t interval_us, int32_t window_us) {
|
||||||
if (duration_ms == 0) {
|
if (duration_ms == 0) {
|
||||||
duration_ms = BLE_HS_FOREVER;
|
duration_ms = BLE_HS_FOREVER;
|
||||||
}
|
}
|
||||||
STATIC const struct ble_gap_disc_params discover_params = {
|
struct ble_gap_disc_params discover_params = {
|
||||||
.itvl = BLE_GAP_SCAN_SLOW_INTERVAL1,
|
.itvl = MAX(BLE_HCI_SCAN_ITVL_MIN, MIN(BLE_HCI_SCAN_ITVL_MAX, interval_us / BLE_HCI_SCAN_ITVL)),
|
||||||
.window = BLE_GAP_SCAN_SLOW_WINDOW1,
|
.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,
|
.filter_policy = BLE_HCI_CONN_FILT_NO_WL,
|
||||||
.limited = 0,
|
.limited = 0,
|
||||||
.passive = 0,
|
.passive = 1, // TODO: Handle BLE_HCI_ADV_RPT_EVTYPE_SCAN_RSP in gap_scan_cb above.
|
||||||
.filter_duplicates = 0,
|
.filter_duplicates = 0,
|
||||||
};
|
};
|
||||||
int err = ble_gap_disc(BLE_OWN_ADDR_PUBLIC, duration_ms, &discover_params, gap_scan_cb, NULL);
|
int err = ble_gap_disc(BLE_OWN_ADDR_PUBLIC, duration_ms, &discover_params, gap_scan_cb, NULL);
|
||||||
|
Loading…
Reference in New Issue
Block a user