How do I filter and connect to BLE devices using the device name in Zephyr?
How do I filter and connect to BLE devices using the device name in Zephyr?
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 Heidi 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.
Thank you Heidi 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!
Yes, you should be able to access the device name in the advertising packet, if it is there.
I don't know what setup you are using, but if the advertising device is running with your application, you need to make sure that the device name and type are passed to bt_le_adv_start(), to make sure the device name is actually encoded into the advertising data.
See struct bt_data, BT_DATA(_type, _data, _data_len), and bt_le_adv_start().
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 (
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;
}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)