简体   繁体   English

STATE_CONNECTED 不代表真的是CONNECTED?

[英]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() :还有getConnectedDevices()的解释:

Return the set of devices which are in state STATE_CONNECTED返回处于状态STATE_CONNECTED的设备STATE_CONNECTED

The return type of the method is List<BluetoothDevice> , and it returns more than one in my case.该方法的返回类型是List<BluetoothDevice> ,在我的情况下它返回多个。 One for Galaxy Watch, one for Galaxy Buds.一种用于 Galaxy Watch,一种用于 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.当前使用的将在调用BluetoothHeadset.isAudioConnected()时返回 true。 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.我更想了解STATE_CONNECTED真正含义。

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.这是因为用户的蓝牙耳机在打开蓝牙耳机并与智能手机同步后会立即变为STATE_CONNECTED

So, what is STATE_CONNECTED exactly?那么,究竟什么是STATE_CONNECTED

As per documentation, bluetoothAdapter.getProfileConnectionState(BluetoothProfile.HEADSET) returns the current connection state of the BluetoothProfile.HEADSET profile.根据文档, bluetoothAdapter.getProfileConnectionState(BluetoothProfile.HEADSET)返回BluetoothProfile.HEADSET配置文件的当前连接状态。

This call returns BluetoothProfile.STATE_CONNECTED if the bluetooth adapter of the phone is currently connected to a device with the headset profile.如果手机的蓝牙适配器当前已连接到具有耳机配置文件的设备,则此调用将返回BluetoothProfile.STATE_CONNECTED

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.这意味着耳机已准备好处理与手机的通信。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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