I can enumerate the paired Bluetooth
devices, and I need to check their current connection state on app startup.
But, I found no way of getting the connection state ?!
Here are some efforts I've tried but failed:
It has the method to get the bond state of the remote device, and possible values for the bond state are: BOND_NONE
, BOND_BONDING
, BOND_BONDED
. But it's not the connection state.
The method can be used to get the connection state, but the BluetoothProfile
object must be obtained first.
public abstract int getConnectionState (BluetoothDevice device);
As describe in doc: "Clients should call getProfileProxy(Context, BluetoothProfile.ServiceListener, int), to get the Profile Proxy.", it can ONLY be retrieve in a ServiceListener
called when state changes.
What if I query the state on startup without any state changes happen yet?
It provides a method:
public int getConnectionState (BluetoothDevice device, int profile);
But it cannot be used to detect connection state of a Bluetooth
speaker, as the profile argument only accepts GATT
or GATT_SERVER
(for low energy device), other profiles (HEADSET
, HEALTH
, A2DP
) are not supported.
It provides a method:
public int getProfileConnectionState (int profile);
This function can be used to check whether the local Bluetooth
adapter is connected to any remote device for a specific profile. It can be used to check a specified profile. But it cannot be used to check a given device.
Does anyone know how to get the connection state of a given Bluetooth
device?
Thanks in advance.
See the notes in the answer from similar question How to programmatically tell if a Bluetooth device is connected? (Android 2.2):
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With