title: Monitoring the connection of a paired bluetooth device
body: <p>I have written the code which returns me a list of all the paired bluetooth devices of a android device.One among the list of paired bluetooth devices is my laptop as well.</p>
  
  <p>Now my question is </p>
  
  <p>Is there a way I can monitoring the bluetooth connectivity status between the laptop and the android device.The output which I need is "Connected" if there is a bluetooth connection present and "Disconnected" if there is no bluetooth connection</p>
  
  <hr>
  
  <p>The android version is 2.1.Eclair</p>
  
  <p>Before I proceed further , I have some questions.</p>
  
  <p>->Should I test this application when I connect the laptop and device through USB?
  ->How to run it on a separate thread or from an AsyncTask?
  ->The above code is not working for me if I connect the device and the laptop through USB and then launch the application. Even though the laptop and the phone are nearby and they are paired, I am not able to see a connected status in the phone.</p>
  
  <p>The code which I have written is as follows</p>
  
  <pre><code>public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_bluetooth_);
  
      out = (TextView) findViewById(R.id.textView1);
  
      // Getting the Bluetooth adapter
      BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
      // out.append("\nAdapter: " + adapter);
  
      // Check for Bluetooth support in the first place
      // Emulator doesn't support Bluetooth and will return null
      if (adapter == null) {
          out.append("\nBluetooth NOT supported. Aborting.");
          return;
      }
  
      // Starting the device discovery
      out.append("\nStarting discovery...");
      adapter.startDiscovery();
      out.append("\nDone with discovery...");
  
      // Listing paired devices
      out.append("\nDevices Pared:");
      Set&lt;BluetoothDevice&gt; devices = BluetoothAdapter.getDefaultAdapter()
              .getBondedDevices();
  
      for (BluetoothDevice device : devices) {
          out.append("\nFound device: " + device);
          out.append("\n The name is: " + device.getName());
          out.append("\n The type is: "
                  + device.getBluetoothClass().getDeviceClass());
  
          Method m;
          try {
              System.out.println("Trying to connect to " + device.getName());
              m = device.getClass().getMethod("createInsecureRfcommSocket",
                      new Class[] { int.class });
              BluetoothSocket socket = (BluetoothSocket) m.invoke(device, 1);
              socket.connect();
              out.append("Connected");
          } catch (Exception e) {
              e.printStackTrace();
              out.append("\nDisconnected");
          }
      }
  }
  </code></pre>