<?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>Disconnect from Peripheral and stop Advertising</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/19384/disconnect-from-peripheral-and-stop-advertising</link><description>Hello , 
 I&amp;#39;m working on custom board, I&amp;#39;m trying to disconnect from the Central (Andriod app) when the battery is below certain level and stop re-advertising.
Below is the piece of code i used: 
 /* Disconnect and then disable advertising */ 
 if</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Wed, 01 Feb 2017 13:56:36 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/19384/disconnect-from-peripheral-and-stop-advertising" /><item><title>RE: Disconnect from Peripheral and stop Advertising</title><link>https://devzone.nordicsemi.com/thread/75216?ContentTypeID=1</link><pubDate>Wed, 01 Feb 2017 13:56:36 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:3cc6d95b-5434-4345-bc48-f1d2c9e24e19</guid><dc:creator>Sigurd</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;If you call &lt;code&gt;sd_ble_gap_adv_stop()&lt;/code&gt; when you are not advertising, the function will return &lt;code&gt;NRF_ERROR_INVALID_STATE&lt;/code&gt;. The APP_ERROR_CHECK() will therefore call the error handling function. Don&amp;#39;t call the &lt;code&gt;sd_ble_gap_adv_stop()&lt;/code&gt; when you are not advertising, or ignore the invalid state error message. The &lt;code&gt;sd_ble_gap_disconnect()&lt;/code&gt; function will also return &lt;code&gt;NRF_ERROR_INVALID_STATE&lt;/code&gt; if you try to call it when e.g. a disconnection is already in progress. Try this code instead, and see if it solves your issues:&lt;/p&gt;
&lt;pre&gt;&lt;code&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;/code&gt;&lt;/pre&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>