0

I'm developing an android app and I need to know when Bluetooth is connected with a wearable device. I tried to use a method with a Bluetooth adapter, but without good results. How can I program this method?

public static boolean btConnected() {
    if( /*Bluetooth is connected*/ )
        return true;
    else
        return false;
}
Nicola Gallazzi
  • 7,897
  • 6
  • 45
  • 64
  • 1
    Possible duplicate of [How to check if bluetooth is enabled programmatically?](https://stackoverflow.com/questions/7672334/how-to-check-if-bluetooth-is-enabled-programmatically) – Tomas Jablonskis Mar 18 '19 at 11:24
  • Does your app manage connection with the device or you only want to know if the bluetooth adapter is connected to some device? – Nicola Gallazzi Mar 18 '19 at 11:26

1 Answers1

0

Add this permission in your Manifest.xml

<uses-permission android:name="android.permission.BLUETOOTH" />

and create a method which will check if Bluetooth is connected

private boolean isBluetootConnected() {
    BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    return mBluetoothAdapter != null && mBluetoothAdapter.isEnabled()
            && mBluetoothAdapter.getProfileConnectionState(BluetoothHeadset.HEADSET) == BluetoothHeadset.STATE_CONNECTED;
} 
Abid Khan
  • 2,451
  • 4
  • 22
  • 45
  • This would only work for devices that implement the headset profile – Nicola Gallazzi Mar 18 '19 at 11:27
  • In case the condition with head set can be removed and can be used only the one with isEnabled before checking it's not null. `return mBluetoothAdapter != null && mBluetoothAdapter.isEnabled()` – Abid Khan Mar 18 '19 at 11:31
  • that's not correct, isEnabled only tells you if the adapter is on, not if it's connected with some device. To manage connections you have to use GATT – Nicola Gallazzi Mar 18 '19 at 11:35