<?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>Check if currently advertising</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/14071/check-if-currently-advertising</link><description>In my application, a host co-processor controls when to start/stop advertising, connect etc.
 I&amp;#39;ve noticed that soft-device command are state-dependent, e.g. calling ble_advertising_start() while advertising, or calling sd_ble_gap_adv_stop() while not</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Sat, 14 Nov 2020 11:12:17 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/14071/check-if-currently-advertising" /><item><title>RE: Check if currently advertising</title><link>https://devzone.nordicsemi.com/thread/280001?ContentTypeID=1</link><pubDate>Sat, 14 Nov 2020 11:12:17 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:70f38824-7697-4662-85ce-e6bfc412f8b5</guid><dc:creator>Chandi</dc:creator><description>&lt;p&gt;As a simplified method, at the first advertising instance, I would start a one-shot timer with a timeout value equal to advertising duration (APP_ADV_DURATION) and check if timer expired before issuing subsequent&amp;nbsp;&lt;span&gt;ble_advertising_start(). If timer not expired,&amp;nbsp;sd_ble_gap_adv_stop() can be issued.&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Check if currently advertising</title><link>https://devzone.nordicsemi.com/thread/254791?ContentTypeID=1</link><pubDate>Fri, 12 Jun 2020 16:33:55 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:b8d7857b-fea7-484b-8a11-e9602b364ae5</guid><dc:creator>Oleg Z</dc:creator><description>&lt;p&gt;4. BLE_GAP_EVT_ADV_SET_TERMINATED is fired. Then advertising is stopped. You can also be more specific and take different actions regarding the reason for the event (p_ble_evt-&amp;gt;evt.gap_evt.params.adv_set_terminated.reason)&lt;/p&gt;
