<?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>When is NUS ready?</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/111767/when-is-nus-ready</link><description>Hi! 
 We have two similar nRF52840 boards and want them to talk via nus_client and nus_server. 
 At the moment, we consider connection established after client receives the generic bluetooth connected callback and setups nus (below code). 
 
 Of course</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Mon, 24 Jun 2024 09:02:17 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/111767/when-is-nus-ready" /><item><title>RE: When is NUS ready?</title><link>https://devzone.nordicsemi.com/thread/490185?ContentTypeID=1</link><pubDate>Mon, 24 Jun 2024 09:02:17 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:1de26481-0868-451d-b3e7-bfa283d9d08b</guid><dc:creator>Sigurd</dc:creator><description>&lt;p&gt;In the bonded use-case, try waiting for the&amp;nbsp;security_changed callback as well.&lt;/p&gt;
&lt;p&gt;If that does not work either, then a sniffer trace might be useful here:&amp;nbsp;&lt;a href="https://www.nordicsemi.com/Products/Development-tools/nrf-sniffer-for-bluetooth-le"&gt;https://www.nordicsemi.com/Products/Development-tools/nrf-sniffer-for-bluetooth-le&lt;/a&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: When is NUS ready?</title><link>https://devzone.nordicsemi.com/thread/489168?ContentTypeID=1</link><pubDate>Mon, 17 Jun 2024 23:47:42 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:04304c5d-c9d6-4e43-b229-1186dfa75b97</guid><dc:creator>kat829</dc:creator><description>&lt;p&gt;Yes, we have.&lt;/p&gt;
&lt;p&gt;Any ideas what to do about it?&lt;/p&gt;
&lt;p&gt;(I.e., to find out when the client is actually ready.)&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: When is NUS ready?</title><link>https://devzone.nordicsemi.com/thread/489167?ContentTypeID=1</link><pubDate>Mon, 17 Jun 2024 23:18:09 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:6b2841f8-7146-4280-a4d1-b874c2960098</guid><dc:creator>Sigurd</dc:creator><description>[quote user="kat829"]NUS server reports send_enabled before the client even starts gatt discovery.[/quote]
&lt;p&gt;Hva you paired and bonded to the client? If yes, then this might explain why this happens.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: When is NUS ready?</title><link>https://devzone.nordicsemi.com/thread/489163?ContentTypeID=1</link><pubDate>Mon, 17 Jun 2024 21:59:37 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:47d0caa2-0b39-410f-8fb8-2d61be5d72f7</guid><dc:creator>kat829</dc:creator><description>&lt;p&gt;Bump?&lt;/p&gt;
&lt;p&gt;Tl;dr: current state of affairs is that NUS server reports send_enabled before the client even starts gatt discovery.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: When is NUS ready?</title><link>https://devzone.nordicsemi.com/thread/488581?ContentTypeID=1</link><pubDate>Thu, 13 Jun 2024 00:36:26 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:c22290bd-49ce-476d-9f31-67361643ab25</guid><dc:creator>kat829</dc:creator><description>&lt;p&gt;Well, there has to be something wrong about our client initialization sequence.&lt;br /&gt;&lt;br /&gt;The trouble seems to be that the NUS server receives the BT_NUS_SEND_STATUS_ENABLED *before* the other device even starts the gatt discovery (see code bellow).&lt;/p&gt;
&lt;p&gt;&lt;br /&gt;Can you help me understand where exactly subscribing to the RX characteristic of NUS server takes place?&lt;br /&gt;&lt;br /&gt;Currently we do something along these lines:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;// Here we start a NUS server. (The device communicates over two NUS connections - one as client, one as a server.)

// We also start advertising HID and NUS...

...

// NusClient_Init()
bt_scan_init(&amp;amp;scan_init);
bt_scan_cb_register(&amp;amp;scan_cb);
bt_scan_filter_add(BT_SCAN_FILTER_TYPE_ADDR, &amp;amp;Peers[PeerIdClient].addr);
bt_scan_filter_enable(BT_SCAN_ADDR_FILTER, false);

bt_nus_client_init(&amp;amp;nus_client, &amp;amp;init);
bt_scan_start(BT_SCAN_TYPE_SCAN_ACTIVE);

// connected(...)
// We receive the connected(struct bt_conn *conn, uint8_t err) callback.
// We set some connection parameters (bt_conn_le_param_update and bt_conn_le_data_len_update)

// somewhere at this point, nus server reports BT_NUS_SEND_STATUS_ENABLED and starts sending data

k_sleep(K_MSEC(4000)); // all data sent during this delay are lost

