Skip to content

Commit

Permalink
samples: bluetooth: Move device name from ADV data to SCAN RSP data
Browse files Browse the repository at this point in the history
Move Bluetooth device name from Advertising Data to Scan Response Data
to prevent truncation when there is insufficient space in
Advertising Data.

Ref: NCSDK-28545

Signed-off-by: Marcin Jelinski <marcin.jelinski@nordicsemi.no>
  • Loading branch information
maje-emb authored and rlubos committed Sep 3, 2024
1 parent 3ed6390 commit 944e4d4
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 25 deletions.
1 change: 0 additions & 1 deletion samples/bluetooth/multiple_adv_sets/prj.conf
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ CONFIG_BT=y
CONFIG_BT_PERIPHERAL=y
CONFIG_BT_DEVICE_NAME="Nordic multi adv sets"
CONFIG_BT_DEVICE_APPEARANCE=512
CONFIG_BT_DEVICE_NAME_DYNAMIC=y

CONFIG_BT_EXT_ADV=y
CONFIG_BT_EXT_ADV_MAX_ADV_SET=2
Expand Down
34 changes: 14 additions & 20 deletions samples/bluetooth/multiple_adv_sets/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ static K_WORK_DEFINE(advertising_work, advertising_work_handle);

static struct bt_le_ext_adv *ext_adv[CONFIG_BT_EXT_ADV_MAX_ADV_SET];
static const struct bt_le_adv_param *non_connectable_adv_param =
BT_LE_ADV_PARAM(BT_LE_ADV_OPT_NONE,
BT_LE_ADV_PARAM(BT_LE_ADV_OPT_SCANNABLE,
0x140, /* 200 ms */
0x190, /* 250 ms */
NULL);
Expand All @@ -42,14 +42,18 @@ static const struct bt_le_adv_param *connectable_adv_param =
BT_GAP_ADV_FAST_INT_MAX_2, /* 150 ms */
NULL);

