Skip to content

samples: bluetooth: add Device Name sample#745

Open
PizzaAllTheWay wants to merge 1 commit into
nrfconnect:mainfrom
PizzaAllTheWay:device-name-sample
Open

samples: bluetooth: add Device Name sample#745
PizzaAllTheWay wants to merge 1 commit into
nrfconnect:mainfrom
PizzaAllTheWay:device-name-sample

Conversation

@PizzaAllTheWay
Copy link
Copy Markdown
Contributor

Add a new BLE sample demonstrating how a peer can read and write the GAP Device Name characteristic (UUID 0x2A00) over BLE.

The sample:

  • Advertises with a configurable default device name.
  • Allows connected peers to write a new name with open permissions.
  • Updates the SoftDevice GAP name and advertising data immediately.
  • Persists the new name to flash (BM ZMS) so it survives reboots.
  • Loads the saved name from storage on boot, falling back to the Kconfig default if none exists.

Includes sample documentation and changelog entry.

@PizzaAllTheWay PizzaAllTheWay self-assigned this Apr 14, 2026
@PizzaAllTheWay PizzaAllTheWay requested review from a team as code owners April 14, 2026 07:59
@github-actions github-actions Bot added the doc-required PR must not be merged without tech writer approval. label Apr 14, 2026
@PizzaAllTheWay PizzaAllTheWay force-pushed the device-name-sample branch 3 times, most recently from 4ef7306 to 56936e4 Compare April 14, 2026 08:34
@PizzaAllTheWay PizzaAllTheWay changed the title samples: bluetooth: add GAP Device Name sample samples: bluetooth: add Device Name sample Apr 14, 2026
@github-actions
Copy link
Copy Markdown

You can find the documentation preview for this PR here.

@PizzaAllTheWay PizzaAllTheWay force-pushed the device-name-sample branch 2 times, most recently from 243b4e7 to a8dce41 Compare April 14, 2026 08:50
Comment on lines +24 to +25
#define STORAGE_OFFSET DT_REG_ADDR(STORAGE_NODE)
#define STORAGE_SIZE DT_REG_SIZE(STORAGE_NODE)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use partition macros for this

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nordicjm Please give a suggestion here. This is the pattern used in other samples.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DT_FIXED_PARTITION_ADDR() for address with #include <zephyr/devicetree/fixed-partitions.h>

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried DT_FIXED_PARTITION_ADDR() but it doesn't work for storage0_partition in this sample due to the DT layout:

rram@0
└── partitions
    └── storage_partition (compatible = "fixed-subpartitions", reg = <0x61800 0x2000>)
        ├── peer_manager_partition (reg = <0x0     0x1000>)
        └── storage0_partition     (reg = <0x1000  0x1000>)

DT_FIXED_PARTITION_ADDR() is defined as DT_REG_ADDR(node) + DT_REG_ADDR(DT_GPARENT(node)), which assumes the standard flash → partitions → partition layout. With the extra fixed-subpartitions level in this project, the grandparent of storage0_partition is the partitions node, which has no reg property for the macro to read. This causes a compile error:

'DT_N_..._partitions_REG_IDX_0_VAL_ADDRESSU' undeclared

Pointing at storage_partition instead does compile, but then we'd be writing across the entire 0x2000 region and overlap with peer_manager_partition, which defeats the purpose of the sub-partition split.

I switched to the flash_map helpers, which handle this correctly:

#include <zephyr/storage/flash_map.h>

#define STORAGE_OFFSET  FIXED_PARTITION_OFFSET(storage0_partition)
#define STORAGE_SIZE    FIXED_PARTITION_SIZE(storage0_partition)

That said, given the constraints here, maybe the original DT_REG_ADDR/DT_REG_SIZE was the cleaner solution after all.

@nordicjm what do you think?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

update to mapped partitions then this becomes a non-issue

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oki, I will open a separate PR for the partition layout change since it's out of scope here.

Copy link
Copy Markdown
Contributor

@eivindj-nordic eivindj-nordic May 19, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nordicjm We are using

#define STORAGE_OFFSET         DT_REG_ADDR(STORAGE_NODE)
#define STORAGE_SIZE           DT_REG_SIZE(STORAGE_NODE)

in other samples in BM main, that is pointing to a NCS revision before mapped-partitions are introduced. We would like to get this in with the same pattern now and update the partition layout and usage repo-wide in a separate PR once the manifest is updated.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reverted back to using DT_REG_ADDR and DT_REG_SIZE for now until Device Tree is restructured properly

Copy link
Copy Markdown
Contributor

@b-gent b-gent left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

