How to programmatically check the connection status of a Bluetooth device in Android?

I can list paired Bluetooth devices, and I need to check their current connection status when the application starts.

But I did not find a way to get the connection status ?!

Here are some attempts I tried but failed:

  • android.bluetooth.BluetoothDevice

It has a way to get the communication status of the remote device, and the possible values ​​for the communication status are: BOND_NONE , BOND_BONDING , BOND_BONDED . But this is not a connection state.

  1. android.bluetooth.BluetoothProfile

This method can be used to get the connection status, but the BluetoothProfile object must first be received.

 public abstract int getConnectionState (BluetoothDevice device); 

As described in the document: "Clients must call getProfileProxy (Context, BluetoothProfile.ServiceListener, int) to get a proxy proxy.", It can ONLY be retrieved into the ServiceListener when the state changes.

What if I request a state at startup without any state changes?

  1. android.bluetooth.BluetoothManager

It provides a method:

 public int getConnectionState (BluetoothDevice device, int profile); 

But it cannot be used to determine the connection status of the Bluetooth speaker, since the profile argument accepts only GATT or GATT_SERVER (for a low-energy device), other profiles ( HEADSET , HEALTH , A2DP ) are not supported.

  1. android.bluetooth.BluetoothAdapter

It provides a method:

 public int getProfileConnectionState (int profile); 

This function can be used to check if the local Bluetooth adapter is connected to any remote device for a specific profile. It can be used to verify a given profile. But it cannot be used to test this device.

Does anyone know how to get the connection status of this Bluetooth ?

Thanks in advance.

+8
android bluetooth
source share
1 answer

See the notes in the answer to a similar question. How can I programmatically indicate if a Bluetooth device is connected? (Android 2.2) :

  • Unable to get the list of connected devices when the application starts. The Bluetooth API does not allow you to execute QUERY; instead, it allows you to listen to CHANGES.
  • The hard work on the above problem was to get a list of all known / paired devices ... then try to connect to each of them (to determine if you are connected). In addition, you can have a background service that monitors the Bluetooth API and write device statuses to disk for your application for later use.
+1
source share

All Articles