title: Not able to connect to bluetooth devices programmatically
body: <p>My aim is to connect to already paired bluetooth devices. I have got all the paired devices names and I am displaying them in a listview. When I click on any of the device name from the listview it should connect to that device if its within the range.</p>
  
  <p>Whenever I try to connect to the device by calling <code>mmSocket.connect()</code> I get this system error in my log cat shown below and then it calls <code>mmSocket.close()</code>:</p>
  
  <pre><code>06-22 17:40:50.617: W/System.err(25986): java.io.IOException: read failed, socket might closed or timeout, read ret: -1
  06-22 17:40:50.618: W/System.err(25986):    at android.bluetooth.BluetoothSocket.readAll(BluetoothSocket.java:510)
  06-22 17:40:50.619: W/System.err(25986):    at android.bluetooth.BluetoothSocket.readInt(BluetoothSocket.java:521)
  06-22 17:40:50.619: W/System.err(25986):    at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:320)
  06-22 17:40:50.619: W/System.err(25986):    at ik.learning.bluetooth.MainActivity$ConnectThread.run(MainActivity.java:233)
  </code></pre>
  
  <p>I am not sure why is this happening can some one help me out here. Below is my code:</p>
  
  <pre><code>public class MainActivity  extends Activity implements OnItemClickListener{
  
  ArrayAdapter&lt;String&gt; listAdapter;
  Button connectNew;
  ListView listView;
  BluetoothAdapter btAdapter;
  Set&lt;BluetoothDevice&gt; devicesArray;
  IntentFilter filter;
  BroadcastReceiver receiver;
  ArrayList&lt;String&gt; pairedDevices;
  ArrayList&lt;BluetoothDevice&gt; devices;
  public static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
  protected static final int SUCCESS_CONNECT = 0;
  protected static final int MESSAGE_READ = 1;
  Handler mHandler;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  
      init();
  
      if(btAdapter == null){
          Toast.makeText(this, "No bluetooth detected", Toast.LENGTH_LONG).show();
          finish();
      } else {
          if(!btAdapter.isEnabled()){
              turnOnBluetooth();
          }
  
          getPairedDevices();
          startDiscovery();
  
      }
  
      mHandler = new Handler(){
          @Override
          public void handleMessage(Message msg) {
              super.handleMessage(msg);
              switch(msg.what){
              case SUCCESS_CONNECT:
                  ConnectedThread connectedThread = new ConnectedThread((BluetoothSocket) msg.obj);
                  Toast.makeText(getApplicationContext(), "Connected", Toast.LENGTH_SHORT).show();
                  String s = "Successfully connected";
                  connectedThread.write(s.getBytes());
                  break;
              case MESSAGE_READ:  
                  byte[] readBuff = (byte[]) msg.obj;
                  String string = new String(readBuff);
                  Toast.makeText(getApplicationContext(), string, Toast.LENGTH_SHORT).show();
                  break;
              }
          }
      };
  
  }
  
  private void startDiscovery() {
      // TODO Auto-generated method stub
      btAdapter.cancelDiscovery();
      btAdapter.startDiscovery();
  }
  
  private void turnOnBluetooth() {
      // TODO Auto-generated method stub
      Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
      startActivityForResult(intent, 1);
  }
  
  private void getPairedDevices() {
      // TODO Auto-generated method stub
      devicesArray = btAdapter.getBondedDevices();
      if(devicesArray.size() &gt; 0){
          for(BluetoothDevice device : devicesArray){
              //listAdapter.add(device.getName()+"\n"+device.getAddress());
              pairedDevices.add(device.getName());//+"\n"+device.getAddress());
          }
      }
  }
  
  private void init() {
      connectNew = (Button) findViewById(R.id.btnConnectNew);
      listView = (ListView) findViewById(R.id.bluetoothlistView);
      listView.setOnItemClickListener(this);
      listAdapter = new ArrayAdapter&lt;String&gt;(this, android.R.layout.simple_expandable_list_item_1, 0);
      listView.setAdapter(listAdapter);
      btAdapter = BluetoothAdapter.getDefaultAdapter();
      pairedDevices = new ArrayList&lt;String&gt;();
      filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
      devices = new ArrayList&lt;BluetoothDevice&gt;();
  
      receiver = new BroadcastReceiver() {
  
          @Override
          public void onReceive(Context context, Intent intent) {
              // TODO Auto-generated method stub
              String action = intent.getAction();
  
              if(BluetoothDevice.ACTION_FOUND.equals(action)){
                  BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                  devices.add(device);
                  String s = "";
                  for(int i = 0; i&lt; pairedDevices.size(); i++){
                      if(device.getName().equals(pairedDevices.get(i))){
                          s = "PAIRED";
                          break;
                      }
                  }
                  listAdapter.add(device.getName()+" ("+s+") "+"\n"+device.getAddress());
              } 
  
              else if(BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)){
  
              }
  
              else if(BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)){
  
              }
  
              else if(BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)){
                  if(btAdapter.getState() == btAdapter.STATE_OFF){
                      turnOnBluetooth();
                  }
              }
  
          }
      };
  
      registerReceiver(receiver, filter);
  
      filter  = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
      registerReceiver(receiver, filter);
  
      filter  = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
      registerReceiver(receiver, filter);
  
      filter  = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
      registerReceiver(receiver, filter);
  }
  
  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
      // TODO Auto-generated method stub
      super.onActivityResult(requestCode, resultCode, data);
      if(resultCode == RESULT_CANCELED){
          Toast.makeText(getApplicationContext(), "Bluetooth must be enabled to continue", Toast.LENGTH_SHORT).show();
      }
  }
  
  @Override
  protected void onPause() {
      // TODO Auto-generated method stub
      super.onPause();
      unregisterReceiver(receiver);
  }
  
  @Override
  public void onItemClick(AdapterView&lt;?&gt; arg0, View arg1, int arg2, long arg3) {
      // TODO Auto-generated method stub
      if(btAdapter.isDiscovering()){
          btAdapter.cancelDiscovery();
      }
      if(listAdapter.getItem(arg2).contains("PAIRED")){
          //Object[] o = devicesArray.toArray();
          BluetoothDevice selectedDevice = devices.get(arg2); 
          ConnectThread connect = new ConnectThread(selectedDevice);
          connect.start();
          Toast.makeText(getApplicationContext(), "Devices Paired", Toast.LENGTH_SHORT).show();
      } else {
          Toast.makeText(getApplicationContext(), "Devices Not Paired", Toast.LENGTH_SHORT).show();
      }
  
  }
  
  
  private class ConnectThread extends Thread {
      private final BluetoothSocket mmSocket;
      private final BluetoothDevice mmDevice;
  
      public ConnectThread(BluetoothDevice device) {
          // Use a temporary object that is later assigned to mmSocket,
          // because mmSocket is final
          BluetoothSocket tmp = null;
          mmDevice = device;
  
          // Get a BluetoothSocket to connect with the given BluetoothDevice
          try {
              // MY_UUID is the app's UUID string, also used by the server code
              tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
          } catch (IOException e) { }
          mmSocket = tmp;
      }
  
      public void run() {
          // Cancel discovery because it will slow down the connection
          btAdapter.cancelDiscovery();
  
          try {
              // Connect the device through the socket. This will block
              // until it succeeds or throws an exception
              Log.d("ConnectThread","try connect");
              //mHandler.obtainMessage(SUCCESS_CONNECT, mmSocket).sendToTarget();
              mmSocket.connect();
  
          } catch (IOException connectException) {
              // Unable to connect; close the socket and get out
              connectException.printStackTrace();
              Log.d("ConnectThread","catch connect: "+connectException);
  
              try {
                  Log.d("ConnectThread","try soccet close");
                  mmSocket.close();
              } catch (IOException closeException) { 
                  Log.d("ConnectThread","catch close: "+connectException);
              }
              return;
          }
  
          // Do work to manage the connection (in a separate thread)
          Log.d("ConnectThread","before manage connected socket");
          //manageConnectedSocket(mmSocket);
          mHandler.obtainMessage(SUCCESS_CONNECT, mmSocket).sendToTarget();
  
      }
  
      /*private void manageConnectedSocket(BluetoothSocket mmSocket2) {
          // TODO Auto-generated method stub
  
      }*/
  
      /** Will cancel an in-progress connection, and close the socket */
      public void cancel() {
          try {
              mmSocket.close();
          } catch (IOException e) { }
      }
  }
  
  private class ConnectedThread extends Thread {
      private final BluetoothSocket mmSocket;
      private final InputStream mmInStream;
      private final OutputStream mmOutStream;
  
      public ConnectedThread(BluetoothSocket socket) {
          mmSocket = socket;
          InputStream tmpIn = null;
          OutputStream tmpOut = null;
  
          // Get the input and output streams, using temp objects because
          // member streams are final
          try {
              tmpIn = socket.getInputStream();
              tmpOut = socket.getOutputStream();
          } catch (IOException e) { }
  
          mmInStream = tmpIn;
          mmOutStream = tmpOut;
      }
  
      public void run() {
          byte[] buffer;  // buffer store for the stream
          int bytes; // bytes returned from read()
  
          // Keep listening to the InputStream until an exception occurs
          while (true) {
              try {
                  buffer = new byte[1024];  // buffer store for the stream
                  // Read from the InputStream
                  bytes = mmInStream.read(buffer);
                  // Send the obtained bytes to the UI activity
                  mHandler.obtainMessage(MESSAGE_READ, bytes, -1, buffer)
                          .sendToTarget();
  
              } catch (IOException e) {
                  break;
              }
          }
      }
  
      /* Call this from the main activity to send data to the remote device */
      public void write(byte[] bytes) {
          try {
              mmOutStream.write(bytes);
          } catch (IOException e) { }
      }
  
      /* Call this from the main activity to shutdown the connection */
      public void cancel() {
          try {
              mmSocket.close();
          } catch (IOException e) { }
      }
  }
  }
  </code></pre>