繁体   English   中英

如何以编程方式检查Android中蓝牙设备的连接状态?

[英]How to programmatically check connection state of a Bluetooth device in Android?

我可以枚举配对的Bluetooth设备,我需要在应用启动时检查它们当前的连接状态。

但是,我发现无法获得连接状态?!

以下是我尝试但失败的一些努力:

  1. android.bluetooth.BluetoothDevice

它具有获取远程设备的绑定状态的方法,并且绑定状态的可能值为: BOND_NONEBOND_BONDINGBOND_BONDED 但这不是连接状态。

  1. android.bluetooth.BluetoothProfile

该方法可用于获取连接状态,但必须首先获取BluetoothProfile对象。

public abstract int getConnectionState (BluetoothDevice device); 

正如doc中所描述的:“ 客户端应调用getProfileProxy(Context,BluetoothProfile.ServiceListener,int),以获取配置文件代理。 ”,它只能在状态更改时调用的ServiceListener检索。

如果我在启动时查询状态而没有发生任何状态更改,该怎么办?

  1. android.bluetooth.BluetoothManager

它提供了一种方法:

public int getConnectionState (BluetoothDevice device, int profile);

但它不能用于检测Bluetooth扬声器的连接状态,因为配置文件参数只接受GATTGATT_SERVER (对于低能耗设备),不支持其他配置文件( HEADSETHEALTHA2DP )。

  1. android.bluetooth.BluetoothAdapter

它提供了一种方法:

public int getProfileConnectionState (int profile);

此功能可用于检查本地Bluetooth适配器是否已连接到特定配置文件的任何远程设备。 它可用于检查指定的配置文件。 但它不能用于检查给定的设备。

有谁知道如何获得给定Bluetooth设备的连接状态?

提前致谢。

请参阅类似问题的答案中的注释如何以编程方式判断蓝牙设备是否已连接? (Android 2.2)

  • 在应用程序启动时无法检索已连接设备的列表。 蓝牙API不允许您进行查询,而是允许您收听更改。
  • 对上述问题的一个愚蠢的解决方法是检索所有已知/配对设备的列表...然后尝试连接到每个设备(以确定您是否已连接)。 或者,您可以让后台服务观看蓝牙API并将设备状态写入磁盘,以便您的应用程序在以后使用。

暂无
暂无

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

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