&lt;p&gt;for some odd reason&amp;nbsp;&lt;span&gt;BLE_GAP_EVT_TIMEOUT is never fired for me on the advertising timeout.&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Check if currently advertising</title><link>https://devzone.nordicsemi.com/thread/53797?ContentTypeID=1</link><pubDate>Thu, 26 May 2016 20:11:40 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:ad518d39-a56c-4f05-9cd8-a1971a5abb15</guid><dc:creator>Ulrich Myhre</dc:creator><description>&lt;p&gt;If the advertising stops, and you have not yet pulled the event stating this, then I think you should be safe. I&amp;#39;m not 100% sure on the internals, but as far as I know, all states are &amp;quot;pending&amp;quot; until you pull them with &lt;code&gt;sd_ble_evt_get()&lt;/code&gt;. From then on, the stack assumes that you have dealt with the consequences of the event.&lt;/p&gt;
&lt;p&gt;I&amp;#39;m not well-wersed in the current SDK, so someone should correct me - but I think the correct way is to simply make an&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;if ((err_code != NRF_SUCCESS) &amp;amp;&amp;amp; (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><item><title>RE: Check if currently advertising</title><link>https://devzone.nordicsemi.com/thread/53796?ContentTypeID=1</link><pubDate>Wed, 25 May 2016 14:31:24 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:b7609555-ffe9-486a-88c6-9bf6554ee2d8</guid><dc:creator>Boaz</dc:creator><description>&lt;p&gt;Thanks. Am I right to say that first option (using a volatile the flag) does not solve the race condition?
Come to think of it, you will &lt;strong&gt;always&lt;/strong&gt; have race conditions in a communication protocol, e.g. you call disconnect() at the exact same moment as your peer disconnects. The device must not reset in these situations.&lt;/p&gt;
&lt;p&gt;I&amp;#39;m opting for the second option, and be tolerant to &amp;#39;Invalid State&amp;#39; errors, hence:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Do you see any problems with this approach I might have missed?&lt;/li&gt;
&lt;li&gt;What is the correct way to ignore the &lt;code&gt;NRF_ERROR_INVALID_STATE&lt;/code&gt; code? Should I override &lt;code&gt;app_error_fault_handler()&lt;/code&gt;?&lt;/li&gt;
&lt;/ul&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Check if currently advertising</title><link>https://devzone.nordicsemi.com/thread/53795?ContentTypeID=1</link><pubDate>Wed, 25 May 2016 14:08:03 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:d01956ac-f598-4f23-b605-5fe0d9e79331</guid><dc:creator>Ulrich Myhre</dc:creator><description>&lt;p&gt;The safe way is to create a (possibly volatile) flag like &lt;code&gt;is_advertising&lt;/code&gt;, which is set to true whenever &lt;code&gt;adv_start()&lt;/code&gt; returns &lt;code&gt;NRF_SUCCESS&lt;/code&gt;, and set to false when any of the three conditions above are hit. Never call &lt;code&gt;adv_stop()&lt;/code&gt; unless the flag is true, and &lt;code&gt;adv_start()&lt;/code&gt; unless it is false. The easier way is to simply handle or ignore the &lt;code&gt;NRF_ERROR_INVALID_STATE&lt;/code&gt; return code, accepting that you do not really keep track of the state yourself.&lt;/p&gt;
&lt;p&gt;What I meant about race conditions is in the hypothetical case where you had a function to retrieve state. Then you could look at the state, see that it is advertising, then call adv_stop(). If advertising stops by itself between the two last steps, you would have the same problem as now. There are no such problems in the current implementation, unless you are changing your own application states in higher interrupt levels.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Check if currently advertising</title><link>https://devzone.nordicsemi.com/thread/53798?ContentTypeID=1</link><pubDate>Wed, 25 May 2016 13:07:23 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:5490672a-caa6-4062-8f6d-f3720a39e430</guid><dc:creator>Boaz</dc:creator><description>&lt;p&gt;Thanks Ulrich. Some thoughts:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Can you please suggest a safe way to stop advertising in case: 1) Advertising timeout is finite 2) Advertising is forever?&lt;/li&gt;
&lt;li&gt;You mentioned race conditions. Assuming I only call SD methods from the main thread, will using the application scheduler and &lt;code&gt;SOFTDEVICE_HANDLER_APPSH_INIT&lt;/code&gt; mitigate those?&lt;/li&gt;
&lt;/ul&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Check if currently advertising</title><link>https://devzone.nordicsemi.com/thread/53794?ContentTypeID=1</link><pubDate>Wed, 25 May 2016 11:52:28 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:8328d504-b724-44f0-860f-d4b6df57eb62</guid><dc:creator>Ulrich Myhre</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;First I want to mention that it is not the SD that asserts. It simply returns an error code. The APP_ERROR_CHECK macro is the one that will reset the device, unless you choose to handle it in a different way. You can call any API functions with any parameter, at any time, and as long as you do not lie about the length of any buffers you provide, the SD shall never assert or crash. The functions will return an error code stating what happened.&lt;/p&gt;
&lt;p&gt;And for your question, you are right in your assumption that you have to remember the state in your application. If &lt;code&gt;sd_ble_gap_adv_start()&lt;/code&gt; returns &lt;code&gt;NRF_SUCCESS&lt;/code&gt;, the device will advertise until&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;BLE_GAP_EVT_CONNECTED&lt;/code&gt; event is received, i.e. a connection is made (in the case of connectable advertisement). If you are trying to use the central to connect at the same time, you need to check the role flag of the connection. If &lt;code&gt;BLE_GAP_ROLE_PERIPH&lt;/code&gt;, you are no longer advertising.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;BLE_GAP_EVT_TIMEOUT&lt;/code&gt; event is received (in the case where you set a timeout, or use high duty-cycle directed advertisements) and the src field is &lt;code&gt;BLE_GAP_TIMEOUT_SRC_ADVERTISING&lt;/code&gt;.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;sd_ble_gap_adv_stop()&lt;/code&gt; has been called.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;There are no commands to check the current state, and it would be prone to race conditions if we had one. Instead, our stack relies on application events to tell you when the internal state changes.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>