<?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>Enabling and disabling Bluetooth</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/51626/enabling-and-disabling-bluetooth</link><description>Hi there, 
 I&amp;#39;m currently working on a project in which I need to be able to deactivate Bluetooth advertising at the press of a button. I am using SDK 15.3 and have built a custom service from the ble_app_template example, following this tutorial . I</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Thu, 12 Sep 2019 07:54:02 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/51626/enabling-and-disabling-bluetooth" /><item><title>RE: Enabling and disabling Bluetooth</title><link>https://devzone.nordicsemi.com/thread/209325?ContentTypeID=1</link><pubDate>Thu, 12 Sep 2019 07:54:02 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:2d381fd7-22f0-4e6f-b88f-0f4ed0a51218</guid><dc:creator>Simonr</dc:creator><description>&lt;p&gt;Glad you were able to figure it out, Charlie! Please verify the answer, so it will be easy for people with the same problem in the future to find the correct answer.&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: Enabling and disabling Bluetooth</title><link>https://devzone.nordicsemi.com/thread/209276?ContentTypeID=1</link><pubDate>Wed, 11 Sep 2019 21:39:53 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:31c834a4-749a-4ed0-8ff6-2c22355537e7</guid><dc:creator>Charlie Tuer</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;I finally managed to get everything working the other day. My main mistake was trying to run advertising_stop() immediately after advertising_disconnect() as the stop function would be called before the disconnect event was complete. To fix this, I added the stop function to the end of my &lt;span&gt;BLE_GAP_EVT_DISCONNECTED&amp;nbsp;&lt;/span&gt;event structure to ensure it was competed first (sd_ble_gap_disconnect(), once complete, will generate the&amp;nbsp;&lt;span&gt;BLE_GAP_EVT_DISCONNECTED evt_id and thus trigger the observer leading to&amp;nbsp;the event handler).&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Thank you for your help,&lt;/p&gt;
&lt;p&gt;Charlie&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Enabling and disabling Bluetooth</title><link>https://devzone.nordicsemi.com/thread/208419?ContentTypeID=1</link><pubDate>Fri, 06 Sep 2019 09:07:49 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:c7b2586a-d269-4c02-9dc0-a95597952e74</guid><dc:creator>Simonr</dc:creator><description>&lt;p&gt;Hi&lt;/p&gt;
&lt;p&gt;How are you handling your disconnect events? The NRF_ERROR_INVALID_STATE from these functions means a disconnect is already in progress. You&amp;#39;ll have to handle the events in the application code. Most of our ble_peripheral examples does this. Please check out the BLE_GAP_EVT_DISCONNECTED in any of them. Also, did you take a look at the post I linked to in my previous reply?&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: Enabling and disabling Bluetooth</title><link>https://devzone.nordicsemi.com/thread/208354?ContentTypeID=1</link><pubDate>Thu, 05 Sep 2019 22:34:18 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:8445f6c6-24cc-42ed-a9c6-83c8d04a3483</guid><dc:creator>Charlie Tuer</dc:creator><description>&lt;p&gt;Hi Simon&lt;/p&gt;
&lt;p&gt;Thank you for your help, I have managed to get&amp;nbsp;&lt;span&gt;sd_ble_gap_adv_stop(); to work when no connection has been established and am able to then toggle between advertising and not. However, I still cannot turn advertising off when a connection is established. &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Simply executing sd_ble_gap_adv_stop(); from the connected state has resulted in a NRF_ERROR_INVALID_STATE. I have tried removing the APP_ERROR_CHECK(err_code); but the connection is still not broken (possible that I am oversimplifying this and removing&amp;nbsp;APP_ERROR_CHECK is not the same as ignoring the error code).&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Now, I am attempting to break the connection using&amp;nbsp;sd_ble_gap_disconnect as bellow, where the function on_disconnect(); is used to update several parameters to match the new state.&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;pre class="ui-code" data-mode="text"&gt;/**@brief Function for handling the Disconnect event.
 *
 * @param[in]   p_cus       Custom Service structure.
 * @param[in]   p_ble_evt   Event received from the BLE stack.
 */