static const struct bt_data non_connectable_data[] = {
static const struct bt_data non_connectable_ad_data[] = {
BT_DATA_BYTES(BT_DATA_FLAGS, BT_LE_AD_NO_BREDR),
BT_DATA_BYTES(BT_DATA_URI, /* The URI of the https://www.nordicsemi.com website */
0x17, /* UTF-8 code point for “https:” */
'/', '/', 'w', 'w', 'w', '.',
'n', 'o', 'r', 'd', 'i', 'c', 's', 'e', 'm', 'i', '.',
'c', 'o', 'm'),
BT_DATA(BT_DATA_NAME_COMPLETE, CONFIG_BT_DEVICE_NAME, sizeof(CONFIG_BT_DEVICE_NAME) - 1),
};

static const struct bt_data non_connectable_sd_data[] = {
BT_DATA(BT_DATA_NAME_COMPLETE, NON_CONNECTABLE_DEVICE_NAME,
sizeof(NON_CONNECTABLE_DEVICE_NAME) - 1),
};

static const struct bt_data connectable_data[] = {
Expand Down Expand Up @@ -124,7 +128,8 @@ BT_CONN_CB_DEFINE(conn_callbacks) = {

static int advertising_set_create(struct bt_le_ext_adv **adv,
const struct bt_le_adv_param *param,
const struct bt_data *ad, size_t ad_len)
const struct bt_data *ad, size_t ad_len,
const struct bt_data *sd, size_t sd_len)
{
int err;
struct bt_le_ext_adv *adv_set;
Expand All @@ -139,8 +144,7 @@ static int advertising_set_create(struct bt_le_ext_adv **adv,

printk("Created adv: %p\n", adv_set);

err = bt_le_ext_adv_set_data(adv_set, ad, ad_len,
NULL, 0);
err = bt_le_ext_adv_set_data(adv_set, ad, ad_len, sd, sd_len);
if (err) {
printk("Failed to set advertising data (err %d)\n", err);
return err;
Expand All @@ -153,14 +157,9 @@ static int non_connectable_adv_create(void)
{
int err;

err = bt_set_name(NON_CONNECTABLE_DEVICE_NAME);
if (err) {
printk("Failed to set device name (err %d)\n", err);
return err;
}

err = advertising_set_create(&ext_adv[NON_CONNECTABLE_ADV_IDX], non_connectable_adv_param,
non_connectable_data, ARRAY_SIZE(non_connectable_data));
non_connectable_ad_data, ARRAY_SIZE(non_connectable_ad_data),
non_connectable_sd_data, ARRAY_SIZE(non_connectable_sd_data));
if (err) {
printk("Failed to create a non-connectable advertising set (err %d)\n", err);
}
Expand All @@ -172,14 +171,9 @@ static int connectable_adv_create(void)
{
int err;

err = bt_set_name(CONFIG_BT_DEVICE_NAME);
if (err) {
printk("Failed to set device name (err %d)\n", err);
return err;
}

err = advertising_set_create(&ext_adv[CONNECTABLE_ADV_IDX], connectable_adv_param,
connectable_data, ARRAY_SIZE(connectable_data));
connectable_data, ARRAY_SIZE(connectable_data),
NULL, 0);
if (err) {
printk("Failed to create a connectable advertising set (err %d)\n", err);
}
Expand Down
6 changes: 4 additions & 2 deletions samples/bluetooth/peripheral_cgms/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ static const struct bt_data ad[] = {
BT_DATA_BYTES(BT_DATA_UUID16_ALL,
BT_UUID_16_ENCODE(BT_UUID_CGMS_VAL),
BT_UUID_16_ENCODE(BT_UUID_DIS_VAL)),
};

static const struct bt_data sd[] = {
BT_DATA(BT_DATA_NAME_COMPLETE, CONFIG_BT_DEVICE_NAME, sizeof(CONFIG_BT_DEVICE_NAME) - 1),
};

Expand Down Expand Up @@ -148,8 +151,7 @@ int main(void)
printk("Error occurred when initializing cgm service (err %d)\n", err);
return 0;
}

err = bt_le_adv_start(BT_LE_ADV_CONN, ad, ARRAY_SIZE(ad), NULL, 0);
err = bt_le_adv_start(BT_LE_ADV_CONN, ad, ARRAY_SIZE(ad), sd, ARRAY_SIZE(sd));
if (err) {
printk("Advertising failed to start (err %d)\n", err);
return 0;
Expand Down
9 changes: 7 additions & 2 deletions samples/bluetooth/peripheral_power_profiling/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ static const struct bt_data non_connectable_ad_data[] = {
'/', '/', 'w', 'w', 'w', '.',
'n', 'o', 'r', 'd', 'i', 'c', 's', 'e', 'm', 'i', '.',
'c', 'o', 'm'),
};

static const struct bt_data non_connectable_sd_data[] = {
BT_DATA(BT_DATA_NAME_COMPLETE, CONFIG_BT_DEVICE_NAME, sizeof(CONFIG_BT_DEVICE_NAME) - 1),
};

Expand All @@ -100,7 +103,7 @@ static const struct bt_le_adv_param *connectable_ad_params =
NULL);

static const struct bt_le_adv_param *non_connectable_ad_params =
BT_LE_ADV_PARAM(BT_LE_ADV_OPT_NONE,
BT_LE_ADV_PARAM(BT_LE_ADV_OPT_SCANNABLE,
NON_CONNECTABLE_ADV_INTERVAL_MIN,
NON_CONNECTABLE_ADV_INTERVAL_MAX,
NULL);
Expand Down Expand Up @@ -399,7 +402,9 @@ static void button_handler(uint32_t button_state, uint32_t has_changed)
}

err = bt_le_ext_adv_set_data(adv_set, non_connectable_ad_data,
ARRAY_SIZE(non_connectable_ad_data), NULL, 0);
ARRAY_SIZE(non_connectable_ad_data),
non_connectable_sd_data,
ARRAY_SIZE(non_connectable_sd_data));
if (err) {
printk("Failed to set data for non-connectable advertising (err %d)\n",
err);
Expand Down

0 comments on commit 944e4d4

Please sign in to comment.