This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

BLE scan filter by device name

Hello there,

It is actually basic for the Bluetooth connection for the nrf52840 using Zephyr, which changed the scan filter by name based on the central UART sample.

As the peripheral device name is assigned "FF_22", and I used the nRF Connect app to connect it.

 

Also edit the prj.conf file below:

CONFIG_BT_SCAN=y
CONFIG_BT_SCAN_FILTER_ENABLE=y
CONFIG_BT_SCAN_UUID_CNT=1
CONFIG_BT_SCAN_NAME_CNT=1

However, it gave the error:

Can you please tell me where I got missing here to configure name scanning filter?

Many thanks in advance!

Best Regards,

Ethan

    err = bt_scan_filter_add(BT_SCAN_FILTER_TYPE_NAME, "FF_22");
	if (err) {
		LOG_ERR("Scanning filters cannot be set (err %d)", err);
		return err;
	}

	err = bt_scan_filter_enable(BT_SCAN_NAME_FILTER, false);
	if (err) {
		LOG_ERR("Filters cannot be turned on (err %d)", err);
		return err;
	}

Parents
  • Hello Karl,

    Thank you for info.

    Firstly, I gave the error coding to return the results from bt_scan_filter_add(), but no work for both.

    	err = bt_scan_filter_add(BT_SCAN_FILTER_TYPE_NAME, name1);
            if (err){
    		printk("Filter1 cannot be set (err %d)", err);
    		return err;
    	}
    	
        err = bt_scan_filter_add(BT_SCAN_FILTER_TYPE_NAME, name2);
    	if (err){
    		printk("Filter2 cannot be set (err %d)", err);
    		return err;
    	}
    	
    	err = bt_scan_filter_enable(BT_SCAN_NAME_FILTER, false);
        if (err){
            printk("Filters cannot be turned on (err %d)", err);
            return err;
        }

    Log info:

    Secondly, I removed the error return like this:

    err = bt_scan_filter_add(BT_SCAN_FILTER_TYPE_NAME, name1);
    
    err = bt_scan_filter_add(BT_SCAN_FILTER_TYPE_NAME, name2);
    
    err = bt_scan_filter_enable(BT_SCAN_NAME_FILTER, false);
    if (err){
        printk("Filters cannot be turned on (err %d)", err);
        return err;
    }

    Log info for the first success connection:

    While the second one was not connected, log info:

    From my understanding, it is probably relevant to the filter by name addition function as the system can recognize the first one which means the BLE connection configure is OK.

    And the system still consider the first name as the filter so that I think the way for adding the name filter is wrong for me.

    Please check it and share your idea.

    Best Regards

    Ethan

Reply
  • Hello Karl,

    Thank you for info.

    Firstly, I gave the error coding to return the results from bt_scan_filter_add(), but no work for both.

    	err = bt_scan_filter_add(BT_SCAN_FILTER_TYPE_NAME, name1);
            if (err){
    		printk("Filter1 cannot be set (err %d)", err);
    		return err;
    	}
    	
        err = bt_scan_filter_add(BT_SCAN_FILTER_TYPE_NAME, name2);
    	if (err){
    		printk("Filter2 cannot be set (err %d)", err);
    		return err;
    	}
    	
    	err = bt_scan_filter_enable(BT_SCAN_NAME_FILTER, false);
        if (err){
            printk("Filters cannot be turned on (err %d)", err);
            return err;
        }

    Log info:

    Secondly, I removed the error return like this:

    err = bt_scan_filter_add(BT_SCAN_FILTER_TYPE_NAME, name1);
    
    err = bt_scan_filter_add(BT_SCAN_FILTER_TYPE_NAME, name2);
    
    err = bt_scan_filter_enable(BT_SCAN_NAME_FILTER, false);
    if (err){
        printk("Filters cannot be turned on (err %d)", err);
        return err;
    }

    Log info for the first success connection:

    While the second one was not connected, log info:

    From my understanding, it is probably relevant to the filter by name addition function as the system can recognize the first one which means the BLE connection configure is OK.

    And the system still consider the first name as the filter so that I think the way for adding the name filter is wrong for me.

    Please check it and share your idea.

    Best Regards

    Ethan

Children
  • Hello again, Ethan

    Thank you for your patience with this.

    EthanH said:
    Secondly, I removed the error return like this:

    I can not recommend that you remove the checks for returned error codes - if you do not check your error codes you will have no way of knowing if a function has unexpectedly failed, or if the program may proceed as usual.
    This is only masking the symptoms of the failure, instead of fixing the root cause.

    I tried scanning for two different device names using the following scan init and CONFIG_BT_SCAN_NAME_CNT=2

    static int scan_init(void)
    {
    	int err;
    	struct bt_scan_init_param scan_init = {
    		.connect_if_match = 1,
    	};
    
    	bt_scan_init(&scan_init);
    	bt_scan_cb_register(&scan_cb);
    
    	err = bt_scan_filter_add(BT_SCAN_FILTER_TYPE_NAME, DEVICE_NAME_FIRST);
    	if (err) {
    		LOG_ERR("Scanning filters cannot be set (err %d)", err);
    		return err;
    	}
    	
    	err = bt_scan_filter_add(BT_SCAN_FILTER_TYPE_NAME, DEVICE_NAME_SECOND);
    	if (err) {
    		LOG_ERR("Scanning filters cannot be set (err %d)", err);
    		return err;
    	}
    
    	err = bt_scan_filter_enable(BT_SCAN_NAME_FILTER, false);
    	if (err) {
    		LOG_ERR("Filters cannot be turned on (err %d)", err);
    		return err;
    	}
    
    	LOG_INF("Scan module initialized");
    	return err;
    }


    And it worked just as expected for me - both DEVICE_NAME_FIRST and DEVICE_NAME_SECOND triggers a scan filter match.
    This is the only change I've made to the central uart sample. To rule out the scan initialization and usage being an issue could you try to make these changes to an unmodified version of the central uart application, and see if you get a filter match on both the different device names?

    I do not see the Scan module initialized - started messages in the initial log you share. This makes me suspect that the application never gets as far as scanning starting.
    Could you try to debug the application and see if it gets stuck somewhere prior to this? I would expect the same scan messages to appear in the log.
    If not, there might be a problem earlier in your application, such as with the usb_cdc_acm which is generating warnings and suspending the device.
    If you are able to properly deploy both name filters on the unmodified central uart example we should look at resolving the usb_cdc_acm warnings first of all, before we proceed with the perceived scanning issue.

    Best regards,
    Karl

Related