<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://test-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>Best way to detect when a client subscribes to the NUS</title><link>https://test-devzone.nordicsemi.com/f/nordic-q-a/87819/best-way-to-detect-when-a-client-subscribes-to-the-nus</link><description>Hi, 
 So we have a project where we use the NUS, and we need to detect when a client subscribes. We eventually found a way, but it seems very obtuse and a bit black magic, so we wondered if this was the way things are &amp;quot;supposed&amp;quot; to work. Can you confirm</description><dc:language>en-US</dc:language><generator>Telligent Community 13 Non-Production</generator><lastBuildDate>Thu, 12 May 2022 08:48:47 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://test-devzone.nordicsemi.com/f/nordic-q-a/87819/best-way-to-detect-when-a-client-subscribes-to-the-nus" /><item><title>RE: Best way to detect when a client subscribes to the NUS</title><link>https://test-devzone.nordicsemi.com/thread/367545?ContentTypeID=1</link><pubDate>Thu, 12 May 2022 08:48:47 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:dc0a0649-a477-4ec0-8fc1-f0c7709e30e9</guid><dc:creator>user112414</dc:creator><description>&lt;p&gt;thanks, this works very well.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Best way to detect when a client subscribes to the NUS</title><link>https://test-devzone.nordicsemi.com/thread/367526?ContentTypeID=1</link><pubDate>Thu, 12 May 2022 07:34:33 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:fabb7b85-a304-432e-b98e-b51543e47847</guid><dc:creator>user4240</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;The NUS service will actually raise an event when notifications are enabled/disabled by the client, but the ble_app_uart example does not subscribe for these events by default so it might be easy to overlook. You can &amp;quot;subscribe&amp;quot; for these events in the nus_data_handler():&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;/**@brief Function for handling the data from the Nordic UART Service.
 *
 * @details This function will process the data received from the Nordic UART BLE Service and send
 *          it to the UART module.
 *
 * @param[in] p_evt       Nordic UART Service event.
 */
/**@snippet [Handling the data received over BLE] */
static void nus_data_handler(ble_nus_evt_t * p_evt)
{

    
    switch (p_evt-&amp;gt;type) 
    {
        case BLE_NUS_EVT_RX_DATA:
        {
    
            uint32_t err_code;

            NRF_LOG_DEBUG(&amp;quot;Received data from BLE NUS. Writing data on UART.&amp;quot;);
            NRF_LOG_HEXDUMP_DEBUG(p_evt-&amp;gt;params.rx_data.p_data, p_evt-&amp;gt;params.rx_data.length);

            for (uint32_t i = 0; i &amp;lt; p_evt-&amp;gt;params.rx_data.length; i++)
            {
                do
                {
                    err_code = app_uart_put(p_evt-&amp;gt;params.rx_data.p_data[i]);
                    if ((err_code != NRF_SUCCESS) &amp;amp;&amp;amp; (err_code != NRF_ERROR_BUSY))
                    {
                        NRF_LOG_ERROR(&amp;quot;Failed receiving NUS message. Error 0x%x. &amp;quot;, err_code);
                        APP_ERROR_CHECK(err_code);
                    }
                } while (err_code == NRF_ERROR_BUSY);
            }
            if (p_evt-&amp;gt;params.rx_data.p_data[p_evt-&amp;gt;params.rx_data.length - 1] == &amp;#39;\r&amp;#39;)
            {
                while (app_uart_put(&amp;#39;\n&amp;#39;) == NRF_ERROR_BUSY);
            }
        } break;

        case BLE_NUS_EVT_COMM_STARTED:
            NRF_LOG_INFO(&amp;quot;Notifications enabled&amp;quot;);
            break;
       
        case BLE_NUS_EVT_COMM_STOPPED:
            NRF_LOG_INFO(&amp;quot;Notifications disabled&amp;quot;);
            break;

        default:
            break;
    }

}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;Best regards,&lt;/p&gt;
&lt;p&gt;Vidar&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>