void on_disconnect(ble_cus_t * p_cus, ble_evt_t const * p_ble_evt)
{
    UNUSED_PARAMETER(p_ble_evt);
    p_cus-&amp;gt;conn_handle = BLE_CONN_HANDLE_INVALID;
    
    ble_cus_evt_t evt;

    evt.evt_type = BLE_CUS_EVT_DISCONNECTED;

    p_cus-&amp;gt;evt_handler(p_cus, &amp;amp;evt);
}

/**@brief Function for stoping advertising.
 */
void advertising_stop(void)
{
    ret_code_t err_code;

    err_code = sd_ble_gap_adv_stop(m_advertising.adv_handle);
    APP_ERROR_CHECK(err_code);
}

/**@brief Function for disconnecting device.
 */
void advertising_disconnect()
{
    ret_code_t err_code;
    
    // If connected, disconnect
    if (m_conn_handle != BLE_CONN_HANDLE_INVALID)
    {
        err_code = sd_ble_gap_disconnect(m_conn_handle,
                                         BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);
        APP_ERROR_CHECK(err_code);
    }

    ble_evt_t const * p_ble_evt;
    void * p_context;

    ble_cus_t * p_cus = (ble_cus_t *) p_context;

    on_disconnect(p_cus, p_ble_evt);
}&lt;/pre&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;The order these functions are called is: connection is established --&amp;gt; advertising_disconnect(); --&amp;gt; advertising_stop();&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Now with the code above, I am getting the error SOFTDEVICE:&amp;nbsp;INVALID MEMORY ACCESS.&amp;nbsp;&lt;br /&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;When on_disconnect(); is commented out, an NRF_ERROR_INVALID_STATE is once again generated (also doesn&amp;#39;t work when the error check is removed).&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;I believe the issue may be from the&amp;nbsp;sd_ble_gap_disconnect(); function&amp;nbsp;as the evt_id appears to be different after every time I call it. Is there another way of severing the connection or just a general better way of handling this?&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Thanks,&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Charlie&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Enabling and disabling Bluetooth</title><link>https://devzone.nordicsemi.com/thread/207373?ContentTypeID=1</link><pubDate>Mon, 02 Sep 2019 11:29:05 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:2ea5f11d-35a1-4f63-8ff3-a658b5f1c27c</guid><dc:creator>Simonr</dc:creator><description>&lt;p&gt;Hi Charlie&lt;/p&gt;
&lt;p&gt;&lt;span&gt;You should be able to call&amp;nbsp;&lt;/span&gt;&lt;code&gt;ble_advertising_start(BLE_ADV_MODE_IDLE);&lt;/code&gt;&lt;span&gt;&amp;nbsp;and&amp;nbsp;&lt;/span&gt;&lt;code&gt;sd_ble_gap_adv_stop();&lt;/code&gt;&lt;span&gt;&amp;nbsp;to stop advertising. You can also check out &lt;a href="https://devzone.nordicsemi.com/f/nordic-q-a/48461/restart-advertisement-timeout-before-its-actual-timeout-restart-whenever-there-is-a-button-press"&gt;this example, where someone is restarting advertising with a button press&lt;/a&gt;. That should be rather similar to what you&amp;#39;re trying to do.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Best regards,&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Simon&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Enabling and disabling Bluetooth</title><link>https://devzone.nordicsemi.com/thread/207237?ContentTypeID=1</link><pubDate>Sat, 31 Aug 2019 15:00:53 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:8e631c7e-bdce-4448-9264-e2e66958db4d</guid><dc:creator>TomWS</dc:creator><description>&lt;p&gt;I&amp;#39;m away from my main system right now, but I believe the proper way to stop advertising is:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;ret_code_t err_code = ble_advertising_start(&amp;amp;m_advertising, BLE_ADV_MODE_IDLE);&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>