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;
	}

  • 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

  • Hello Karl,

    Appreciate for your kindly help. It works for the single filter by name, as you suggested.

    Now I just have the additional issue regarding the two peripheral device connection.
    If I need to filter and connect two device by the name, e.g. FF_01 and FF_02, I gave my understanding as below.

    char device_name[2][5]={"FF_01","FF_02"};
    
    char *name = device_name;
    
    ....
    
    err = bt_scan_filter_add(BT_SCAN_FILTER_TYPE_NAME, name);

    Meanwhile I changed the CONFIG_BT_SCAN_NAME_CNT=2 in the prj.conf.

    The current result is it can filter and connect the first one (FF_01), when I only powered the FF_01.

    While it can't filter and connect the second one (FF_02), when I powered the FF_02 and powered off the FF_01.

    Can you please check which stage is wrong or missing?

    In addition, my final objective is to connect the two devices by the name filter at the same time, transmitting the data directly like the BLE earphone left and right.

    But I should move on step by step to figure out two filter and connection firstly.

    Thanks always for your help!

    Best Regards,

    Ethan

  • Hello again, Ethan

    EthanH said:
    Thanks always for your help!
    EthanH said:
    Appreciate for your kindly help.

    No problem at all, I am happy to help!

    EthanH said:
    It works for the single filter by name, as you suggested.

    Great! I am glad to hear that it now works for filtering of a single device name, we can then move on to adding the filter for the over device.

    EthanH said:
    Now I just have the additional issue regarding the two peripheral device connection.
    If I need to filter and connect two device by the name, e.g. FF_01 and FF_02, I gave my understanding as below.

    It seems that the bt_scan_filter_add function only reads the first c string of the provided buffer, so you can not provide both names in the same call. Please try to separate the two into two calls to bt_scan_filter_add and see if this makes both devices trigger a filter match.

    EthanH said:
    In addition, my final objective is to connect the two devices by the name filter at the same time, transmitting the data directly like the BLE earphone left and right.

    Are you intending to create an left and right earphone, or are you just using this as an example to illustrate your intentions?
    If you are, have you read anything about the coming LE Audio feature?
    We will soon make our LE Audio software support publicly available, so perhaps this could be interesting for you to have a look at if you are still in the process of evaluating different solutions.
    If you would like to hear more about LE Audio, or discuss the roadmap / future releases, I would have to ask that you reach out to your Regional Sales Manager (RSM) with these questions since we do not discuss future releases here on DevZone. If you do not know who your RSM is, please send me a direct message with your location, so I may provide you with their contact information.

    Best regards,
    Karl

  • Hello Karl,

    Thanks for your reply.

    Following your suggestion "Please try to separate the two into two calls to bt_scan_filter_add and see if this makes both devices trigger a filter match."

    I gave the code here:

    char device_name1[5]="FF_02";
    char device_name2[5]="FF_09";
    char *name1 = device_name1;
    char *name2 = device_name2;
    
    ...
    err = bt_scan_filter_add(BT_SCAN_FILTER_TYPE_NAME, name1);
    err = bt_scan_filter_add(BT_SCAN_FILTER_TYPE_NAME, name2);

    It is actually same as the pervious that  it only filter and connect the first one, while no wok for the second.

    And here is the log info:

    Can you please share your code based on this design?

    In addition, I just use the earphone to illustrate my intention, so my case is to BLE connect the two peripheral device and to receive the data from both. It help understand my project here in the overall design.

    Thanks always

    Best Regards,

    Ethan

  • EthanH said:
    I gave the code here:

    Please make sure to check the returned error codes from the bt_scan_filter_add functions as well - I assume you already do so, and that this snippet is just to illustrate, but I mention it just in case.
    Could you confirm that both these calls, and the call to bt_scan_filter_enable, returns successfully?

    EthanH said:
    And here is the log info:

    Could you show me the entire log for the sequence in which you successfully filter match on the first device name, and then unsuccessfully on the second?
    It looks to me like the issue in the log is that the device fails to send data over the BLE connection - even though there is no mention of a connection being established. Error 128 means that there is no valid connection.
    Could it be that you are attempting to call ble_nus_data_send or similar, without first having confirmed whether you are currently in a connection or not?

    EthanH said:
    In addition, I just use the earphone to illustrate my intention, so my case is to BLE connect the two peripheral device and to receive the data from both. It help understand my project here in the overall design.

    Aha, thank you for clarifying, this helps me understand your use case better. I just though I should mention the LE Audio feature since it will be such a huge improvement on audio streaming over BLE. 

    Best regards,
    Karl

Related