【发布时间】:2014-09-11 02:36:22
【问题描述】:
我可以枚举配对的Bluetooth设备,我需要在应用启动时检查它们当前的连接状态。
但是,我找不到获取连接状态的方法?!
以下是我尝试过但失败的一些努力:
- android.bluetooth.BluetoothDevice
它具有获取远程设备绑定状态的方法,绑定状态的可能值为:BOND_NONE、BOND_BONDING、BOND_BONDED。但这不是连接状态。
- android.bluetooth.BluetoothProfile
该方法可用于获取连接状态,但必须先获取BluetoothProfile对象。
public abstract int getConnectionState (BluetoothDevice device);
如文档中所述:“客户端应调用 getProfileProxy(Context, BluetoothProfile.ServiceListener, int),以获取配置文件代理。”,它只能在 ServiceListener 调用时检索状态变化。
如果我在启动时查询状态而没有发生任何状态更改怎么办?
- android.bluetooth.BluetoothManager
它提供了一个方法:
public int getConnectionState (BluetoothDevice device, int profile);
但它不能用于检测Bluetooth 扬声器的连接状态,因为配置文件参数只接受GATT 或GATT_SERVER(用于低能耗设备),其他配置文件(HEADSET、HEALTH、 A2DP) 不受支持。
- android.bluetooth.BluetoothAdapter
它提供了一个方法:
public int getProfileConnectionState (int profile);
此功能可用于检查本地Bluetooth 适配器是否连接到特定配置文件的任何远程设备。它可用于检查指定的配置文件。但它不能用于检查给定的设备。
有人知道如何获取给定Bluetooth 设备的连接状态吗?
提前致谢。
【问题讨论】:
-
你找到答案了吗?
-
对这两天的帮助感到非常感激。您是否找到任何解决方案。我想检查蓝牙 gatt 连接状态是否已连接,然后需要等到它进入断开状态,但我使用了 getConnectionState() 它会引发异常。