<?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>Problem at advertisement restart with button in custom board using NUS: sd_ble_gap_adv_set_configure returns 0x4 NRF_ERROR_NO_MEM</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/61556/problem-at-advertisement-restart-with-button-in-custom-board-using-nus-sd_ble_gap_adv_set_configure-returns-0x4-nrf_error_no_mem</link><description>Good morning, 
 I&amp;#39;m developing an app for NRF52832 SDK 15.3, SD 132 6.1.1 with NUS service. Moreover my app has DFU and Peer Manager with LESC JustWorks security and other FDS records, although now I&amp;#39;m in a more basic branch without these features so</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Fri, 22 May 2020 08:56:35 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/61556/problem-at-advertisement-restart-with-button-in-custom-board-using-nus-sd_ble_gap_adv_set_configure-returns-0x4-nrf_error_no_mem" /><item><title>RE: Problem at advertisement restart with button in custom board using NUS: sd_ble_gap_adv_set_configure returns 0x4 NRF_ERROR_NO_MEM</title><link>https://devzone.nordicsemi.com/thread/251195?ContentTypeID=1</link><pubDate>Fri, 22 May 2020 08:56:35 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:4fec8eea-698a-471a-ab9c-720c0968f403</guid><dc:creator>Edvin</dc:creator><description>&lt;p&gt;Hello,&lt;/p&gt;
