<?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>nRF Peripheral NUS sample, receiving message from nRF Toolbox UART service</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/120127/nrf-peripheral-nus-sample-receiving-message-from-nrf-toolbox-uart-service</link><description>Hi, I have been experimenting with the peripheral NUS sample program with an nRF9160 DK which has the nRF52840 Bluetooth chip. 
 The sample program sends simple Hello World responses which I am able to receive using nRF Toolbox for Android. My intention</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Thu, 27 Mar 2025 13:07:12 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/120127/nrf-peripheral-nus-sample-receiving-message-from-nrf-toolbox-uart-service" /><item><title>RE: nRF Peripheral NUS sample, receiving message from nRF Toolbox UART service</title><link>https://devzone.nordicsemi.com/thread/529294?ContentTypeID=1</link><pubDate>Thu, 27 Mar 2025 13:07:12 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:355a6c11-b8fd-4ef1-9d43-204ac50d4b13</guid><dc:creator>Vidar Berg</dc:creator><description>&lt;p&gt;There is a section named&amp;nbsp;&amp;quot;Data length and MTU&amp;quot; in the devacademy course here&amp;nbsp;&lt;a id="" href="https://academy.nordicsemi.com/courses/bluetooth-low-energy-fundamentals/lessons/lesson-3-bluetooth-le-connections/topic/connection-parameters/"&gt;https://academy.nordicsemi.com/courses/bluetooth-low-energy-fundamentals/lessons/lesson-3-bluetooth-le-connections/topic/connection-parameters/&lt;/a&gt;&amp;nbsp;which I would recommend reading.&lt;/p&gt;
&lt;p&gt;To support longer MTUs you can add the following lines to your prj.conf file:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;CONFIG_BT_BUF_ACL_RX_SIZE=251
CONFIG_BT_BUF_ACL_TX_SIZE=251
CONFIG_BT_L2CAP_TX_MTU=247&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nRF Peripheral NUS sample, receiving message from nRF Toolbox UART service</title><link>https://devzone.nordicsemi.com/thread/529289?ContentTypeID=1</link><pubDate>Thu, 27 Mar 2025 12:45:17 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:cc98f6a0-e3bd-49c0-9870-e496b3dafc7a</guid><dc:creator>zachary_z</dc:creator><description>&lt;p&gt;I tried to send more than 20 characters. I now tried under 12 characters and it worked!&lt;/p&gt;
&lt;p&gt;Is there a way to increase the payload size?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nRF Peripheral NUS sample, receiving message from nRF Toolbox UART service</title><link>https://devzone.nordicsemi.com/thread/529286?ContentTypeID=1</link><pubDate>Thu, 27 Mar 2025 12:41:15 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:e18fd298-502e-4d05-ad9a-a849f0ee447d</guid><dc:creator>Vidar Berg</dc:creator><description>&lt;p&gt;What is the length of the string you&amp;#39;re trying to send? The send function will return with -ENOMEM (&amp;#39;12&amp;#39;) if you try to send more than the max. payload which is the ATT_MTU size - 3 bytes for the ATT header.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nRF Peripheral NUS sample, receiving message from nRF Toolbox UART service</title><link>https://devzone.nordicsemi.com/thread/529282?ContentTypeID=1</link><pubDate>Thu, 27 Mar 2025 12:28:38 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:e7b62a9a-b81e-4a66-a10e-dd12c258e130</guid><dc:creator>zachary_z</dc:creator><description>&lt;p&gt;Thanks for this! The code mostly works.&lt;/p&gt;
&lt;p&gt;However, I now have another problem. I can&amp;#39;t seem to send a concatenated string with bt_nus_send. Everything else has been done correctly, but the send fails with error -12. I cannot find an error corresponding to the value -12. What could this be?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nRF Peripheral NUS sample, receiving message from nRF Toolbox UART service</title><link>https://devzone.nordicsemi.com/thread/528869?ContentTypeID=1</link><pubDate>Tue, 25 Mar 2025 13:42:55 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:2734eacf-285c-49a3-9eff-f40512c87136</guid><dc:creator>Vidar Berg</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;If you want to modify the sample so it sends back a response when receiving data, you can try something like this:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;peripheral_nus -&amp;gt; main.c&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;struct nus_rx_data {
	char message[CONFIG_BT_L2CAP_TX_MTU + 1];
};

/* 
 * Create message queue to pass NUS rx data to the main thread 
 */
K_MSGQ_DEFINE(nus_msgq, sizeof(struct nus_rx_data), 1, 1);

....

static void received(struct bt_conn *conn, const void *data, uint16_t len, void *ctx)
{
	int err;
	struct nus_rx_data rx_data;

	ARG_UNUSED(conn);
	ARG_UNUSED(ctx);

	memset(rx_data.message, 0x0, sizeof(rx_data.message));
	memcpy(rx_data.message, data, MIN(sizeof(rx_data.message) - 1, len));
	
	printk(&amp;quot;%s() - Len: %d, Message: %s\n&amp;quot;, __func__, len, rx_data.message);

	/* Forward received NUS RX data to main thread */
	err = k_msgq_put(&amp;amp;nus_msgq, &amp;amp;rx_data, K_NO_WAIT);
	if (err) {
		printk(&amp;quot;k_msgq_put() failed. (err %d)\n&amp;quot;, err);
	}
}


....

int main(void) 
{
    ...
    while (true) {

		/* Wait for NUS Command */
		k_msgq_get(&amp;amp;nus_msgq, &amp;amp;rx_data, K_FOREVER);

		//TODO: process received command?

		/* Send back response */
		err = bt_nus_send(NULL, rx_data.message, strlen(rx_data.message));
		printk(&amp;quot;Data send - Result: %d\n&amp;quot;, err);

		if (err &amp;lt; 0 &amp;amp;&amp;amp; (err != -EAGAIN) &amp;amp;&amp;amp; (err != -ENOTCONN)) {
			return err;
		}
	}
&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>