Need a working example that uses CONFIG_BT_SETTINGS and stores a BLE random static address to memory

I have searched everywhere and can't find a working example that shows how to create a new BLE ID and save it to the setting s partition. 

Please show me the configurations necessary to create a new BLE address and show that it was saved upon a reset. Ideally with the nrf52840dk.

Below is a simplification of my code so far.

// prj.conf
CONFIG_LOG=y

CONFIG_BT=y
CONFIG_BT_PERIPHERAL=y
CONFIG_BT_DEVICE_NAME="Nordic_Peripheral"
CONFIG_BT_PRIVACY=y

CONFIG_NVS=y
CONFIG_FLASH=y
CONFIG_FLASH_MAP=y
CONFIG_MPU_ALLOW_FLASH_WRITE=y

CONFIG_SETTINGS=y
CONFIG_SETTINGS_NVS=y
CONFIG_SETTINGS_RUNTIME=y
CONFIG_SETTINGS_NONE=y

CONFIG_BT_SETTINGS=y
CONFIG_BT_ID_MAX=2
CONFIG_BT_GATT_CACHING=n

// main.c
#include <zephyr/bluetooth/bluetooth.h>
#include <zephyr/bluetooth/addr.h>
#include <zephyr/settings/settings.h>

size_t num_ids = 0;

void print_ids(void)
{
  char addr_str[BT_ADDR_LE_STR_LEN];

  bt_id_get(NULL, &num_ids);
  printk("Num IDs: %d\n", num_ids);
  bt_addr_le_t addrs[num_ids];
  
  for (int i = 0; i < num_ids; i++) {
    bt_id_get(addrs, &num_ids);
    bt_addr_le_to_str(&addrs[i], addr_str, sizeof(addr_str));
    printk("Addr[%d]: %s\n", i, addr_str);
  }
}

int main(void) {
  print_ids();

  int err = bt_enable(NULL);
  if (err) printk("Bluetooth init failed (err %d)\n", err);

  err = settings_load();
  if (err) printk("settings_load failed (err %d)\n", err);

  print_ids();

  if (num_ids < CONFIG_BT_ID_MAX) {
    bt_addr_le_t new_addr;

    err = bt_addr_le_from_str("FF:EE:DD:CC:BB:AA", "random", &new_addr);
    if (err) printk("Invalid BT address (err %d)\n", err);

    err = bt_id_create(&new_addr, NULL);
    if (err) printk("Creating new ID failed (err %d)\n", err);
    
    print_ids();
  }
	
	// Continue with setup
}
Here is the output, which stays the same on each reset.
*** Booting Zephyr OS build v4.0.0 ***
Num IDs: 0
[00:00:00.251,983] <wrn> net_buf: Timeout discarded. No blocking in syswq
[00:00:00.252,105] <wrn> net_buf: Timeout discarded. No blocking in syswq
[00:00:00.252,227] <wrn> net_buf: Timeout discarded. No blocking in syswq
[00:00:00.252,380] <wrn> net_buf: Timeout discarded. No blocking in syswq
[00:00:00.252,502] <wrn> net_buf: Timeout discarded. No blocking in syswq
[00:00:00.252,624] <wrn> net_buf: Timeout discarded. No blocking in syswq
[00:00:00.252,746] <wrn> net_buf: Timeout discarded. No blocking in syswq
[00:00:00.252,868] <wrn> net_buf: Timeout discarded. No blocking in syswq
[00:00:00.252,990] <wrn> net_buf: Timeout discarded. No blocking in syswq
[00:00:00.253,112] <inf> bt_hci_core: HW Platform: Nordic Semiconductor (0x0002)
[00:00:00.253,112] <inf> bt_hci_core: HW Variant: nRF52x (0x0002)
[00:00:00.253,173] <inf> bt_hci_core: Firmware: Standard Bluetooth controller (0x00) Version 4.0 Build 0
[00:00:00.253,204] <wrn> net_buf: Timeout discarded. No blocking in syswq
[00:00:00.253,356] <wrn> net_buf: Timeout discarded. No blocking in syswq
[00:00:00.253,479] <inf> bt_hci_core: No ID address. App must call settings_load()
[00:00:00.253,540] <wrn> net_buf: Timeout discarded. No blocking in syswq
[00:00:00.253,662] <wrn> net_buf: Timeout discarded. No blocking in syswq
[00:00:00.253,845] <inf> bt_hci_core: Identity: DA:B6:E1:70:F7:25 (random)
[00:00:00.253,875] <inf> bt_hci_core: HCI: version 5.4 (0x0d) revision 0x0000, manufacturer 0x05f1
[00:00:00.253,906] <inf> bt_hci_core: LMP: version 5.4 (0x0d) subver 0xffff
Num IDs: 1
Addr[0]: DA:B6:E1:70:F7:25 (random)
[00:00:00.254,089] <err> bt_settings: Failed to save ID (err -2)
Creating new ID failed (err 1)
Num IDs: 2
Addr[0]: DA:B6:E1:70:F7:25 (random)
Addr[1]: FF:EE:DD:CC:BB:AA (random)
Parents Reply Children
No Data
Related