varun
varun

Reputation: 300

Android: Determine if Bluetooth is connected to any devices or not

Hello everyone is there a way to check if Android phone is connected to any Bluetooth devices programmatically?

Should there be a state such as Bluetooth_state == Bluetooth_connected OR Bluetooth_state == Bluetooth_disconnected OR Bluetooth.isConnected(). The goal is to recognise if Phone's Bluetooth is connected to any device or not.

Upvotes: 0

Views: 3945

Answers (1)

sud007
sud007

Reputation: 6141

If you only want to check that device is connected or not upon launch, try mBluetoothAdapter.getProfileConnectionState(); should work for you.

 public static boolean isBluetoothHeadsetConnected() {
 BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
 return mBluetoothAdapter != null && mBluetoothAdapter.isEnabled()
       && mBluetoothAdapter.getProfileConnectionState(BluetoothHeadset.HEADSET) == BluetoothHeadset.STATE_CONNECTED;
}//BluetoothHeadset.A2DP can also be used for Stereo media devices.

Don't forget to ask for permission in manifest as well.

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

Original Answer by @jobbert

Upvotes: 3

Related Questions