<?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>How can I stop a connection and BLE advertisement?</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/61815/how-can-i-stop-a-connection-and-ble-advertisement</link><description>Dear All, 
 
 I am using the ble_app_template as my baseline. I have created a custom service with various characteristics and I am able to broadcast my service, connect and exchange data with it. 
 
 My next goal is to be able to break the connection</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Thu, 28 May 2020 06:18:11 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/61815/how-can-i-stop-a-connection-and-ble-advertisement" /><item><title>RE: How can I stop a connection and BLE advertisement?</title><link>https://devzone.nordicsemi.com/thread/252073?ContentTypeID=1</link><pubDate>Thu, 28 May 2020 06:18:11 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:ceb20acb-ea30-4057-b5bd-ca4544d4d542</guid><dc:creator>Simonr</dc:creator><description>&lt;p&gt;Hi&lt;/p&gt;
&lt;p&gt;I think this is due to the&amp;nbsp;&lt;strong&gt;on_disconnected()&amp;nbsp;&lt;/strong&gt;function in&amp;nbsp;&lt;strong&gt;ble_advertising.c&lt;/strong&gt; which handles the disconnected events and starts advertising back up upon disconnect events. You will either have to wait for this function to trig and then call &lt;strong&gt;sd_ble_gap_adv_stop()&amp;nbsp;&lt;/strong&gt;or change the&amp;nbsp;&lt;strong&gt;on_disconnected()&lt;/strong&gt; function to not start advertising when called.&lt;/p&gt;
&lt;p&gt;Best regards,&lt;/p&gt;
&lt;p&gt;Simon&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How can I stop a connection and BLE advertisement?</title><link>https://devzone.nordicsemi.com/thread/251907?ContentTypeID=1</link><pubDate>Wed, 27 May 2020 10:49:00 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:dcbd901b-dcf0-4b90-85c6-f7a39f224778</guid><dc:creator>Giannis Anastasopoulos</dc:creator><description>&lt;p&gt;Dear &lt;a href="https://devzone.nordicsemi.com/members/simonr"&gt;Simonr&lt;/a&gt;,&lt;br /&gt;&lt;br /&gt;I have added your snippet, with some small changes:&lt;br /&gt; &lt;pre class="ui-code" data-mode="text"&gt;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_ERROR_INVALID_STATE)
            {
                APP_ERROR_CHECK(err_code);
            }
            m_conn_handle = BLE_CONN_HANDLE_INVALID;
        }
        m_advertising.adv_evt = BLE_ADV_EVT_IDLE;
        err_code = sd_ble_gap_adv_stop(NULL);

        if (err_code != NRF_ERROR_INVALID_STATE)
        {
            APP_ERROR_CHECK(err_code);
        }
    }&lt;/pre&gt; because I am using the v16.0.0 SDK.&lt;br /&gt;&lt;br /&gt;The result is similar to what I had before. If there is no connection, everything seems to work fine.&lt;br /&gt;&lt;br /&gt;But when I have established a connection and I press the button, the connection stops as expected, but the device is still beaconing. Why is that? With the above sample I need to press the button twice. Once the device will disconnect and the next time the beaconing will stop.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How can I stop a connection and BLE advertisement?</title><link>https://devzone.nordicsemi.com/thread/251861?ContentTypeID=1</link><pubDate>Wed, 27 May 2020 08:55:18 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:6d1ba969-3a81-4114-8ee5-ada59217e283</guid><dc:creator>Simonr</dc:creator><description>&lt;p&gt;Hi&lt;/p&gt;
&lt;p&gt;If you call sd_ble_gap_adv_stop() when you aren&amp;#39;t advertising, for example while you&amp;#39;re connected, the function will return the NRF_ERROR_INVALID_STATE. You should not call this function when you aren&amp;#39;t advertising, or you have to ignore the invalid state error message. Same goes for the sd_ble_gap_disconnect() function if you call it if a disconnection is already in progress.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;You can try something like this code snippet to circumvent these errors:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;uint32_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_ERROR_INVALID_STATE)
    {
        APP_ERROR_CHECK(err_code);
    }
}

err_code = sd_ble_gap_adv_stop();
if (err_code != NRF_ERROR_INVALID_STATE)
{
    APP_ERROR_CHECK(err_code);
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;Best regards,&lt;/p&gt;
&lt;p&gt;Simon&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>