<?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>Android disconnect() and close() do not disconnect</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/38422/android-disconnect-and-close-do-not-disconnect</link><description>I have problems with disconnecting my Android devices from my BLE peripheral. I don&amp;#39;t think this is Nordic-specific, but perhaps someone has an idea? 
 Disconnecting using the nRF Connect app is 100% successful. The BLE peripheral sees the remote disconnect</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Tue, 11 Sep 2018 15:27:42 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/38422/android-disconnect-and-close-do-not-disconnect" /><item><title>RE: Android disconnect() and close() do not disconnect</title><link>https://devzone.nordicsemi.com/thread/148263?ContentTypeID=1</link><pubDate>Tue, 11 Sep 2018 15:27:42 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:839c4fda-cac3-46a1-8e37-01e9bfdf6389</guid><dc:creator>Acutetech</dc:creator><description>&lt;p&gt;Thanks so much for this, Aleksander. It is a simple explanation when you know it, but not at all obvious!!!&lt;/p&gt;
&lt;p&gt;Can I suggest you document in as many places as you can! Including various places in the source code.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Android disconnect() and close() do not disconnect</title><link>https://devzone.nordicsemi.com/thread/148235?ContentTypeID=1</link><pubDate>Tue, 11 Sep 2018 14:29:04 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:696cb550-dae7-47fc-81ce-bbff7a1d514c</guid><dc:creator>Aleksander Nowakowski</dc:creator><description>&lt;p&gt;Hello&amp;nbsp;&lt;a href="https://devzone.nordicsemi.com/members/acutetech"&gt;Acutetech&lt;/a&gt;!&lt;/p&gt;
&lt;p&gt;On Android (and iOS as well), it is the OS that manages the physical Bluetooth connections. Many apps may be (almost) independently connected to the same device. Only when the last of them disconnects, the OS closes the physical connection. There is no way* for an app to know if there is any other app that is using this connection, or not. In fact, a single app may open virtual connection to the same device multiple times, just by calling `device.connectGatt(...)` with different callbacks. From the BluetoothGatt object&amp;#39;s perspective, calling disconnect() closes the connection and good bye. But it does not mean that the peripheral was in fact disconnected.&lt;/p&gt;
&lt;p&gt;A second after calling disconnect() the OS checks if there are any other virtual connections open. If so, it just keeps the connection open, as if nothing happened. If no more apps, it disconnects from the peripheral.&lt;/p&gt;
&lt;p&gt;When you are using nRF Connect you don&amp;#39;t have nRF Connect running in background :) nRF Connect, when the Settings -&amp;gt; Connectivity -&amp;gt; Show incoming connections switch is enabled (default) will connect to the device in background and, until Android 8.0.0 will keep a strong reference to the connection, that means that even if your app has disconnected, it will still be connected. Just exit nRF Connect with Back button, instead of switching to another task, to avoid this problem. From Android 8.1.0 nRF Connect tries to use hidden connectGatt method with opportunistic parameter set to true, so the connection is weak. Weak connection, from OS perspective, is no connection, so physical link may be closed when another app disconnects.&lt;/p&gt;
&lt;p&gt;To make sure that your device disconnects, you have to send some kind of Reset command that will send the disconnect command from the peripheral. Android will honor this and will notify all BluetoothGattCallbacks that the device has disconnected. This is for example what we do in DFU when we switch to Bootloader mode.&lt;/p&gt;
&lt;p&gt;Try disconnection without nRF Connect runnign in background, this should work.&lt;/p&gt;
&lt;p&gt;BR, Aleksander&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>