title: How to connect with paired Bluetooth device programmatic in Android?
body: <p>I am developing an application where I have to connect to Bluetooth paired device. These two devices are paired with each other. Now when I try to connect with paired device it stuck with the connecting mode dialog. Log shows that it successfully connect but does not dismiss the dialog.</p>
  
  <p>Here is my code snippet.</p>
  
  <p>Main.java</p>
  
  <pre><code>import java.io.BufferedReader;
  import java.io.File;
  import java.io.FileOutputStream;
  import java.io.IOException;
  import java.io.InputStream;
  import java.io.InputStreamReader;
  import java.text.SimpleDateFormat;
  import java.util.Calendar;
  import java.util.Date;
  import java.util.Random;
  import java.util.Set;
  import java.util.TimeZone;
  import java.util.UUID;
  import android.app.Activity;
  import android.app.AlertDialog;
  import android.app.AlertDialog.Builder;
  import android.app.Dialog;
  import android.app.ProgressDialog;
  import android.bluetooth.BluetoothAdapter;
  import android.bluetooth.BluetoothDevice;
  import android.bluetooth.BluetoothSocket;
  import android.content.Context;
  import android.content.DialogInterface;
  import android.content.DialogInterface.OnCancelListener;
  import android.content.Intent;
  import android.content.SharedPreferences;
  import android.content.res.Configuration;
  import android.graphics.Bitmap;
  import android.graphics.Canvas;
  import android.graphics.Color;
  import android.graphics.ColorMatrix;
  import android.graphics.ColorMatrixColorFilter;
  import android.graphics.PorterDuff.Mode;
  import android.graphics.drawable.BitmapDrawable;
  import android.graphics.drawable.Drawable;
  import android.os.Bundle;
  import android.os.Environment;
  import android.preference.PreferenceManager;
  import android.provider.Settings;
  import android.util.Log;
  import android.view.KeyEvent;
  import android.view.MotionEvent;
  import android.view.View;
  import android.view.Window;
  import android.view.WindowManager;
  import android.view.View.OnTouchListener;
  import android.view.inputmethod.InputMethodManager;
  import android.widget.Button;
  import android.widget.EditText;
  import android.widget.ImageView;
  import android.widget.TextView;
  import android.widget.Toast;
  
  public class Main extends Activity 
  {
      protected static final String TAG = "TAG";
      private static final int REQUEST_CONNECT_DEVICE = 1;
      private static final int REQUEST_ENABLE_BT = 2;
  
      Button mScan;
      BluetoothAdapter mBluetoothAdapter;
      private UUID applicationUUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
  
      @Override
      public void onCreate(Bundle mSavedInstanceState) 
      {
          super.onCreate(mSavedInstanceState);
          requestWindowFeature(Window.FEATURE_NO_TITLE);
          getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
              WindowManager.LayoutParams.FLAG_FULLSCREEN);
          setContentView(R.layout.main);
          mScan = (Button) findViewById(R.id.Scan);
          mScan.setOnClickListener(new View.OnClickListener() 
          {
              public void onClick(View mView) 
              {
                  mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
                  if (mBluetoothAdapter == null) 
                  {
                      Toast.makeText(Main.this, "DeviceHasNoSupport", 2000).show();
                  } 
                  else 
                  {
                      if (!mBluetoothAdapter.isEnabled()) 
                      {
                           Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                          startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
                       } 
                       else 
                       {
                          ListPairedDevices();
                          Intent connectIntent = new Intent(Main.this, DeviceListActivity.class);
                          startActivityForResult(connectIntent, REQUEST_CONNECT_DEVICE);
                      }
                  }
              }
          });
      }// onCreate
  
      public void onActivityResult(int mRequestCode, int mResultCode, Intent mDataIntent) 
      {
          super.onActivityResult(mRequestCode, mResultCode, mDataIntent);
  
          switch (mRequestCode) 
          {
              case REQUEST_CONNECT_DEVICE:
                  if (mResultCode == Activity.RESULT_OK) 
                  {
                      Bundle mExtra = mDataIntent.getExtras();
                      String mDeviceAddress = mExtra.getString("DeviceAddress");
                      Log.v(TAG, "Coming incoming address " + mDeviceAddress);
                      BluetoothDevice mBluetoothDevice = mBluetoothAdapter.getRemoteDevice(mDeviceAddress);
                      pairToDevice(mBluetoothDevice);
                      Log.v(TAG, "pairToDeviceCalled");
                  }
                  break;
  
              case REQUEST_ENABLE_BT:
                  if (mResultCode == Activity.RESULT_OK) 
                  {
                      ListPairedDevices();
                      Intent connectIntent = new Intent(Main.this, DeviceListActivity.class);
                      startActivityForResult(connectIntent, REQUEST_CONNECT_DEVICE);
                  } 
                  else 
                  {
                      Toast.makeText(Main.this, "BTNotEnabled", 2000).show();
                  }
                  break;
          }
      }
  
      private void ListPairedDevices() 
      {
          Set&lt;BluetoothDevice&gt; mPairedDevices = mBluetoothAdapter.getBondedDevices();
          if (mPairedDevices.size() &gt; 0) 
          {
              for (BluetoothDevice mDevice : mPairedDevices) 
              {
                  Log.v(TAG, "PairedDevices: " + mDevice.getName() + " " + mDevice.getAddress());
              }
          }
      }
  
      private void pairToDevice(BluetoothDevice nBluetoothDevice) 
      {
          Log.v(TAG, "InsidepairToDeviceCalled");
          openSocket(nBluetoothDevice);
          Log.v(TAG, "LeavingpairToDeviceCalled");
      }
  
      private void openSocket(BluetoothDevice nBluetoothDevice) 
      {
          try 
          {
              Log.v(TAG, "InsideOpenSockedCalled");
              final ProgressDialog dialog = new ProgressDialog(this);
              final ConnectRunnable connector = new ConnectRunnable(nBluetoothDevice, dialog);
              Log.v(TAG, "InsideOpenSockedConnecterCalled");
              ProgressDialog.show(this, "Connecting...", nBluetoothDevice.getName() + " : " + nBluetoothDevice.getAddress(),
                  true, true,
                  new OnCancelListener() 
              {
                          public void onCancel(DialogInterface dialog) 
                          {
                              connector.cancel();
                          }
                      });
              new Thread(connector).start();
          } 
          catch (IOException ex) 
          {
              Log.d(TAG, "Could not open bluetooth socket", ex);
          }
      }
  
      private class ConnectRunnable implements Runnable 
      {
          private final ProgressDialog dialog;
          private final BluetoothSocket socket;
  
          public ConnectRunnable(BluetoothDevice device, ProgressDialog dialog) throws IOException 
          {
              socket = device.createRfcommSocketToServiceRecord(applicationUUID);
              this.dialog = dialog;
          }
  
          public void run() 
          {
              try 
              {
                  Log.v(TAG, "InsideRunnableCalled");
                  mBluetoothAdapter.cancelDiscovery();
                  socket.connect();
                  Log.v(TAG, "InsideRunnableSocketConnectCalled");
              }
              catch (IOException connectException) 
              {
                  Log.d(TAG, "Could not connect to socket", connectException);
                  closeSocket(socket);
                  return;
              }
              Log.v(TAG, "Connected");
              dismissDialog(dialog);
              closeSocket(socket);
          }
  
          public void cancel() 
          {
              try 
              {
                  socket.close();
              }
              catch (IOException e) 
              {
                  Log.d(TAG, "Canceled connection", e);
              }
          }
      }
  
      private void dismissDialog(final Dialog dialog) 
      {
          runOnUiThread(new Runnable() 
          {
              public void run() 
              {
                  dialog.dismiss();
                  Log.v(TAG, "DialogClosed");
              }
          });
      }
  
      private void closeSocket(BluetoothSocket nOpenSocket) 
      {
          try 
          {
              nOpenSocket.close();
              Log.v(TAG, "SockectClosed");
          } 
          catch (IOException ex) 
          {
              Log.d(TAG, "Could not close exisiting socket", ex);
          }
      }
      @Override
      public boolean onKeyDown(int mKeyCode, KeyEvent mKeyEvent) 
      {
          if ((!(android.os.Build.VERSION.SDK_INT &gt; android.os.Build.VERSION_CODES.DONUT)
              &amp;&amp; mKeyCode == KeyEvent.KEYCODE_BACK &amp;&amp; mKeyEvent.getRepeatCount() == 0)) 
          {
              onBackPressed();
          }
          return super.onKeyDown(mKeyCode, mKeyEvent);
      }
  
      public void onBackPressed() 
      {
          finish();
      }
  }
  </code></pre>
  
  <p>DeviceListActivity.java</p>
  
  <pre><code>import java.util.Set;
  import android.app.Activity;
  import android.bluetooth.BluetoothAdapter;
  import android.bluetooth.BluetoothDevice;
  import android.content.Intent;
  import android.os.Bundle;
  import android.util.Log;
  import android.view.View;
  import android.view.Window;
  import android.widget.AdapterView;
  import android.widget.ArrayAdapter;
  import android.widget.ListView;
  import android.widget.TextView;
  import android.widget.AdapterView.OnItemClickListener;
  
  public class DeviceListActivity extends Activity 
  {
      protected static final String TAG = "TAG";
      private BluetoothAdapter mBluetoothAdapter;
      private ArrayAdapter&lt;String&gt; mPairedDevicesArrayAdapter;
  
      @Override
      protected void onCreate(Bundle mSavedInstanceState) 
      {
          super.onCreate(mSavedInstanceState);
          requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
          setContentView(R.layout.device_list);
  
          setResult(Activity.RESULT_CANCELED);
          mPairedDevicesArrayAdapter = new ArrayAdapter&lt;String&gt;(this, R.layout.device_name);
  
          ListView mPairedListView = (ListView) findViewById(R.id.paired_devices);
          mPairedListView.setAdapter(mPairedDevicesArrayAdapter);
          mPairedListView.setOnItemClickListener(mDeviceClickListener);
  
          mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
          Set&lt;BluetoothDevice&gt; mPairedDevices = mBluetoothAdapter.getBondedDevices();
  
          if (mPairedDevices.size() &gt; 0) 
          {
              findViewById(R.id.title_paired_devices).setVisibility(View.VISIBLE);
              for (BluetoothDevice mDevice : mPairedDevices) 
              {
                  mPairedDevicesArrayAdapter.add(mDevice.getName() + "\n" + mDevice.getAddress());
              }
          } 
          else 
          {
              String mNoDevices = getResources().getText(R.string.none_paired).toString();
              mPairedDevicesArrayAdapter.add(mNoDevices);
          }
      }
  
      @Override
      protected void onDestroy() 
      {
          super.onDestroy();
          if (mBluetoothAdapter != null) 
          {
              mBluetoothAdapter.cancelDiscovery();
          }
      }
  
      private OnItemClickListener mDeviceClickListener = new OnItemClickListener() 
      {
          public void onItemClick(AdapterView&lt;?&gt; mAdapterView, View mView, int mPosition, long mLong) 
          {
              mBluetoothAdapter.cancelDiscovery();
              String mDeviceInfo = ((TextView) mView).getText().toString();
              String mDeviceAddress = mDeviceInfo.substring(mDeviceInfo.length() - 17);
              Log.v(TAG, "Device_Address " + mDeviceAddress);
  
              Bundle mBundle = new Bundle();
              mBundle.putString("DeviceAddress", mDeviceAddress);
              Intent mBackIntent = new Intent();
              mBackIntent.putExtras(mBundle);
              setResult(Activity.RESULT_OK, mBackIntent);
              finish();
          }
      };
  
  }
  </code></pre>
  
  <p>Android Manifest</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.androidvouge.bluetooth" android:versionCode="1"
      android:versionName="1.0"&gt;
          &lt;application android:icon="@drawable/icon" android:label="@string/name"
          android:theme="@android:style/Theme.NoTitleBar.Fullscreen"&gt;
              &lt;activity android:name=".Main" android:screenOrientation="portrait"&gt;
              &lt;intent-filter&gt;
                  &lt;action android:name="android.intent.action.MAIN" /&gt;
                  &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
              &lt;/intent-filter&gt;
          &lt;/activity&gt;
          &lt;activity android:name=".DeviceListActivity"     android:theme="@android:style/Theme.Dialog"
          android:configChanges="orientation|keyboardHidden"
          android:screenOrientation="portrait" /&gt;
      &lt;/application&gt;
      &lt;uses-sdk android:minSdkVersion="7" android:targetSdkVersion="11" /&gt;
      &lt;uses-permission android:name="android.permission.BLUETOOTH" /&gt;
      &lt;uses-permission android:name="android.permission.BLUETOOTH_ADMIN" /&gt;
      &lt;supports-screens android:smallScreens="false"
          android:normalScreens="true" android:largeScreens="true" /&gt;
  &lt;/manifest&gt;
  </code></pre>
  
  <p>device_list.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:orientation="vertical" android:layout_width="fill_parent"
      android:layout_height="fill_parent"&gt;
      &lt;TextView android:id="@+id/title_paired_devices"
          android:layout_width="fill_parent" android:layout_height="wrap_content"
          android:text="@string/title_paired_devices" android:visibility="gone"
          android:background="#666" android:textColor="#fff"
          android:paddingLeft="5dip" /&gt;
      &lt;ListView android:id="@+id/paired_devices"
          android:layout_width="fill_parent" android:layout_height="wrap_content"
          android:stackFromBottom="true" android:layout_weight="1" /&gt;
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>device_name.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;TextView xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="fill_parent" android:layout_height="wrap_content"
      android:textSize="18sp" android:padding="5dip" /&gt;
  </code></pre>
  
  <p>main.xml has only button.</p>
  
  <p>Here is the logcat output.</p>
  
  <pre><code>06-16 13:46:08.640 19062 19062 E CachedBluetoothDevice: updating profiles for $ $ S A \/ A N $ $
  06-16 13:46:08.640 19062 19062 V CachedBluetoothDevice: opp classbits != uuid
  06-16 13:46:08.640 19062 19062 V CachedBluetoothDevice: Class: 5a0204
  06-16 13:46:08.640 19062 19062 V CachedBluetoothDevice: UUID:
  06-16 13:46:08.640 19062 19062 V CachedBluetoothDevice:   00001101-0000-1000-8000-00805f9b34fb
  06-16 13:46:08.667 19091 19091 V TAG     : PairedDevices: $ $ S A \/ A N $ $ 00:1D:3B:05:B4:D5
  06-16 13:46:10.582 19091 19091 V TAG     : Device_Address 00:1D:3B:05:B4:D5
  06-16 13:46:10.597 19091 19091 V TAG     : Coming incoming address 00:1D:3B:05:B4:D5
  06-16 13:46:10.597 19091 19091 V TAG     : InsidepairToDeviceCalled
  06-16 13:46:10.597 19091 19091 V TAG     : InsideOpenSockedCalled
  06-16 13:46:10.718 19091 19091 V TAG     : InsideOpenSockedConnecterCalled
  06-16 13:46:10.789 19091 19091 V TAG     : LeavingpairToDeviceCalled
  06-16 13:46:10.789 19091 19091 V TAG     : pairToDeviceCalled
  06-16 13:46:10.800 19091 19128 V TAG     : InsideRunnableCalled
  06-16 13:46:13.730 19091 19128 V TAG     : InsideRunnableSocketConnectCalled
  06-16 13:46:13.730 19091 19128 V TAG     : Connected
  06-16 13:46:13.730 19091 19128 V TAG     : SockectClosed
  06-16 13:46:13.734 19091 19091 V TAG     : DialogClosed
  </code></pre>
  
  <p>Where am going wrong? Anybody who has successfully configured Bluetooth with processing dialog, Please give me a hand.</p>
  
  <p>Thanks.</p>