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

Longer BLE Device name

I'm using NCS v1.5.0 to create a BLE peripheral using nRF5340-DK.

I set the device name in the prj.conf file using CONFIG_BT_DEVICE_NAME and the name shows up when BLE advertising starts (using LightBlue to monitor).

But the device name seems to be truncated (to about 11 characters).   

To try to get the full name (about 21 characters), I first tried to set CONFIG_BT_DEVICE_NAME to 32 (larger than the actual name), but that didn't have any effect.

I found an NCS BLE tutorial in DevZone (written by Haakonsh on 11 Feb 2020) that suggested the following:

#define DEVICE_NAME      CONFIG_BT_DEVICE_NAME        // Set in prj.conf
#define DEVICE_NAME_LEN  (sizeof(DEVICE_NAME) - 1)

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

then using this when calling bt_le_adv_start:

        err = bt_le_adv_start(BT_LE_ADV_PARAM(
                                   BT_LE_ADV_OPT_CONNECTABLE
                                 | BT_LE_ADV_OPT_ONE_TIME
                                 | BT_LE_ADV_OPT_USE_NAME,
                                 160,
                                 1600,
                                 NULL),
                              ad, ARRAY_SIZE(ad),
                              sd, ARRAY_SIZE(sd));

This call returns -22  (Illegal argument).   I traced the origin of the error to this call in hci_core.c (around line 7880):

static int le_adv_update(struct bt_le_ext_adv *adv,
			 const struct bt_data *ad, size_t ad_len,
			 const struct bt_data *sd, size_t sd_len,
			 bool ext_adv, bool scannable, bool use_name)
{
	struct bt_ad d[2] = {};
	struct bt_data data;
	size_t d_len;
	int err;

	if (use_name) {
		const char *name = bt_get_name();

		if ((ad && ad_has_name(ad, ad_len)) ||
		    (sd && ad_has_name(sd, sd_len))) {
			/* Cannot use name if name is already set */
			return -EINVAL;
		}

		data = (struct bt_data)BT_DATA(
			BT_DATA_NAME_COMPLETE,
			name, strlen(name));
	}

The call to "ad_has_name" checks if the name is specified in the arguments:

static inline bool ad_has_name(const struct bt_data *ad, size_t ad_len)
{
	size_t i;

	for (i = 0; i < ad_len; i++) {
		if (ad[i].type == BT_DATA_NAME_COMPLETE ||
		    ad[i].type == BT_DATA_NAME_SHORTENED) {
			return true;
		}
	}

	return false;
}

Since the name is specified it returns true.

The "return -EINVAL" occurs since the name was defined in the ad[] array (using BT_DATA_NAME_COMPLETE).

The comment "Cannot use name if name is already set" seems to indicate that the name has been declared/defined somewhere else, but I don't see where...

Is this check really necessary?     

I know BLE advertisement names can be longer (I have used longer ones on other systems).   Did I miss something?

Thanks!!

Parents Reply Children
No Data
Related