&lt;p&gt;That looks like a valid workaround. Either that, or not manually start the advertisement after you call disconnect would work equally fine. Alternatively, you could filter out the NRF_ERROR_INVALID_STATE on your advertising start call and ignore it (not pass it into APP_ERROR_CHECK(err_code);&lt;/p&gt;
&lt;p&gt;However, if the current workaround is working, you don&amp;#39;t need to change anything. Glad to hear you got it working!&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Best regards,&lt;/p&gt;
&lt;p&gt;Edvin&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Problem at advertisement restart with button in custom board using NUS: sd_ble_gap_adv_set_configure returns 0x4 NRF_ERROR_NO_MEM</title><link>https://devzone.nordicsemi.com/thread/251126?ContentTypeID=1</link><pubDate>Thu, 21 May 2020 10:45:28 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:f65c7568-709f-4d85-8462-c6749953f890</guid><dc:creator>Bruno Santamaria</dc:creator><description>&lt;p&gt;Hi, you were right (of course). I had not tried to find out what that fatal error meant because I thought it was the same as before. But it was indeed NRF_ERROR_INVALID_STATE (8) because advertising had already been restarted when I requested that again.&lt;/p&gt;
&lt;p&gt;I explain the full problem and solution in case it helps anyone reading and in case you think there is a better one:&lt;/p&gt;
&lt;p&gt;When connected and using my function ble_app_disconnect(), a&amp;nbsp;BLE_GAP_EVT_DISCONNECTED was handled in ble_advertising.c, calling on_disconnected(), which restarts advertising even if disconnection was made intentionally.&lt;/p&gt;
&lt;p&gt;What I did is this. I added&amp;nbsp;init.config.ble_adv_on_disconnect_disabled = true; in advertising_init() to prevent on_disconnected from being called. And I added the code from that function to my own handled BLE_GAP_EVNT_DISCONNECTED event, but only in case disconnection was not done on purpose:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;static void ble_evt_handler(ble_evt_t const * p_ble_evt, void * p_context)
{
    uint32_t err_code;

    switch (p_ble_evt-&amp;gt;header.evt_id)
    {
        //{...}

        case BLE_GAP_EVT_DISCONNECTED:
            NRF_LOG_INFO(&amp;quot;Disconnected&amp;quot;);
            // LED indication will be changed when advertising starts.
            m_conn_handle = BLE_CONN_HANDLE_INVALID;

            // &amp;lt;ADDED&amp;gt;
			// If device is not asleep, restart advertising and try to reconnect
			// Advertising restart is done here instead of in ble_advertising.c because ble_adv_on_disconnect_disabled set true
			// If device is asleep, device initiated disconnection. Don&amp;#39;t start advertising
			if(device.flags.is_not_asleep == 1){
				m_advertising.whitelist_temporarily_disabled = false;

				if (p_ble_evt-&amp;gt;evt.gap_evt.conn_handle == m_advertising.current_slave_link_conn_handle)
				{
				   err_code = ble_advertising_start(&amp;amp;m_advertising, BLE_ADV_MODE_DIRECTED_HIGH_DUTY);
				   if ((err_code != NRF_SUCCESS) &amp;amp;&amp;amp; (m_advertising.error_handler != NULL))
				   {
					   m_advertising.error_handler(err_code);
				   }
				}
			}
			// &amp;lt;/ADDED&amp;gt;
            break;
        
        //{...}
        default:
            // No implementation needed.
            break;
    }
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;Thanks &lt;a href="https://devzone.nordicsemi.com/members/edvin-holmseth"&gt;Edvin&lt;/a&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Problem at advertisement restart with button in custom board using NUS: sd_ble_gap_adv_set_configure returns 0x4 NRF_ERROR_NO_MEM</title><link>https://devzone.nordicsemi.com/thread/250955?ContentTypeID=1</link><pubDate>Wed, 20 May 2020 12:04:47 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:9bf1e737-13a8-4393-9c84-cd384d0d1851</guid><dc:creator>Edvin</dc:creator><description>&lt;p&gt;Whenever you see &amp;quot;Fatal error&amp;quot; in your log, you should go to your preprocessor definitions and add &amp;quot;DEBUG&amp;quot; as a preprocessor define. If you do this, then the log will tell you what APP_ERROR_CHECK(err_code) that received an err_code != 0. Try to do that. If you are not sure how to change your preprocessor definitions, please let me know what IDE you are using.&lt;/p&gt;
&lt;p&gt;I guess you are trying to restart advertising twice, and the last one returns NRF_ERROR_INVALID_STATE (8).&lt;/p&gt;
&lt;p&gt;I can only guess what might happen, but the fastest way is for you to check what causes the fatal error by checking the log with the DEBUG preprocessor define&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Problem at advertisement restart with button in custom board using NUS: sd_ble_gap_adv_set_configure returns 0x4 NRF_ERROR_NO_MEM</title><link>https://devzone.nordicsemi.com/thread/250886?ContentTypeID=1</link><pubDate>Wed, 20 May 2020 09:26:09 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:28410cfd-3bd5-4361-82f4-5512af4ccb85</guid><dc:creator>Bruno Santamaria</dc:creator><description>&lt;p&gt;Of course, it makes sense. Thanks! Truth be told, I don&amp;#39;t know what is that line doing there. I guess the previous developer copied that from some example without knowing.&lt;/p&gt;
&lt;p&gt;I have removed it and now I can restart advertising... only when not connected to the central device.&lt;/p&gt;
&lt;p&gt;This is the log of that situation:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;Button ON-OFF pressed

Sleep!

&amp;lt;info&amp;gt; app: BLE Disconnected

&amp;lt;info&amp;gt; app: Disconnected

Button ON-OFF released 

Button ON-OFF pressed 

Wake up!

Button ON-OFF released 

&amp;lt;info&amp;gt; app: Before AdvRestart, advertising handle: 0

&amp;lt;error&amp;gt; app: Fatal error&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;I&amp;#39;ve tried to change the code in my ble_app_disconnect() function to only stop advertising when not connected, but result was the same.&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;void ble_app_disconnect(void){
	ret_code_t err_code;

	if (m_conn_handle != BLE_CONN_HANDLE_INVALID)
	{
		err_code = sd_ble_gap_disconnect(m_conn_handle, BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);
		if (err_code == NRF_SUCCESS){
			NRF_LOG_INFO(&amp;quot;BLE Disconnected&amp;quot;);
		} else if (err_code != NRF_ERROR_INVALID_STATE){
			APP_ERROR_CHECK(err_code);
			NRF_LOG_INFO(&amp;quot;BLE Not previously connected&amp;quot;);
		}
	} else {
		ble_app_advertising_stop();
	}
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;With my debug lines I see that when advertising/disconnected, my log prints &lt;strong&gt;&amp;lt;info&amp;gt; app: BLE advertising stopped&lt;/strong&gt;, from function&amp;nbsp;&lt;strong&gt;ble_app_advertising_stop&lt;/strong&gt;. When not advertising/connected, it prints&amp;nbsp;&lt;strong&gt;&amp;lt;info&amp;gt; app: BLE Disconnected &lt;/strong&gt;from function&amp;nbsp;&lt;strong&gt;ble_app_disconnect&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;This seems to be an additional problem. Any idea? If you need any other debug information please tell me.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Problem at advertisement restart with button in custom board using NUS: sd_ble_gap_adv_set_configure returns 0x4 NRF_ERROR_NO_MEM</title><link>https://devzone.nordicsemi.com/thread/250874?ContentTypeID=1</link><pubDate>Wed, 20 May 2020 08:48:48 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:13b669ed-06f3-45ce-a669-3de8e79f315a</guid><dc:creator>Edvin</dc:creator><description>&lt;p&gt;So you set the advertising handle to 0xFF when you stop advertising. Is there a particular reason for this? I suspect that if you comment out this line in ble_app_advertising_stop it will work as you intend:&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;        m_advertising.adv_handle = (uint8_t)BLE_GAP_ADV_SET_HANDLE_NOT_SET;&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Problem at advertisement restart with button in custom board using NUS: sd_ble_gap_adv_set_configure returns 0x4 NRF_ERROR_NO_MEM</title><link>https://devzone.nordicsemi.com/thread/250870?ContentTypeID=1</link><pubDate>Wed, 20 May 2020 08:38:52 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:05a4aa4d-5056-4f2d-bcaf-168aa7f2efb0</guid><dc:creator>Bruno Santamaria</dc:creator><description>&lt;p&gt;Hi &lt;a href="https://devzone.nordicsemi.com/members/edvin-holmseth"&gt;Edvin&lt;/a&gt;,&lt;/p&gt;
&lt;p&gt;all my three advertising funtions (&lt;span&gt;ble_advertising_init(),&amp;nbsp;ble_advertising_start() and&amp;nbsp;ble_app_advertising_restart()) are placed in a file, which I&amp;#39;ve named ble_app.c. On top of that file I define m_advertising instance. Here I show you this code:&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;BLE_ADVERTISING_DEF(m_advertising);                                                 /**&amp;lt; Advertising module instance. */

/**@brief Function for initializing the Advertising functionality.
 */
void ble_app_advertising_init(void)
{
    uint32_t               err_code;
    ble_advertising_init_t init;

    memset(&amp;amp;init, 0, sizeof(init));

    init.advdata.name_type          = BLE_ADVDATA_FULL_NAME;
    init.advdata.include_appearance = false;
    init.advdata.flags              = BLE_GAP_ADV_FLAGS_LE_ONLY_LIMITED_DISC_MODE;

    init.srdata.uuids_complete.uuid_cnt = sizeof(m_adv_uuids) / sizeof(m_adv_uuids[0]);
    init.srdata.uuids_complete.p_uuids  = m_adv_uuids;

    init.config.ble_adv_fast_enabled  = true;
    init.config.ble_adv_fast_interval = APP_ADV_INTERVAL;
    init.config.ble_adv_fast_timeout  = APP_ADV_DURATION;
    init.evt_handler = on_adv_evt;

    err_code = ble_advertising_init(&amp;amp;m_advertising, &amp;amp;init);
    APP_ERROR_CHECK(err_code);

    ble_advertising_conn_cfg_tag_set(&amp;amp;m_advertising, APP_BLE_CONN_CFG_TAG);
	NRF_LOG_INFO(&amp;quot;After AdvInit, advertising handle: %d&amp;quot;, m_advertising.adv_handle);
}

/**@brief Function for starting advertising.
 */
void ble_app_advertising_start(void)
{
    uint32_t err_code = ble_advertising_start(&amp;amp;m_advertising, BLE_ADV_MODE_FAST);
    APP_ERROR_CHECK(err_code);
	NRF_LOG_INFO(&amp;quot;After AdvStart, advertising handle: %d&amp;quot;, m_advertising.adv_handle);
}


/**@brief Function for stopping advertising.
 */
void ble_app_advertising_stop(void)
{
    ret_code_t err_code = NRF_SUCCESS;

    if (m_advertising.adv_handle != (uint8_t)BLE_GAP_ADV_SET_HANDLE_NOT_SET)
    {
        err_code = sd_ble_gap_adv_stop(m_advertising.adv_handle);
        if (err_code == NRF_SUCCESS){
			NRF_LOG_INFO(&amp;quot;BLE advertising stopped&amp;quot;);
		} else if (err_code != NRF_ERROR_INVALID_STATE){
			NRF_LOG_INFO(&amp;quot;BLE not previously advertising&amp;quot;);
            APP_ERROR_CHECK(err_code);
        }
        m_advertising.adv_handle = (uint8_t)BLE_GAP_ADV_SET_HANDLE_NOT_SET;
    } 
}

void ble_app_advertising_restart(void){
	ret_code_t err_code;
	NRF_LOG_INFO(&amp;quot;Before AdvRestart, advertising handle: %d&amp;quot;, m_advertising.adv_handle);
	err_code = ble_advertising_start(&amp;amp;m_advertising, BLE_ADV_MODE_FAST);
	if(err_code != NRF_SUCCESS)
	{
		APP_ERROR_CHECK(err_code);
	}
}&lt;/pre&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Here is the result that you requested:&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;pre class="ui-code" data-mode="text"&gt;&amp;lt;info&amp;gt; app: After AdvInit, advertising handle: 0

&amp;lt;info&amp;gt; app: After AdvStart, advertising handle: 0

&amp;lt;info&amp;gt; app: Before AdvRestart, advertising handle: 255

&amp;lt;error&amp;gt; app: Fatal error&lt;/pre&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;It is the same result if there is a connection with a central device and if there is not (in this case the module is advertising which I stop with ble_app_advertising_stop).&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Problem at advertisement restart with button in custom board using NUS: sd_ble_gap_adv_set_configure returns 0x4 NRF_ERROR_NO_MEM</title><link>https://devzone.nordicsemi.com/thread/250776?ContentTypeID=1</link><pubDate>Tue, 19 May 2020 14:27:38 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:eec0be54-9258-4a0a-b067-e2346b480e26</guid><dc:creator>Edvin</dc:creator><description>&lt;p&gt;Hello,&lt;/p&gt;
&lt;p&gt;Do you use the same instance of m_advertising in your ble_app_advertising_restart?&lt;/p&gt;
&lt;p&gt;Try to print out the advertising handle at these points:&lt;/p&gt;
&lt;p&gt;1: After ble_advertising_init()&lt;/p&gt;
&lt;p&gt;2: Before ble_advertising_start();&lt;/p&gt;
&lt;p&gt;3: Before you restart your advertising&amp;nbsp;in your custom&amp;nbsp;ble_app_advertising_restart()&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;You can print it like this:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;NRF_LOG_INFO(&amp;quot;advertising handle: %d&amp;quot;, m_advertising.adv_handle);&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;What is the advertising handle at the different scenarios?&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;BR,&lt;/p&gt;
&lt;p&gt;Edvin&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>