views:

22

answers:

0

I want to connect my Droid via bluetooth to OBDKey . I get Exception : connectionFailed .How can I solve this problem?

This is my part of code

private class ConnectThread extends Thread {
   private final BluetoothDevice mmDevice;
   public ConnectThread(BluetoothDevice device) {
      mmDevice = device;
      int RFCOMM_CHANEL = 1; //SPP chanel 
      Method m;
      try {
          m = mmDevice.getClass().getMethod("createRfcommSocket", new Class[] {  
              int.class });         
          mmSocket = (BluetoothSocket) m.invoke(mmDevice, RFCOMM_CHANEL); 
      } catch (Exception e) {
          e.printStackTrace();
      } 
   }

  public void run() {

     setName("ConnectThread");
     int countAtemptoConnect=0;
     boolean flag=false;
     if(IS_READING) {
        // cancel discovery because it will slow down a connection
        if(mAdapter!=null)
           mAdapter.cancelDiscovery();

        while(!flag){
           countAtemptoConnect++;
           // Make a connection to the BluetoothSocket
           try {
              // This is a blocking call and will only return on a
              // successful connection or an exception

              mmSocket.connect();
           } catch (IOException e) {
              Log.e(TAG, "connectionFailed", e);

              connectionFailed();
              // Close the socket
             try {
               mmSocket.close();
             } catch (IOException e2) {}
         }
         if(countAtemptoConnect==5)
             return;
       }
     }
     // Reset the ConnectThread because we're done
     synchronized (BluetoothService.this) {
     mConnectThread = null;
   }
    if(IS_READING)
       // Start the connected thread
       connected(mmSocket, mmDevice);
}