small comments wrt doc

:local:
:depth: 2

The Device Name sample demonstrates how to read and write the BLE GAP Device Name characteristic using |BMlong|.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
The Device Name sample demonstrates how to read and write the BLE GAP Device Name characteristic using |BMlong|.
The Device Name sample demonstrates how to read and write the Bluetooth LE GAP Device Name characteristic using |BMlong|.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

:depth: 2

The Device Name sample demonstrates how to read and write the BLE GAP Device Name characteristic using |BMlong|.
A connected peer can change the device name over BLE. The new name takes effect immediately, persists across disconnects, and survives power cycles.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
A connected peer can change the device name over BLE. The new name takes effect immediately, persists across disconnects, and survives power cycles.
A connected peer can change the device name over Bluetooth LE.
The new name takes effect immediately, persists across disconnects, and survives power cycles.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

Comment thread samples/bluetooth/ble_device_name/Kconfig Outdated
Comment thread samples/bluetooth/ble_device_name/Kconfig Outdated
Comment thread samples/bluetooth/ble_device_name/Kconfig Outdated
Comment thread samples/bluetooth/ble_device_name/README.rst Outdated
Comment thread samples/bluetooth/ble_device_name/README.rst Outdated
Comment thread samples/bluetooth/ble_device_name/src/main.c Outdated
Comment thread samples/bluetooth/ble_device_name/src/main.c Outdated
Comment thread samples/bluetooth/ble_device_name/src/main.c Outdated
Comment thread samples/bluetooth/ble_device_name/src/main.c Outdated
Comment thread samples/bluetooth/ble_device_name/src/main.c Outdated
Comment on lines +41 to +48
static struct ble_adv_config adv_cfg = {
.conn_cfg_tag = CONFIG_NRF_SDH_BLE_CONN_TAG,
.evt_handler = ble_adv_evt_handler,
.adv_data = {
.name_type = BLE_ADV_DATA_FULL_NAME,
.flags = BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE,
},
};
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We only need adv_data part for updating advertising data. Config can be in main or wherever adv_init is called.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oki, I think I understand what you meant, only adv_data really needs global scope since the BLE event handler uses it for ble_adv_data_update(). The rest of the config (conn_cfg_tag, evt_handler) is only needed once at ble_adv_init() time, so I moved that into main() as a local.
Please take a look and let me know if this is what you had in mind?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is correct.

Comment thread samples/bluetooth/ble_device_name/src/main.c Outdated
Comment thread samples/bluetooth/ble_device_name/src/main.c Outdated
goto idle;
}

LOG_INF("Device name set to: %.*s", saved_len, saved_name);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is in practice the same information as the "advertising as ..." log below. I think this can be removed.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's worth keeping it. Setting the device name and initializing advertising are separate steps, so this log specifically confirms sd_ble_gap_device_name_set() succeeded. Might be useful if something later in the advertising setup fails. It's also consistent with the step-by-step logs used throughout init.

nrf_gpio_pin_write(BOARD_PIN_LED_0, !BOARD_LED_ACTIVE_STATE);
nrf_gpio_pin_write(BOARD_PIN_LED_1, !BOARD_LED_ACTIVE_STATE);

LOG_INF("LEDs enabled");
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not necessary for this sample.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I follow what's unnecessary here. Most of the other Bluetooth samples use LEDs (and buttons) for interaction and visual feedback, why should this one be different?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The log entry I mean.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah ok, fixed

Comment thread samples/bluetooth/ble_device_name/src/main.c Outdated
@PizzaAllTheWay PizzaAllTheWay force-pushed the device-name-sample branch 2 times, most recently from e4f3a11 to 25cf9fc Compare May 4, 2026 09:09
@eivindj-nordic eivindj-nordic added this to the v3.0.0 milestone May 7, 2026
@PizzaAllTheWay PizzaAllTheWay force-pushed the device-name-sample branch 4 times, most recently from dc22253 to c5bc75a Compare May 19, 2026 08:31
Add a new BLE sample demonstrating how a peer can read and write
the GAP Device Name characteristic (UUID 0x2A00) over BLE.

The sample:
- Advertises with a configurable default device name.
- Allows connected peers to write a new name with open permissions.
- Updates the SoftDevice GAP name and advertising data immediately.
- Persists the new name to flash (BM ZMS) so it survives reboots.
- Loads the saved name from storage on boot, falling back to the
  Kconfig default if none exists.

Includes sample documentation and changelog entry.

Signed-off-by: Martynas Smilingis <martynas.smilingis@nordicsemi.no>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

doc-required PR must not be merged without tech writer approval.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants