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

Filtering BLE devices by device name

How do I filter and connect to BLE devices using the device name in Zephyr?

Parents
  • Hi!

    Yes, if you use the Scanning Module in advanced mode, you can use a filter type that filters the set to a target name.

    You can add a new filter with this filter type like this:

    bt_scan_filter_add ( BT_SCAN_FILTER_TYPE_NAME, data),

    where data is a pointer to the filter data to add, (i.e target names to filter by). 

    Let me know if you have any more questions!

    Best regards,

    Heidi

  • Thank you for your reply.  I will take a look at it.  For now, I am filtering by UUID by accessing the advertisement data.  I think it should be possible to access the device name as well through the advertisement data.  The issue is that the type and data do not show up for the device name.  In my case, only type 0x01 and type 0x07 shows up but not type 0x09.  My eye caught some privacy/security settings, I guess it might have to do with that.

  • Hi!

    Below is my advertising data:

    static const struct bt_data ad[] = {
    	BT_DATA_BYTES(BT_DATA_FLAGS, (BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR)),
    	BT_DATA_BYTES(BT_DATA_UUID128_ALL,
    		0xef, 0x41, 0x16, 0xdf, 0x0a, 0x11, 0x4b, 0x49,
    		0xae, 0x5a, 0x65, 0x92, 0xd5, 0xa2, 0x6d, 0xa5),
    };

    From what I understand from your last reply, I have to add the device name (

    BT_DATA_NAME_COMPLETE) in this advertising struct.
    static bool eir_found(struct bt_data *data, void *user_data)
    {
    	bt_addr_le_t *addr = user_data;
    	struct bt_uuid *uuid;
    	uint16_t u16;
    	int err;
    
    	printk("[AD]: %u data_len %u\n", data->type, data->data_len);
    	switch (data->type) {
    	case BT_DATA_UUID128_ALL:
    		if (data->data_len % sizeof(uint16_t) != 0U) {
    			printk("AD malformed\n");
    			return true;
    		}
    I use this EIR function to print the type and length of each EIR packet.  The weird thing is the device name shows up in the nRFConnect app but not in the application.
  • JPSvanderWalt said:

    From what I understand from your last reply, I have to add the device name (

    BT_DATA_NAME_COMPLETE) in this advertising struct.

     Yes, exactly.

     

    JPSvanderWalt said:
    The weird thing is the device name shows up in the nRFConnect app but not in the application.

     What do you mean by "shows up in the nRF Connect app"?

  • I added the device name to the advertising data as bellow:

    #define DEVICE_NAME			CONFIG_BT_DEVICE_NAME
    #define DEVICE_NAME_LEN		(sizeof(DEVICE_NAME) - 1)
    
    struct bt_conn *default_conn;
    
    static const struct bt_data ad[] = {
    	BT_DATA_BYTES(BT_DATA_FLAGS, (BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR)),
    	BT_DATA_BYTES(BT_DATA_NAME_COMPLETE, DEVICE_NAME),
    	BT_DATA_BYTES(BT_DATA_UUID128_ALL,
    		0xef, 0x41, 0x16, 0xdf, 0x0a, 0x11, 0x4b, 0x49,
    		0xae, 0x5a, 0x65, 0x92, 0xd5, 0xa2, 0x6d, 0xa5),
    };

    I then get the following error in the serial terminal

    Advertising failed to start (err -22)

    Where can I look up the error code?

  • Hi!

    -22 means invalid argument. You can look up the error codes in libc minimal errno.h or newlib errno.h depending on if your application is built with newlib library or not (CONFIG_NEWLIB_LIBC is enabled). 

    You have to add it using the BT_DATA macro like this:

    BT_DATA(BT_DATA_NAME_COMPLETE, DEVICE_NAME, DEVICE_NAME_LENGTH)

  • Hi!

    I have tried the suggested BT_DATA macro and I am still getting the same error.  I even tried to add it to the scan response data with no success.

    static const struct bt_data ad[] = {
    	BT_DATA_BYTES(BT_DATA_FLAGS, (BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR)),
    	//BT_DATA(BT_DATA_NAME_COMPLETE, DEVICE_NAME,DEVICE_NAME_LEN),
    	BT_DATA_BYTES(BT_DATA_UUID128_ALL,
    		0xef, 0x41, 0x16, 0xdf, 0x0a, 0x11, 0x4b, 0x49,
    		0xae, 0x5a, 0x65, 0x92, 0xd5, 0xa2, 0x6d, 0xa5),
    };
    
    static const struct bt_data sd[] = {
    	BT_DATA(BT_DATA_NAME_COMPLETE, DEVICE_NAME, DEVICE_NAME_LEN),
    
    };
    
    err = bt_le_adv_start(BT_LE_ADV_CONN_NAME, ad, ARRAY_SIZE(ad), sd, ARRAY_SIZE(sd));

    I am not sure if there is something specific I need to add to the config file to enable this?

Reply
  • Hi!

    I have tried the suggested BT_DATA macro and I am still getting the same error.  I even tried to add it to the scan response data with no success.

    static const struct bt_data ad[] = {
    	BT_DATA_BYTES(BT_DATA_FLAGS, (BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR)),
    	//BT_DATA(BT_DATA_NAME_COMPLETE, DEVICE_NAME,DEVICE_NAME_LEN),
    	BT_DATA_BYTES(BT_DATA_UUID128_ALL,
    		0xef, 0x41, 0x16, 0xdf, 0x0a, 0x11, 0x4b, 0x49,
    		0xae, 0x5a, 0x65, 0x92, 0xd5, 0xa2, 0x6d, 0xa5),
    };
    
    static const struct bt_data sd[] = {
    	BT_DATA(BT_DATA_NAME_COMPLETE, DEVICE_NAME, DEVICE_NAME_LEN),
    
    };
    
    err = bt_le_adv_start(BT_LE_ADV_CONN_NAME, ad, ARRAY_SIZE(ad), sd, ARRAY_SIZE(sd));

    I am not sure if there is something specific I need to add to the config file to enable this?

Children
Related