<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://devzone.nordicsemi.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>nRF5340 - setting public bt address</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/104222/nrf5340---setting-public-bt-address</link><description>Hello, 
 Still on my custom nRF5340 board for a Bluetooth LE peripheral application (building environment: NCS 2.2.0, Zephyr 3.2.99). The net core is populated with the Zephyr HCI RPMsg sample LE Controller as it is. I don&amp;#39;t want to change it if possible</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Wed, 27 Sep 2023 17:11:13 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/104222/nrf5340---setting-public-bt-address" /><item><title>RE: nRF5340 - setting public bt address</title><link>https://devzone.nordicsemi.com/thread/448020?ContentTypeID=1</link><pubDate>Wed, 27 Sep 2023 17:11:13 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:67ee7946-647d-449a-9520-f320b83195a3</guid><dc:creator>Gabriele</dc:creator><description>&lt;p&gt;I&amp;#39;ve solved as follows, strongly inspired to &lt;a href="https://devzone.nordicsemi.com/f/nordic-q-a/77164/setting-the-ble-public-address-on-nrf5340/325131"&gt;link&lt;/a&gt;.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;#include &amp;lt;zephyr/bluetooth/hci_vs.h&amp;gt;

static char bt_mac_buf[BT_ADDR_LE_STR_LEN];

void ble_set_mac(bt_addr_t* addr)
{
	struct net_buf *buf;
	int rc;

	buf = bt_hci_cmd_create(BT_HCI_OP_VS_WRITE_BD_ADDR, sizeof(*addr));
	if (!buf) {
		LOG_ERR(&amp;quot;failed to create buffer [%d]&amp;quot;, -ENOBUFS);
	}

	net_buf_add_mem(buf, addr, sizeof(*addr));

	rc = bt_hci_cmd_send_sync(BT_HCI_OP_VS_WRITE_BD_ADDR, buf, NULL);
	if (rc) {
		LOG_ERR(&amp;quot;failed to set BT address [%d]&amp;quot;, rc);
	}
}

int ble_init(void)
{
	bt_enable(NULL);

	/* check for public address in UICR.OPT area */
	if(NRF_UICR-&amp;gt;OTP[0] != 0xffffffff &amp;amp;&amp;amp; \
			NRF_UICR-&amp;gt;OTP[1] != 0xffffffff) {
		LOG_INF(&amp;quot;setting public address retrieved from UICR&amp;quot;);
		bt_addr_t addr;
		uint8_t tmp[BT_ADDR_SIZE];
		int i;
		memcpy(&amp;amp;tmp[0], (uint8_t *)&amp;amp;NRF_UICR-&amp;gt;OTP[0], BT_ADDR_SIZE);
		for (i=0; i &amp;lt; BT_ADDR_SIZE; i++) {
			addr.val[i] = tmp[BT_ADDR_SIZE - i -1];
		}

		ble_set_mac(&amp;amp;addr);

	} else {
		LOG_INF(&amp;quot;going to set random static address&amp;quot;);
	}

	if (IS_ENABLED(CONFIG_SETTINGS)) {
		settings_load();
	}
}
&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>