// NusClient_Setup()
bt_gatt_exchange_mtu(conn, &amp;amp;exchange_params);
bt_conn_set_security(conn, BT_SECURITY_L2);
bt_gatt_dm_start(conn, BT_UUID_NUS_SERVICE, &amp;amp;discovery_cb, &amp;amp;nus_client);

...

static void discovery_complete(struct bt_gatt_dm *dm, void *context) {
    struct bt_nus_client *nus = context;
    bt_gatt_dm_data_print(dm);
    bt_nus_handles_assign(dm, nus);
    bt_nus_subscribe_receive(nus);
    bt_gatt_dm_data_release(dm);

    // we consider the client initialized here.
}&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: When is NUS ready?</title><link>https://devzone.nordicsemi.com/thread/488361?ContentTypeID=1</link><pubDate>Tue, 11 Jun 2024 14:25:43 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:f1f1bc7f-8f3c-47f1-b517-7df87c29b942</guid><dc:creator>kat829</dc:creator><description>&lt;p&gt;The same.&lt;/p&gt;
&lt;p&gt;(LogBt originally evaluated to `&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; err = bt_nus_send(NULL, data, len);`, but supplementing it with a connection pointer doesn&amp;#39;t make a difference.)&lt;/p&gt;
&lt;p&gt;(Similarly, throwing out the LogBt machinery and sending raw data with `bt_nus_send(conn, data, sizeof(data));` right in the main loop doesn&amp;#39;t make a difference.)&lt;/p&gt;
&lt;p&gt;In case you spot something in our logs, here they are:&lt;/p&gt;
&lt;p&gt;server:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;sync:
I: Initialized
I: 2 Sectors of 4096 bytes
I: alloc wra: 0, f38
I: data wra: 0, 160
I: SoftDevice Controller build revision:
I: 36 f0 e5 0e 87 68 48 fb |6....hH.
I: 02 fd 9f 82 cc 32 e5 7b |.....2.{
I: 91 b1 5c ed             |..\.
I: HW Platform: Nordic Semiconductor (0x0002)
I: HW Variant: nRF52x (0x0002)
I: Firmware: Standard Bluetooth controller (0x00) Version 54.58864 Build 1214809870
I: No ID address. App must call settings_load()
Settings: Found peer &amp;#39;client&amp;#39; with address 92a4ce1198f0
I: Identity: E7:60:F0:D9:98:51 (random)
I: HCI: version 5.4 (0x0d) revision 0x118f, manufacturer 0x0059
I: LMP: version 5.4 (0x0d) subver 0x118f
NUS advertising continued
W: opcode 0x200a status 0x0d
Connected to client (f0:98:11:ce:a4:92)
NUS data sent to client (f0:98:11:ce:a4:92)
NUS data sent to client (f0:98:11:ce:a4:92)
NUS data sent to client (f0:98:11:ce:a4:92)
NUS data sent to client (f0:98:11:ce:a4:92)
NUS data sent to client (f0:98:11:ce:a4:92)
NUS data sent to client (f0:98:11:ce:a4:92)
NUS data sent to client (f0:98:11:ce:a4:92)
&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;client:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;client$ ATT error code: 0x0E
Disconnected from server (e7:60:f0:d9:98:51), reason 8
Start scan
Client failed to send data over BLE connection (err -128)
Filters matched: server (e7:60:f0:d9:98:51), connectable:1
Scan connecting: server (e7:60:f0:d9:98:51)
Filters matched: server (e7:60:f0:d9:98:51), connectable:0
Connecting failed: server (e7:60:f0:d9:98:51)
Connected to server (e7:60:f0:d9:98:51)
Failed to set security for server (e7:60:f0:d9:98:51): -12
MTU exchange done with server (e7:60:f0:d9:98:51)
Service discovery completed
server: === Ping 11
server: ---;
server: === Ping 12
server: ---&amp;lt;
server: === Ping 13
server: ---=
server: === Ping 14
server: ---&amp;gt;
server: === Ping 15
server: ---?
...

&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Scenario: server restarts and once nus connection is established, it tries to ping the client. (`---` lines are from a direct raw call to `bt_nus_send(conn, data, sizeof(data))`)&lt;/p&gt;
&lt;p&gt;Current relevant code from server main loop is:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;    while (NusServerInitialized == false || RightConn == NULL) {
        k_sleep(K_MSEC(1));
    }
    for (int i = 0; i &amp;lt; 30; i++) {
        LogBt(&amp;quot;=== Ping %i\n&amp;quot;, i);
        uint8_t data[20] = {
            MessageId_Log,
            &amp;#39;-&amp;#39;,
            &amp;#39;-&amp;#39;,
            &amp;#39;-&amp;#39;,
            &amp;#39;0&amp;#39;+i,
            &amp;#39;\n&amp;#39;,
            0
        };
        int err = bt_nus_send(RightConn, data, sizeof(data));
        if (err) {
            printk(&amp;quot;Failed to send message (err: %d)\n&amp;quot;, err);
        }
        k_sleep(K_MSEC(50));&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: When is NUS ready?</title><link>https://devzone.nordicsemi.com/thread/488316?ContentTypeID=1</link><pubDate>Tue, 11 Jun 2024 12:14:54 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:620ecdbc-f531-4421-9b0e-bf071ca533c5</guid><dc:creator>Sigurd</dc:creator><description>&lt;p&gt;Hi!&lt;/p&gt;
&lt;p&gt;Do you see any difference with&lt;span&gt;`bt_nus_send(current_conn&amp;nbsp;, data, len);` instead?&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: When is NUS ready?</title><link>https://devzone.nordicsemi.com/thread/488186?ContentTypeID=1</link><pubDate>Mon, 10 Jun 2024 19:41:53 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:6b769097-a427-4d12-9423-6074a8f11149</guid><dc:creator>kat829</dc:creator><description>&lt;p&gt;Actually, we are still having problems when sending messages from nus server to nus client.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;static void send_enabled(enum bt_nus_send_status status)
{
    if (status == BT_NUS_SEND_STATUS_ENABLED) {
        NusServerInitialized = true;
    }
}

static struct bt_nus_cb nus_cb = {
    .received = received,
    .sent = sent,
    .send_enabled = send_enabled,
};
&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;    while (NusServerInitialized == false) {
        k_sleep(K_MSEC(1));
    }
    for (int i = 0; i &amp;lt; 30; i++) {
        LogBt(&amp;quot;=== Ping %i\n&amp;quot;, i);
        k_sleep(K_MSEC(50));
    }&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;From the very first message, `bt_nus_send(NULL, data, len);` succeeds, we receive the `static void sent(struct bt_conn *conn)` callback with correct address, yet first 8 messages don&amp;#39;t arrive on the client. &lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: When is NUS ready?</title><link>https://devzone.nordicsemi.com/thread/487224?ContentTypeID=1</link><pubDate>Tue, 04 Jun 2024 07:46:13 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:c2a70971-2c86-4a26-9809-f0e77604608e</guid><dc:creator>kat829</dc:creator><description>&lt;p&gt;Ok, thanks!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: When is NUS ready?</title><link>https://devzone.nordicsemi.com/thread/487181?ContentTypeID=1</link><pubDate>Mon, 03 Jun 2024 23:28:14 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:78857b8e-22b8-4e5d-8e6c-70db860c6517</guid><dc:creator>Sigurd</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;When you get the&amp;nbsp;discovery_complete() and after&amp;nbsp;bt_nus_subscribe_receive() is called in that callback.&lt;/p&gt;
&lt;p&gt;&lt;a href="https://github.com/nrfconnect/sdk-nrf/blob/v2.6.1/samples/bluetooth/central_uart/src/main.c#L296"&gt;https://github.com/nrfconnect/sdk-nrf/blob/v2.6.1/samples/bluetooth/central_uart/src/main.c#L296&lt;/a&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: When is NUS ready?</title><link>https://devzone.nordicsemi.com/thread/487121?ContentTypeID=1</link><pubDate>Mon, 03 Jun 2024 12:30:06 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:bba0e411-a553-445d-96bc-2d701a29413b</guid><dc:creator>kat829</dc:creator><description>&lt;p&gt;Allright, I can see this used in nus server initialization.&lt;/p&gt;
&lt;p&gt;Is there a similar callback for nus client?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: When is NUS ready?</title><link>https://devzone.nordicsemi.com/thread/487114?ContentTypeID=1</link><pubDate>Mon, 03 Jun 2024 12:09:57 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:eb50de56-1ee8-436e-acb4-be9f61853d9f</guid><dc:creator>Sigurd</dc:creator><description>&lt;p&gt;Hi!&lt;/p&gt;
&lt;p&gt;You can add a callback like this. This will be called when the client have enabled notifications, and you can then start sending&amp;nbsp;&lt;span&gt;notifications.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;static void send_enabled(enum bt_nus_send_status status)
{

	LOG_INF(&amp;quot;Notifications %sabled&amp;quot;, (status == BT_NUS_SEND_STATUS_ENABLED) ? &amp;quot;en&amp;quot; : &amp;quot;dis&amp;quot;);
}

static struct bt_nus_cb nus_cb = {
	.received = bt_receive_cb,
	.send_enabled = send_enabled,
};&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>