[英]How to programmatically check connection state of a Bluetooth device in Android?
I can enumerate the paired Bluetooth
devices, and I need to check their current connection state on app startup. 我可以枚举配对的
Bluetooth
设备,我需要在应用启动时检查它们当前的连接状态。
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
. 它具有获取远程设备的绑定状态的方法,并且绑定状态的可能值为:
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. 该方法可用于获取连接状态,但必须首先获取
BluetoothProfile
对象。
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. 正如doc中所描述的:“ 客户端应调用getProfileProxy(Context,BluetoothProfile.ServiceListener,int),以获取配置文件代理。 ”,它只能在状态更改时调用的
ServiceListener
检索。
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. 但它不能用于检测
Bluetooth
扬声器的连接状态,因为配置文件参数只接受GATT
或GATT_SERVER
(对于低能耗设备),不支持其他配置文件( HEADSET
, HEALTH
, A2DP
)。
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. 此功能可用于检查本地
Bluetooth
适配器是否已连接到特定配置文件的任何远程设备。 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? 有谁知道如何获得给定
Bluetooth
设备的连接状态?
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) :
(Android 2.2) :
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.