<?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>Zephyr BLE connection management</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/95881/zephyr-ble-connection-management</link><description>Hi, I have a bluetooth central app which connects to several BLE peripherals and am now working on fixing various connection reliability issues. White lists and bonding are bring used and I am forcing connection loss via use of faraday bags. I have questions</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Thu, 26 Jan 2023 16:36:56 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/95881/zephyr-ble-connection-management" /><item><title>RE: Zephyr BLE connection management</title><link>https://devzone.nordicsemi.com/thread/406731?ContentTypeID=1</link><pubDate>Thu, 26 Jan 2023 16:36:56 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:8dff5879-f656-4cfc-9ad5-fe80443ac152</guid><dc:creator>Amanda Hsieh</dc:creator><description>&lt;p&gt;Hi,&amp;nbsp;&lt;/p&gt;
&lt;p&gt;There are some APIs that are documented as giving you a reference. And the application needs to unref this when they don&amp;#39;t use it anymore &lt;span&gt;like&amp;nbsp;&lt;/span&gt;&lt;a href="https://github.com/nrfconnect/sdk-zephyr/blob/d9f99ed1951a973c691cd5d6bd70aecb597b2030/include/zephyr/bluetooth/conn.h#L656-L679"&gt;this&lt;/a&gt;&lt;span&gt;,&amp;nbsp;&lt;/span&gt;&lt;a href="https://github.com/nrfconnect/sdk-zephyr/blob/d9f99ed1951a973c691cd5d6bd70aecb597b2030/include/zephyr/bluetooth/conn.h#L1510-L1519"&gt;this&lt;span&gt;&amp;nbsp;&lt;/span&gt;&lt;/a&gt;&lt;span&gt;and&amp;nbsp;&lt;/span&gt;&lt;a href="https://github.com/nrfconnect/sdk-zephyr/blob/d9f99ed1951a973c691cd5d6bd70aecb597b2030/include/zephyr/bluetooth/audio/has.h#L193-L201"&gt;this&lt;/a&gt;.&lt;/p&gt;
&lt;div&gt;
&lt;pre&gt; *  The caller gets a new reference to the connection object which must be
 *  released with bt_conn_unref() once done using the object.&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&lt;/p&gt;
&lt;div&gt;Neither the connected-callback invocation nor the disconnected-callback invocation comes with an ownership transfer of a bt_conn reference. (When a API does, it has to be documented in the API doc.) Any call to bt_conn_unref in disconnected() presumably gets rid of ownership acquired earlier from some other source.&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;
[quote user="owain"]See here attached a trace where bt_conn_unref() is called on disconnect(); this results in an assertion that related to refrence counters.[/quote]
&lt;p&gt;Do you use&amp;nbsp;&lt;span&gt;bt_conn_ref or the function would increase the counter? If not, then it doesn&amp;#39;t need to call bt_conn_unref. I would correct my previous answer.&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;-Amanda H.&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Zephyr BLE connection management</title><link>https://devzone.nordicsemi.com/thread/406016?ContentTypeID=1</link><pubDate>Mon, 23 Jan 2023 11:58:50 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:190fa1be-318c-47a4-b894-6a2f95e5bcdd</guid><dc:creator>OwainIncus</dc:creator><description>&lt;p&gt;In fact if I search through all sample code; one sees most examples using bt_conn_unref(); but only some that have calls to bt_conn_ref().&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Zephyr BLE connection management</title><link>https://devzone.nordicsemi.com/thread/406000?ContentTypeID=1</link><pubDate>Mon, 23 Jan 2023 10:37:26 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:0651e0e5-1637-430c-82d7-0830d015318a</guid><dc:creator>OwainIncus</dc:creator><description>&lt;p&gt;Hi Amanda,&lt;/p&gt;
&lt;p&gt;So in the connected() callback we are given the reference to the connection (A reference counter has been incremented in the stack somewhere?).&lt;/p&gt;
&lt;p&gt;&lt;br /&gt;And in the disconnected() callback I must explicitly call bt_conn_unref()? this is not very symmetrical. Counter incremented inside the stack but decremented outside the stack. Where is that reference counter incremented?&lt;br /&gt;&lt;br /&gt;Its funny because at the moment the bt_conn_unref() is commented out in my disconnect routine before disconnect routine calls bt_conn_le_create_auto() and the device is merrily disconnecting/reconnecting as peripheral RF connection is blocked/freed using a faraday bag.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;See here attached a trace where bt_conn_unref() is called on disconnect(); this results in an assertion that related to refrence counters.&lt;br /&gt;&lt;br /&gt;Regards,&lt;/p&gt;
&lt;p&gt;Owain&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;a href="https://devzone.nordicsemi.com/cfs-file/__key/communityserver-discussions-components-files/4/blePeripheralRefCounter.txt"&gt;devzone.nordicsemi.com/.../blePeripheralRefCounter.txt&lt;/a&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Zephyr BLE connection management</title><link>https://devzone.nordicsemi.com/thread/405900?ContentTypeID=1</link><pubDate>Fri, 20 Jan 2023 17:35:51 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:dca88c5d-2552-4bea-8716-7f2c7ad39281</guid><dc:creator>Amanda Hsieh</dc:creator><description>&lt;p&gt;Hi,&amp;nbsp;&lt;/p&gt;
&lt;p&gt;1. The flow looks correct. Security and s&lt;span&gt;ervice discovery&lt;/span&gt;&lt;span&gt;&amp;nbsp;should be executed on each connection.&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;2. &lt;a href="https://developer.nordicsemi.com/nRF_Connect_SDK/doc/latest/zephyr/connectivity/bluetooth/api/connection_mgmt.html#c.bt_conn_unref"&gt;bt_conn_unref&lt;/a&gt;&amp;nbsp;would decrement a connection’s reference count. It is used when the connection counter involved the application.&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Regards,&lt;br /&gt;Amanda H.&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Zephyr BLE connection management</title><link>https://devzone.nordicsemi.com/thread/405894?ContentTypeID=1</link><pubDate>Fri, 20 Jan 2023 16:18:45 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:c2b35523-86c8-4f7e-85d7-b44dcae261e2</guid><dc:creator>OwainIncus</dc:creator><description>&lt;p&gt;This looks to be to do with the connection reference counters.&lt;br /&gt;If I remove the bt_conn_unref() from the disconnect() callback and just re&amp;#39;arm the wit for incoming connection with bt_conn_le_create_auto(); then I can merrily loose and regain the sensor at random and the connection is reconnected after each period of disconnect.&lt;br /&gt;&lt;br /&gt;So only if it is a deliberately disconnect a peripherl should one do a bt_conn_unref()?&lt;br /&gt;&lt;br /&gt;This may be answer to one of my initial questions.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Zephyr BLE connection management</title><link>https://devzone.nordicsemi.com/thread/405810?ContentTypeID=1</link><pubDate>Fri, 20 Jan 2023 10:59:59 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:01c93587-9b86-4802-a277-a44dd029445f</guid><dc:creator>OwainIncus</dc:creator><description>&lt;p&gt;Hi Hung,&lt;br /&gt;&lt;br /&gt;Any hints here?&lt;br /&gt;Unfortunately samples don&amp;#39;t go into this detail.&lt;br /&gt;&lt;br /&gt;Regards,&lt;br /&gt;Owain&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>