title: Connecting Bluetooth Socket While Device Discovery is On
body: <p>I am using bluetooth socket connection in my andorid app to connect to medical devices in near range.I want to connect the bluetooth socket by clicking on a particuar device on the ListView,while the bluetooth discovery is not finished yet.
  I am using the follwoing code to connect the bluetooth socket to connect to a device found in the bluetooth range.</p>
  
  <pre><code>BluetoothSocket mBluetoothSocket;
  
  mBluetoothSocket = mBluetoothDevice.createRfcommSocketToServiceRecord(UUID
                                  .fromString("00001101-0000-1000-8000-00805F9B34FB"));
  
                          if (mBluetoothSocket != null) {
                              // Device does not support Bluetooth
                              Log.w("MyDebugMsg", "Bluetooth Socket present");
                              mBluetoothAdapter.cancelDiscovery();
                              mDialog2 = ProgressDialog.show(HL7MedicalAppActivity.this,
                                      "", "Connecting to device, Please wait...",
                                      true);
  
                              new Thread(new Runnable() {
                                  public void run() {
                                      try {
                                          mBluetoothSocket.connect();
  
                                      } catch (IOException e) {
                                          runOnUiThread(new Runnable() {
                                              public void run() {
                                                  AlertDialog.Builder alertDialog = new AlertDialog.Builder(
                                                          HL7MedicalAppActivity.this);
                                                  // Setting Dialog Title
                                                  alertDialog.setTitle("Error");
                                                  // Setting Dialog Message
                                                  alertDialog
                                                          .setMessage("Connection refused by remote device");
                                                  // Setting OK Button
                                                  alertDialog
                                                          .setPositiveButton(
                                                                  "OK",
                                                                  new DialogInterface.OnClickListener() {
                                                                      public void onClick(
                                                                              DialogInterface dialog,
                                                                              int id) {
                                                                          HL7MedicalAppActivity.this
                                                                                  .finish();
                                                                      }
                                                                  });
  
                                                  // Showing Alert Message
                                                  alertDialog.show();
                                              }
                                          });
                                          // TODO Auto-generated catch block
                                          e.printStackTrace();
                                      }
  </code></pre>
  
  <p>But if I try to connect before device discovery is finished it is showing me follwing Bluetooth Socket Connect error</p>
  
  <pre><code>04-09 19:03:32.720: W/MyDebugMsg(5463): Bluetooth Socket present
  04-09 19:03:32.730: I/BluetoothAdapterProperties(2879):    
  Callback:discoveryStateChangeCallback with state:0
  04-09 19:03:32.730: I/BluetoothAdapterProperties(2879):   
  Callback:discoveryStateChangeCallback with state:0
  04-09 19:03:32.740: V/BluetoothDiscoveryReceiver(2847): Received:   
  android.bluetooth.adapter.action.DISCOVERY_FINISHED
   04-09 19:03:32.740: D/audio_hw_primary(177): select_devices: out_snd_device(2:     
   speaker) in_snd_device(0: )
   04-09 19:03:32.740: D/ACDB-LOADER(177): ACDB -&gt; send_afe_cal
   04-09 19:03:32.750: W/BluetoothAdapter(5463): getBluetoothService() called with no  
   BluetoothManagerCallback
   04-09 19:03:32.750: D/BTIF_SOCK(2879): service_uuid:     
   00001101-0000-1000-8000-00805f9b34fb
   04-09 19:03:32.750: E/bt-btif(2879): DISCOVERY_COMP_EVT slot id:12, failed to find  
   channle,                                       status:2, scn:-86134667
   04-09 19:03:32.750: W/bt-btif(2879): invalid rfc slot id: 12
  04-09 19:03:32.750: D/BluetoothSocket(5463): connect(), SocketState: INIT, mPfd:         {ParcelFileDescriptor: FileDescriptor[58]}
  04-09 19:03:32.750: W/System.err(5463): java.io.IOException: read failed, socket might 
  closed or timeout, read ret: -1
  04-09 19:03:32.750: W/System.err(5463):     at  
  android.bluetooth.BluetoothSocket.readAll(BluetoothSocket.java:505)
  04-09 19:03:32.750: W/System.err(5463):     at   
  android.bluetooth.BluetoothSocket.readInt(BluetoothSocket.java:516)
  04-09 19:03:32.750: W/System.err(5463):     at 
   android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:320)
  04-09 19:03:32.750: W/System.err(5463):     at        
  com.example.hl7medicalapp.HL7MedicalAppActivity$4$1.run(HL7MedicalAppActivity.java:389)
  04-09 19:03:32.750: W/System.err(5463):     at java.lang.Thread.run(Thread.java:841)
  </code></pre>
  
  <p>Can anyone suggest how to connect this BT socket successfully while device discovery is going on?</p>
  
  <p>Any help is highly appreciated.</p>