<?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>Peripheral fails to re-start advertising after disconnecting from Client</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/100588/peripheral-fails-to-re-start-advertising-after-disconnecting-from-client</link><description>I&amp;#39;m having some issues with my Peripheral failing to restart advertising after its disconnected from the Client. 
 In my main loop, I am looking at a bunch of flags to decide what to do next, and part of that is to check one that tracks the BLE status</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Wed, 12 Jul 2023 06:08:24 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/100588/peripheral-fails-to-re-start-advertising-after-disconnecting-from-client" /><item><title>RE: Peripheral fails to re-start advertising after disconnecting from Client</title><link>https://devzone.nordicsemi.com/thread/435849?ContentTypeID=1</link><pubDate>Wed, 12 Jul 2023 06:08:24 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:d20ea12f-93f5-4014-9822-47d30213c9be</guid><dc:creator>Mike Austin (LPI)</dc:creator><description>&lt;p&gt;Hi Joakim,&lt;/p&gt;
&lt;p&gt;Seems I spoke too soon.&amp;nbsp; I&amp;#39;m still having this issue.&lt;/p&gt;
&lt;p&gt;For whatever reason, when I have an iOS Client (such as the nRF Connect App) attempt to disconnect from my device, the connection remains valid and I cannot see it re-advertise.&lt;/p&gt;
&lt;p&gt;The only way I seem to be able to get the disconnection to work successfully is to actually have a Disconnect Service and Characteristic, and when the Client writes to the Characteristic, I call:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;// Function to update the value of disconnect_status when Client writes to it
static ssize_t write_disconnect_status(struct bt_conn *conn,
			       const struct bt_gatt_attr *attr, const void *buf,
			       uint16_t len, uint16_t offset, uint8_t flags)
{
	LOG_DBG(&amp;quot;INFO: Received value for disconnect_status: %d&amp;quot;, ((uint8_t *) buf)[0]);
	disconnect_status = ((uint8_t *) buf)[0];
	if (disconnect_status)
	{
		uint8_t rv = bt_conn_disconnect(conn, BT_HCI_ERR_REMOTE_USER_TERM_CONN);
		if (rv == 0) {
			LOG_DBG(&amp;quot;INFO: Peripheral successfully disconencted from Central&amp;quot;);
			disconnect_status = 0;
		}
		else {
			LOG_DBG(&amp;quot;ERROR: Peripheral failed to disconenct from Central&amp;quot;);
		}
	}
	return len;
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;This then successfully forces a disconnection of the current connection.&lt;/p&gt;
&lt;p&gt;Yet, if the iOS Client simply issues a disconnect request, the disconnect callback I have defined doesn&amp;#39;t appear to get called.&lt;/p&gt;
&lt;p&gt;My Peripheral is Paired and Bonded to the Client (I need this for getting the CTS).&amp;nbsp; Not sure if this is part of the problem?&lt;/p&gt;
&lt;p&gt;I recall seeing this issue about a year ago (refer to&amp;nbsp;&lt;a href="https://devzone.nordicsemi.com/f/nordic-q-a/86503/problems-with-nrfdk-not-disconnecting-from-iphone-running-nrf-connect-app"&gt;this&lt;/a&gt;&amp;nbsp;link) and was never really able to resolve it beyond have a specific Service/Characteristic that would enable me to force a disconnection.&lt;/p&gt;
&lt;p&gt;If I used an nRF52-DK as my Client, and have this interfaced to the nRF Connect for Desktop Bluetooth Low Energy Standalone App on my Windows machine, then when I press the &amp;quot;disconnect&amp;quot; button on that, it successfully disconnects from the Peripheral and the Peripheral will start to advertise again.&lt;/p&gt;
&lt;p&gt;Regards,&lt;/p&gt;
&lt;p&gt;Mike&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Peripheral fails to re-start advertising after disconnecting from Client</title><link>https://devzone.nordicsemi.com/thread/430320?ContentTypeID=1</link><pubDate>Sun, 11 Jun 2023 06:30:50 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:b60a00c8-39b4-49b7-bfd9-5db29ef27f51</guid><dc:creator>Mike Austin (LPI)</dc:creator><description>&lt;p&gt;Hi Joakim,&lt;/p&gt;
&lt;p&gt;I eventually got to the bottom of the issue.&amp;nbsp; I&amp;#39;d blindly copied some code from another example that was providing notifications on a characteristic, and in the connection callback for that example, they had the following line:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;current_conn = bt_conn_ref(conn);&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;The current_conn parameter was used to make changes to the MTU and a few other connection parameters.&amp;nbsp; But calling bt_conn_ref() I believe is incrementing the connection counter, so when I disconnected, I believe I was only disconnecting from one connection, or the connection counter was such that it was indicating there was still a valid connection.&amp;nbsp; So, when I went to update the advertisting data, it still was effectively connected, not advertising, hence the error messages.&lt;/p&gt;
&lt;p&gt;I can fix this issue in two ways:&lt;/p&gt;
&lt;p&gt;1. Add bt_conn_unref(current_con) in my disconnect callback&lt;/p&gt;
&lt;p&gt;2. Calling current_con = conn in my connection callback, and excluding the call to bt_conn_ref().&lt;/p&gt;
&lt;p&gt;I&amp;#39;m not exactly sure why you could use bt_conn_ref() and I&amp;#39;m not sure which of the above solutions is actually the correct (or if they both are, then better) option.&lt;/p&gt;
&lt;p&gt;Regards,&lt;/p&gt;
&lt;p&gt;Mike&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Peripheral fails to re-start advertising after disconnecting from Client</title><link>https://devzone.nordicsemi.com/thread/430134?ContentTypeID=1</link><pubDate>Fri, 09 Jun 2023 07:55:41 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:d3e73ff0-841f-4466-a7fb-0260b40ef5a9</guid><dc:creator>Joakim Jakobsen</dc:creator><description>&lt;p&gt;Hi Mike.&amp;nbsp;&lt;/p&gt;
[quote user=""]I would expect that once the Peripheral disconnects, it should automatically restart advertising.[/quote]
&lt;p&gt;This is partially true. Most of the samples implements this functionality, because in most use cases it makes sense to start advertising once the device disconnects.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;However, this&amp;nbsp;is controlled by the application. So after your device disconnects to have to make sure that &lt;span&gt;bt_le_adv_start() is called.&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;You can use the&amp;nbsp;function bt_le_adv_update_data() if your advertising is already started.&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Br,&amp;nbsp;&lt;br /&gt;Joakim&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>