title: Adapting Android BluetoothChat example to single server, multiple clients
body: <p>I'm attempting to adapt the BluetoothChat example that comes with the SDK to a single-server, multiple-client configuration. I'm having issues and hope someone can help.</p>
  
  <p>I've created two distinct apps, a server and a client, rather than have a single app function as both server and client, as in the example. So the upon starting the app, the server creates a bluetooth service, which creates a new AcceptThread, which listens for incoming connections. I made a couple of modifications to the example code so that the AcceptThread continues to listen, even after the first connection is made (the example stops listening after the one connection is made, because it only needs to establish connections between two devices). I created an ArrayList to handle the multiple connections. Each time a new connection is attempted from a remote device, a new ConnectedThread is created and added to the list.</p>
  
  <p>The first client connects just fine.</p>
  
  <p>When the second client attempts to connect, the connection fails. The log output is:</p>
  
  <pre><code>D/BluetoothEventLoop(  140): Device property changed: &lt;device address&gt; property: Connected value: true
  I/BluetoothService(  140): ACL connected, mAclLinkCount = 2
  D/BluetoothEventLoop(  140): Device property changed: &lt;device address&gt; property: Connected value: false
  I/BluetoothService(  140): ACL disconnected, mAclLinkCount = 1
  </code></pre>
  
  <p>So it looks like the second connection attempt is successful, but then it is immediately disconnected. This looks like it's happening at a low level, outside anything the code is handling. This happens with a number of different types of test devices, so I don't think it's due to a particular firmware or hardware issue. </p>
  
  <p>Can anyone help with this?</p>
  
  <p>Also, can anyone point to an example of a server->multiple client connection example using bluetooth with Android? I came across an Air Hockey example:</p>
  
  <p><a href="http://code.google.com/p/apps-for-android/source/browse/#svn%2Ftrunk%2FBTClickLinkCompete%2Fsrc%2Fnet%2Fclc%2Fbt" rel="nofollow">http://code.google.com/p/apps-for-android/source/browse/#svn%2Ftrunk%2FBTClickLinkCompete%2Fsrc%2Fnet%2Fclc%2Fbt</a></p>
  
  <p>But this project is very glitchy on my test devices and the actual game won't display properly. Does anyone know if there is an example of BluetoothChat extended to handle multiple connections? </p>
  
  <p>UPDATE:</p>
  
  <p>In attempting to solve this issue, I believe I have successfully extended the official Android Bluetooth Chat example to work with a server connecting to multiple clients.</p>
  
  <p>I've uploaded my modifications to github <a href="https://github.com/polyclef/BluetoothChatMulti" rel="nofollow">here</a>.</p>
  
  <p>One device acts as the server by selecting "Menu>Make discoverable". Each client then connects by selecting "Menu>Connect a device". I was successfully able to connect my Motorola Droid and HTC Eris as clients to my Samsung Galaxy Tab 10.1". Each message sent from a client displays only on the server. A message sent from the server displays on all clients.</p>
  
  <p>This was a tedious process getting it to work at all. Connectivity with the HTC devices is sporadic, though. I've read elsewhere that bluetooth on HTC is buggy, so I'm chalking it up to that.</p>
  
  <p>Hopefully someone else will find this example useful. If you have any questions, suggestions, or other feedback, please let me know.</p>