title: Android Bluetooth Socket
body: <p>What I am trying to do:
  I am working on an application that will fetch data from biometric device using Bluetooth using SPP. For that I have some command (in byte and HEX format) and steps provided by biometric device manufacturer.</p>
  
  <p>My Problem :
  I have to follow 2 steps for getting the data i.e. send two commands one after another. The biometric device return data when receive second command. I have successfully establish connection with device and execute first command but when I send second command to device the app get stuck and after some time biometric device terminate connection and I receive IO Exception : Socket closed, don't understand what is happening on second command.</p>
  
  <p>I have done:
  I have used BluetoothChatService.class from <a href="http://developer.android.com/tools/samples/index.html" rel="nofollow">Bluetooth Chat</a> example provided in Android SDK Samples and modify <a href="http://developer.android.com/guide/topics/connectivity/bluetooth.html" rel="nofollow">ConnectThread</a> thread's Run function according to my requirement i.e. I am sending commands in this function. And used "00001101-0000-1000-8000-00805F9B34FB" UUID. For connecting to socket I have tried following codes :</p>
  
  <pre><code>if (Build.VERSION.SDK_INT &gt; 9) {
                  tmp = device.createInsecureRfcommSocketToServiceRecord(MY_UUID);
              }
              else {
                  tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
              }
  </code></pre>
  
  <p>And </p>
  
  <pre><code>Method m = device.getClass().getMethod("createRfcommSocket", 
                      new Class[] { int.class });
              tmp = (BluetoothSocket) m.invoke(device, Integer.valueOf(1));
  </code></pre>
  
  <p>And </p>
  
  <pre><code>tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
  </code></pre>
  
  <p>Any one know where I am wrong?</p>
  
  <p>Thank you in advance. 
  but one of these can solve my problem.</p>
  
  <p><strong>Edit</strong>
  I have resolve this problem. Here is method who I am establishing socket connection </p>
  
  <pre><code>if (Build.VERSION.SDK_INT &lt; 10){
  Method m = device.getClass().getMethod("createRfcommSocket",new Class[] { int.class });
  tmp = (BluetoothSocket) m.invoke(device, Integer.valueOf(1));
  </code></pre>
  
  <p>}</p>
  
  <blockquote>
    <p>else{
        tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
    }</p>
  </blockquote>