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

how to bt_scan_filter_add by short name ?

hi all

how to bt scan filter by short name ?

i have been set "#define CONFIG_BT_SCAN_SHORT_NAME_CNT 1"

it get bus fault error:

"Bluetooth ready
***** BUS FAULT *****
  Precise data bus error
  BFAR Address: 0x72616d53
***** Hardware exception *****"

bt_scan_filter_add

const char bleDeviceName[]="myBLEdevice";

err = bt_scan_filter_add(BT_SCAN_FILTER_TYPE_SHORT_NAME, bleDeviceName);
    if (err) {
                printk("error = %d\n",err);
        printk("Scanning filters cannot be set\n");
        return;
    }

autoconfig.h

#define CONFIG_BT_GATT_DM 1
#define CONFIG_BT_GATT_DM_MAX_ATTRS 35
#define CONFIG_BT_GATT_DM_MAX_MEM_CHUNKS 6
#define CONFIG_BT_SCAN 1
#define CONFIG_BT_SCAN_NAME_MAX_LEN 32
#define CONFIG_BT_SCAN_SHORT_NAME_MAX_LEN 32
#define CONFIG_BT_SCAN_FILTER_ENABLE 1
#define CONFIG_BT_SCAN_MANUFACTURER_DATA_MAX_LEN 32
#define CONFIG_BT_SCAN_UUID_CNT 0
#define CONFIG_BT_SCAN_NAME_CNT 0
#define CONFIG_BT_SCAN_SHORT_NAME_CNT 1
#define CONFIG_BT_SCAN_ADDRESS_CNT 0
#define CONFIG_BT_SCAN_APPEARANCE_CNT 0
#define CONFIG_BT_SCAN_MANUFACTURER_DATA_CNT 0

  • Hoping we can get some help on this.

    The "documentation" on this feature (such as it is) can be found here. However, it doesn't describe how to pass a short name as a filter.

    Worse, the filter doesn't appear to work at all. Not really surprised - A LOT of their code doesn't work as documented (when it's even documented at all).

    If you dig into the actual header file, you'll note that `bt_scan_filter_add()` takes a void pointer for the value to filter on, which is cast on execution.

    int bt_scan_filter_add(enum bt_scan_filter_type type,
    		       const void *data)
    {
    	char *name;
    	struct bt_scan_short_name *short_name;
    	bt_addr_le_t *addr;
    	struct bt_uuid *uuid;
    	uint16_t appearance;
    	struct bt_scan_manufacturer_data *manufacturer_data;
    	int err = 0;
    
    	if (!data) {
    		return -EINVAL;
    	}
    
    	k_mutex_lock(&scan_mutex, K_FOREVER);
    
    	switch (type) {
    	case BT_SCAN_FILTER_TYPE_NAME:
    		name = (char *)data;
    		err = scan_name_filter_add(name);
    		break;
    
    	case BT_SCAN_FILTER_TYPE_SHORT_NAME:
    		short_name = (struct bt_scan_short_name *)data;
    		err = scan_short_name_filter_add(short_name);
    		break;
    
    	case BT_SCAN_FILTER_TYPE_ADDR:
    		addr = (bt_addr_le_t *)data;
    		err = scan_addr_filter_add(addr);
    		break;
    
    	case BT_SCAN_FILTER_TYPE_UUID:
    		uuid = (struct bt_uuid *)data;
    		err = scan_uuid_filter_add(uuid);
    		break;
    
    	case BT_SCAN_FILTER_TYPE_APPEARANCE:
    		appearance = *((uint16_t *)data);
    		err = scan_appearance_filter_add(appearance);
    		break;
    
    	case BT_SCAN_FILTER_TYPE_MANUFACTURER_DATA:
    		manufacturer_data = (struct bt_scan_manufacturer_data *)data;
    		err = scan_manufacturer_data_filter_add(manufacturer_data);
    		break;
    
    	default:
    		err = -EINVAL;
    		break;
    	}
    
    	k_mutex_unlock(&scan_mutex);
    
    	return err;
    }

    The important line is `case BT_SCAN_FILTER_TYPE_SHORT_NAME:`

    You'll see that `short_name` is cast to `struct bt_scan_short_name`. That struct is as follows:

    /**@brief A helper structure to set filters for the name.
     */
    struct bt_scan_short_name {
    	/** Pointer to the short name. */
    	const char *name;
    
    	/** Minimum length of the short name. */
    	uint8_t min_len;
    };



    Putting that all together, you *should* be able to set a short name filter with _something_ like:

        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) {
    		printk("\nScanning filters cannot be set (err %d)\n", err);
    		return err;
    	}
    
    	err = bt_scan_filter_enable(BT_SCAN_SHORT_NAME_FILTER, false);
    	if (err) {
    		printk("\nFilters cannot be turned on (err %d)\n", err);
    		return err;
    	}

    Edit: This still requires you to set the number of Short Name Filters in the `guiconfig` or `menuconfig` via `BT_SCAN_SHORT_NAME_CNT`.

    Very sad that Nordic hasn't really documented this very well, and doesn't appear to be listening to people asking for guidance.

    I wonder if they'll step up now.

Related