title: Check Bluetooth State on specific device on android
body: <p>I'm trying to know at my application start up if a paired device is connect or not. In practice the main app give me an MAC address and I've got to respond if we're connected or not. </p>
  
  <p>A BroadcastReceiver can't be a full option because if the user start the connection with the device before launching the app I'll be unable to know if I'm connect to the device or not.</p>
  
  <p>I found this on the forum : <a href="https://stackoverflow.com/questions/4715865/how-to-programmatically-tell-if-a-bluetooth-device-is-connected">How to programmatically tell if a Bluetooth device is connected?</a></p>
  
  <blockquote>
    <p>There is no way to retrieve the list of connected devices at application startup. The Bluetooth API will only let you listen to connection changes.</p>
  </blockquote>
  
  <p>but i think this isn't accurate anymore because with this code : </p>
  
  <pre><code>public class cServiceListener  implements BluetoothProfile.ServiceListener  {
  private static final int[] states={ BluetoothProfile.STATE_DISCONNECTING,
          BluetoothProfile.STATE_DISCONNECTED,
          BluetoothProfile.STATE_CONNECTED,
          BluetoothProfile.STATE_CONNECTING};
  @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
  @Override
  public void onServiceConnected(int profile, BluetoothProfile bluetoothProfile) {
  
  
      //List&lt;BluetoothDevice&gt; Devices=bluetoothProfile.getDevicesMatchingConnectionStates(states);
      BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
      Set&lt;BluetoothDevice&gt; devices = mBluetoothAdapter.getBondedDevices();
  
      Log.i("myTag","\n\n&lt;==============profile connexion state =============&gt; : "+ mBluetoothAdapter.getProfileConnectionState(1));
  
      for (BluetoothDevice loop:devices){
          Log.i("myTag","Nom du device :" +loop.getName()+" Etat du Device:"+bluetoothProfile.getConnectionState(loop)
          + "  Type du device : "+ loop.getType() + " Classe du device : " + loop.getBluetoothClass().toString() );
      }
  }
  
  @Override
  public void onServiceDisconnected(int profile) {
  }}
  </code></pre>
  
  <p>I'm able to tell if SOME devices are connected or not but not with all of them !! 
  I try the code with portable speakers and with others devices where the calls AND the sounds is take in charge and it work perfectly : if the device is connect it return 2 and if not it return 0. 
  But when i try it on my real objective (which is a car) it return 0 at anytime. The test car only take in charge calls (you can't play your music with Bluetooth connection, to old). So I think with more modern car it can work..</p>
  
  <p>First i was thinking that car using BLE technology so I try with this code </p>
  
  <pre><code>         List&lt;BluetoothDevice&gt; devices = bluetoothManager.getConnectedDevices(BluetoothProfile.GATT);
              Log.d("myTag","\n Taille de la liste : " + devices.size());
              for(BluetoothDevice device : devices) {
  
                  if(device.getType() == BluetoothDevice.DEVICE_TYPE_LE) {
                      Log.d("myTag","\n BLE &lt;===&gt; Nom du device :" + device.getName());
                  }
                  Log.d("myTag","\n&lt;====NONE BLE=====&gt; // \n\n Nom du device :" + device.getName()+"\n\n");
              }
  </code></pre>
  
  <p>But in fact this isn't the point, this don't work. 
  And it's weird because </p>
  
  <pre><code> mBluetoothAdapter.getProfileConnectionState(1)
  </code></pre>
  
  <p>returning the correct value when I'm connect to the car only.
  I really don't know what to do know and why
  bluetoothProfile.getConnectionState() returning the wrong value.
  If someone have any idea, I'll be glad to know.</p>