title: android bluetooth pairing request and wait for success
body: <p>I am designing this app for api level 2.2</p>
  
  <p>I want pair with a available bluetooth device and then listen the ststus,
  I have done that by the following way:</p>
  
  <pre><code> listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
          @Override
          public void onItemClick(AdapterView&lt;?&gt; adapterView, View view, int i, long l) {
              if(btAdapter.isDiscovering()){
                  btAdapter.cancelDiscovery();
              }
              if(!listAdapter.getItem(i).contains("Paired")){
                  try {
                      BluetoothDevice selectedDevice = devices.get(i);
                      pairDevice(selectedDevice);
                      Intent intent=new Intent(getBaseContext(),ConnectedBtActivity.class);
                      intent.putExtra("MAC",selectedDevice.getAddress());
                      startActivity(intent);
                  }
                  catch (Exception e){}
              }
              else{
                  BluetoothDevice selectedDevice = devices.get(i);
                  Intent intent=new Intent(getBaseContext(),ConnectedBtActivity.class);
                  intent.putExtra("MAC",selectedDevice.getAddress());
                  startActivity(intent);
              }
          }
      });
  
  private void pairDevice(BluetoothDevice device) {
      try {
          Method m = device.getClass().getMethod("createBond", (Class[]) null);
          m.invoke(device, (Object[]) null);
      } catch (Exception e) {
          Toast.makeText(getBaseContext(),"Exception: "+e.getMessage(),Toast.LENGTH_LONG ).show();
      }
  }
  </code></pre>
  
  <p>Here listView contain all the available devices, listAdapter is an ArrayAdapter which contain the available device name and for pair device concate "(Paired)" with device name.
  You can clearly see I want to open a new activity if the device already paired and if not paired then initiate the pairing process. Now issue is that pairDevice() is a multi threaded process that means does not wait until the pairing is complete. I want to listen whether the pairing is done or not after that new activity should open.
  For better clarification I am posting full code:</p>
  
  <pre><code>public class MyBluetoothScanActivity extends AppCompatActivity{
  
  
  Button bt,bt_count;
  ListView listView;
  BluetoothAdapter btAdapter;
  Set&lt;BluetoothDevice&gt; devicesArray;
  public static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
  IntentFilter filter;
  BroadcastReceiver receiver;
  ArrayAdapter&lt;String&gt; listAdapter;
  ArrayList&lt;String&gt; pairedDevices;
  ArrayList&lt;BluetoothDevice&gt; devices;
  
  int count=0;
  
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_my_bluetooth_scan);
  
      bt=(Button) findViewById(R.id.bT_scan2);
      bt.setTransformationMethod(null);
  
      bt_count=(Button) findViewById(R.id.bt_count);
      bt_count.setTransformationMethod(null);
  
      bt_count.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View view) {
              Toast.makeText(getBaseContext(),"Count: "+count,Toast.LENGTH_SHORT ).show();
          }
      });
  
  
  
  
      listView=(ListView) findViewById(R.id.listViewscan);
  
      btAdapter = BluetoothAdapter.getDefaultAdapter();
      filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
  
      if(!btAdapter.isEnabled()){
          turnOnBT();
      }
  
  
      init();
  
      bt.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View view) {
              newScan();
          }
      });
  
  
      listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
          @Override
          public void onItemClick(AdapterView&lt;?&gt; adapterView, View view, int i, long l) {
              if(btAdapter.isDiscovering()){
                  btAdapter.cancelDiscovery();
              }
              if(!listAdapter.getItem(i).contains("Paired")){
                  try {
                      BluetoothDevice selectedDevice = devices.get(i);
                      pairDevice(selectedDevice);
                      Intent intent=new Intent(getBaseContext(),ConnectedBtActivity.class);
                      intent.putExtra("MAC",selectedDevice.getAddress());
                      startActivity(intent);
                  }
                  catch (Exception e){}
              }
              else{
                  BluetoothDevice selectedDevice = devices.get(i);
                  Intent intent=new Intent(getBaseContext(),ConnectedBtActivity.class);
                  intent.putExtra("MAC",selectedDevice.getAddress());
                  startActivity(intent);
              }
          }
      });
  }
  
  private void newScan(){
      btAdapter.cancelDiscovery();
      Toast.makeText(getBaseContext(),"New Scan Start",Toast.LENGTH_SHORT ).show();
  
      listAdapter= new ArrayAdapter&lt;String&gt;(getBaseContext(), android.R.layout.simple_list_item_1,0);
      listView.setAdapter(listAdapter);
  
      devices = new ArrayList&lt;BluetoothDevice&gt;();
      btAdapter.startDiscovery();
  }
  private void getPairedDevices() {
      devicesArray = btAdapter.getBondedDevices();
      if(devicesArray.size()&gt;0){
          for(BluetoothDevice device:devicesArray){
              pairedDevices.add(device.getName());
  
          }
      }
  }
  
  void turnOnBT(){
      Intent intent =new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
      startActivity(intent);
  }
  
  void init(){
  
      receiver = new BroadcastReceiver(){
          @Override
          public void onReceive(Context context, Intent intent) {
  
              String action = intent.getAction();
  
              Toast.makeText(getBaseContext(),"new br: "+action,Toast.LENGTH_LONG ).show();
  
              if(BluetoothDevice.ACTION_FOUND.equals(action)){
  
                  pairedDevices=new ArrayList&lt;String&gt;();
                  getPairedDevices();
                  BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
  
                  Toast.makeText(getBaseContext(),"Dev: "+device.getName(),Toast.LENGTH_LONG ).show();
                  devices.add(device);
                  String s = "";
                  for(int a = 0; a &lt; pairedDevices.size(); a++){
                      if(device.getName().equals(pairedDevices.get(a))){
                          //append
                          s = "(Paired)";
                          break;
                      }
                  }
  
                  listAdapter.add(device.getName()+" "+s+" "+"\n"+device.getAddress());
  
              }
              else if(BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)){
                  if(btAdapter.getState() == btAdapter.STATE_OFF){
                      turnOnBT();
                  }
              }
  
          }
      };
  
      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);
  
  }
  
  private void pairDevice(BluetoothDevice device) {
      try {
          Method m = device.getClass().getMethod("createBond", (Class[]) null);
          m.invoke(device, (Object[]) null);
      } catch (Exception e) {
          Toast.makeText(getBaseContext(),"Exception: "+e.getMessage(),Toast.LENGTH_LONG ).show();
      }
  }
  
  @Override
  protected void onDestroy() {
      super.onDestroy();
      try {
          Toast.makeText(getBaseContext(),"Un registration",Toast.LENGTH_SHORT ).show();
          unregisterReceiver(receiver);
      }
      catch (Exception e){}
  }
  </code></pre>
  
  <p>}</p>