title: How to manage a Bluetooth connection on Android through configuration changes?
body: <p><strong>Question:</strong><br>
  How can I manage (Connect, Read, Write, Disconnect) a bluetooth connection that persists through configuration changes?</p>
  
  <p>Prefer solutions that are compatible with device version 2.2 "Froyo" using <a href="http://actionbarsherlock.com" rel="nofollow noreferrer">ActionBarSherlock</a>.</p>
  
  <p><strong>Problems...</strong>  </p>
  
  <ul>
  <li><p>Neither <code>BluetoothDevice</code> nor <code>BluetoothSocket</code> can be retained in <code>onSaveState</code>.  </p></li>
  <li><p>In order to <a href="http://developer.android.com/training/articles/perf-anr.html" rel="nofollow noreferrer">keep my app responsive</a>, the  12 second blocking call <a href="http://developer.android.com/reference/android/bluetooth/BluetoothSocket.html#connect()" rel="nofollow noreferrer"><code>BluetoothSocket.connect()</code></a> must be made on a separate thread. Starting a <code>Runnable</code> is the recommended way to thread long tasks, but it's a nightmare trying to recover on a configuration change. The official docs point to three different solutions. </p>
  
  <ul>
  <li><p>Use <a href="http://developer.android.com/reference/android/app/Activity.html#getLastNonConfigurationInstance()" rel="nofollow noreferrer"><code>getLastNonConfigurationInstance()</code></a>, which is deprecated (seriously?!). </p></li>
  <li><p>Set <code>android:configChanges="keyboardHidden|orientation"</code> like the <a href="http://developer.android.com/tools/samples/index.html" rel="nofollow noreferrer">BluetoothChat Sample</a>. However, this does not account for all types of configuration changes.</p></li>
  <li><p>Cancel &amp; restart tasks like the <a href="http://code.google.com/p/shelves/" rel="nofollow noreferrer">Shelves Example</a>.  In this case, this could potentially waste another 12 seconds.</p></li>
  </ul></li>
  </ul>
  
  <p><strong>Update 1</strong></p>
  
  <ul>
  <li><p>Further research led me to <a href="http://developer.android.com/reference/android/content/AsyncTaskLoader.html" rel="nofollow noreferrer">asyncTaskLoader</a>, but it seems like this can only update the UI on completion, and cannot provide updates.  </p></li>
  <li><p>The BluetoothHDP sample uses a service.  Services seem focused on inter-process communication and the need to persist beyond the activity life-cycle.  I don't need either of these features.  </p></li>
  </ul>
  
  <p><strong>Update 2</strong></p>
  
  <p><a href="https://stackoverflow.com/a/15731702/884677">As pointed out by Reuben</a>, <a href="http://developer.android.com/reference/android/app/Fragment.html#setRetainInstance(boolean)" rel="nofollow noreferrer"><code>Fragment.setRetainInstance(bool)</code></a> has replaced the deprecated <code>getLastNonConfigurationInstance()</code>. At this point, it seems like the best option is to make a persistent non-UI fragment using <code>setRetainInstance(true)</code>.  </p>