I am attempting to use the scanning module to scan with a filter. Right now I am trying to implement a short name filter.
I am using the nrf Connect extension for VS code for the environment and I am using the <bluetooth/scan.h> include to import the nordic Bluetooth LE Scanning library for use.
I am using windows and using the Seeed Studio XIAO nRF52840 board for development.
The Issue Explained:
To my understanding so far to enable a filtered scan, at a high level I need to:
Add the filter to the scanning parameters through bt_scan_filter_add
The structure for the filter should be found from the source code
Enable the filters which would need you to specify the filter modes that you want to include bt_scan_filter_enable
Set the callback functions through bt_scan_cb_register. the scan_filter_match callback will define how the filter matches are delt with
Start the scanning with bt_scan_start
Given this highlevel understanding I have written the below code:
//--------------SCAN TESTING START---------------------//
// int err = 0;
const struct bt_scan_init_param bt_scan_init_opts = {
.scan_param = NULL, //default config
.connect_if_match = true,
.conn_param = NULL, //default config
};
bt_scan_init(&bt_scan_init_opts);
bt_scan_filter_remove_all();
bt_scan_filter_disable();
//double check if the definitin of the short name filter is correct
struct bt_scan_short_name ble_shrt_name;
ble_shrt_name.name = "shrtname";
ble_shrt_name.min_len = 8;
err = bt_scan_filter_add(BT_SCAN_FILTER_TYPE_SHORT_NAME, &ble_shrt_name);
if (err < 0) {
LOG_ERR("Error setting the short name filter (err: %d)n", err);
// return err;
}
uint8_t filter_modes = BT_SCAN_SHORT_NAME_FILTER | BT_SCAN_UUID_FILTER;
err = bt_scan_filter_enable(filter_modes, true); //Want all filters to be matched when looking for a new device
if (err < 0) {
LOG_ERR("Error establishing scan filters (err: %d)n", err);
// return err;
}
bt_scan_cb_register(scan_filter_match);
bt_scan_cb_register(scan_filter_no_match);
bt_scan_cb_register(scan_connecting);
bt_scan_cb_register(scan_connecting_error);
err = bt_scan_start(BT_SCAN_TYPE_SCAN_ACTIVE);
if (err < 0) {
LOG_ERR("Error starting the bt scan (err: %d)n", err);
}
//--------------SCAN TESTING END---------------------//
I am getting an ENOMEM (-12) Cannot allocate memory when I try to add the short name filter to the scan system.
to my understanding this is due to calling for more memory than what is available to the mcu. However, I have increase the memory available to the mcu through the config file by setting the following parameter:
CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE=4096
CONFIG_MAIN_STACK_SIZE=4096
CONFIG_HEAP_MEM_POOL_SIZE=8192
CONFIG_LOG_BUFFER_SIZE=4096
Going through some other posts on a similar problem here: how to bt_scan_filter_add by short name ?
in the end it is stated: “Edit: This still requires you to set the number of Short Name Filters in the guiconfig
or menuconfig
via BT_SCAN_SHORT_NAME_CNT
.”
I think the guiconfig and menuconfig are the same as the proj.config file used in the NRF Connect extension for vs code as well. However when I se this config flag I get an error that
ignoring malformed line 'BT_SCAN_SHORT_NAME_CNT=1'
And through googling the zephyr configs it seems like this flag is not available.
Can anyone guide me as to what the error could be?
Any guidance is much appreciated.
Thanks All.