简体   繁体   中英

STATE_CONNECTED doesn't mean it's really CONNECTED?

If we have a look at this , it says :

Android only supports one connected Bluetooth Headset at a time .

Also the explanation of getConnectedDevices() :

Return the set of devices which are in state STATE_CONNECTED

The return type of the method is List<BluetoothDevice> , and it returns more than one in my case. One for Galaxy Watch, one for Galaxy Buds.

I know how to tell which one is active at the moment. The one currently being used will return true when BluetoothHeadset.isAudioConnected() is called. So I'm not asking how to find the active Bluetooth headset device here. I'm rather trying to understand what STATE_CONNECTED really means.

I think this is also useful to others because there are many answers like the following, which won't work as expected in some cases :

public static boolean isConnected() {
    BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    return  bluetoothAdapter != null
        && bluetoothAdapter.isEnabled()
        && bluetoothAdapter.getProfileConnectionState(BluetoothProfile.HEADSET) == BluetoothProfile.STATE_CONNECTED;
}

This is not the proper one if you just want to check if your user is talking on the phone. It's because the user's Bluetooth headset will become STATE_CONNECTED immediately after the Bluetooth headset is turned on and syncs with the smartphone.

So, what is STATE_CONNECTED exactly?

As per documentation, bluetoothAdapter.getProfileConnectionState(BluetoothProfile.HEADSET) returns the current connection state of the BluetoothProfile.HEADSET profile.

This call returns BluetoothProfile.STATE_CONNECTED if the bluetooth adapter of the phone is currently connected to a device with the headset profile.

It does not mean the user is currently using the headset to talk. It means the headset is ready to handle communication with the phone.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM