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 Reply Children
  • Hello Ethan,

    EthanH said:
    Thanks for your help.

    No problem at all, I am happy to help!

    EthanH said:
    I know it is a pointer to the data type.
    EthanH said:
    You can check the answer by Einar that he directly used the device name here "nRF Connect".

    I did not mean to imply that you did not, I thought it had to be passed the output of a macro or specific struct, like in the case of UUID's for instance, my mistake.
    It does indeed seem like it should just accept the name as a string when filtering on name.
    This should indeed work, and so then should your original code:

    #define DEVICE_NAME "nordic"
    
    ..
    	bt_scan_init(&scan_init);
    	bt_scan_cb_register(&scan_cb);
    
    	err = bt_scan_filter_add(BT_SCAN_FILTER_TYPE_NAME, DEVICE_NAME);
    	if (err){
    		printk("Filters 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;
        }
    ..


    Could you show me your entire project config? The error you are getting - Error 12 - means ENOMEM, so perhaps the issue here instead lays with the project configuration, even though it looks alright since you have already set CONFIG_BT_SCAN_NAME_CNT = 1.

    Best regards,
    Karl

  • Hello Karl,

    Thank you for the help and info firstly.

    Here is my prj.conf file:

    #
    # Copyright (c) 2018 Nordic Semiconductor
    #
    # SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
    #
    
    # Enable the UART driver
    # CONFIG_NRFX_UARTE0=y
    # CONFIG_SERIAL=y
    # CONFIG_CONSOLE=y
    # CONFIG_UART_CONSOLE=y
    
    CONFIG_USB=y
    CONFIG_USB_DEVICE_STACK=y
    CONFIG_USB_DEVICE_PRODUCT="FF"
    CONFIG_USB_UART_CONSOLE=y
    
    CONFIG_UART_INTERRUPT_DRIVEN=y
    CONFIG_UART_LINE_CTRL=y
    CONFIG_UART_CONSOLE_ON_DEV_NAME="CDC_ACM_0"
    
    # Enable the BLE stack with GATT Client configuration
    CONFIG_BT=y
    CONFIG_BT_CENTRAL=y
    CONFIG_BT_SMP=y
    CONFIG_BT_GATT_CLIENT=y
    
    # Enable the BLE modules from NCS
    CONFIG_BT_NUS_CLIENT=y
    CONFIG_BT_SCAN=y
    CONFIG_BT_SCAN_FILTER_ENABLE=y
    CONFIG_BT_SCAN_UUID_CNT=1
    CONFIG_BT_SCAN_NAME_CNT=1
    CONFIG_BT_GATT_DM=y
    CONFIG_HEAP_MEM_POOL_SIZE=2048
    
    # This example requires more workqueue stack
    CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE=2048
    
    # Enable bonding
    CONFIG_BT_SETTINGS=y
    CONFIG_FLASH=y
    CONFIG_FLASH_PAGE_LAYOUT=y
    CONFIG_FLASH_MAP=y
    CONFIG_NVS=y
    CONFIG_SETTINGS=y
    
    # Config logger
    CONFIG_LOG=y
    CONFIG_USE_SEGGER_RTT=y
    CONFIG_LOG_BACKEND_RTT=y
    CONFIG_LOG_BACKEND_UART=n
    CONFIG_BT_DEBUG_ATT=y
    
    CONFIG_ASSERT=y
    

    Please check it, I think the issue mainly is relevant to the configuration as you were saying.

    Best Regards

    Ethan

  • Hello Karl,

    Thanks for your help.

    I still have the question about the filter by two different names, can we achieve that?

    As writing in your code, if I give two names (nordic1 and nordic2) are both required to be scanned by name filter, what should I do for the command of bt_scan_filter_add?

    Here is my trial, while the program seems to be stuck and cannot continue。

    #define DEVICE_NAME1 "nordic1"
    #define DEVICE_NAME2 "nordic2"
    
    ...
        err = bt_scan_filter_add(BT_SCAN_FILTER_TYPE_NAME, DEVICE_NAME1);
    	if (err){
    		printk("Filter1 cannot be set (err %d)", err);
    		return err;
    	}
    	
        err = bt_scan_filter_add(BT_SCAN_FILTER_TYPE_NAME, DEVICE_NAME1);
            if (err){
    		printk("Filter2 cannot be set (err %d)", err);
    		return err;
    	}

    Thanks again and please share your suggestion here.

    Best Regards,

    Ethan

  • Hello again, Ethan

    I do not immediately see anything wrong with your prj.conf either, unfortunately - it looks alright to me.
    I went ahead and tried this on my end - starting with the BLE UART central example, and modifying it with the code lines from my previous comment, with a prj.conf similar to yours (with CONFIG_BT_SCAN_NAME_CNT=1), and it works immediately.
    Looking into the source code of the scanning library I see that it is the count for the name filter that is triggering the ENOMEM error. Could you try to increase the CONFIG_BT_SCAN_NAME_CNT = 2, and see if this error persists?
    I ask because I suspect that the name filter might have been set already somewhere else in your code.
    Could it be that you have already attempted to implement the 'filter for two different names' you mention in your later comment, and that this is thus exceeding the CONFIG_BT_SCAN_NAME_CNT=1 configuration?

    If this does not resolve your issue, could you share your entire main.c file with me, so I may take a look?

    EthanH said:

    I still have the question about the filter by two different names, can we achieve that?

    As writing in your code, if I give two names (nordic1 and nordic2) are both required to be scanned by name filter, what should I do for the command of bt_scan_filter_add?

    Do I understand you correctly that you would like to scan for two different names, and be alerted if either of them is found?
    If so, you will need to set CONFIG_BT_SCAN_NAME_CNT=2, and add and enable the two different name filters like I showed in my previous comment.
    If the scanning module is using the Normal filter mode it will trigger a filter match if any one of the enabled filters are met - unlike multifilter mode, which requires a combination of filters to be met.

    Best regards,
    Karl

Related