简体   繁体   English

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

[英]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: 以下是我尝试但失败的一些努力:

  1. android.bluetooth.BluetoothDevice android.bluetooth.BluetoothDevice

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_NONEBOND_BONDINGBOND_BONDED But it's not the connection state. 但这不是连接状态。

  1. android.bluetooth.BluetoothProfile android.bluetooth.BluetoothProfile

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? 如果我在启动时查询状态而没有发生任何状态更改,该怎么办?

  1. android.bluetooth.BluetoothManager android.bluetooth.BluetoothManager

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扬声器的连接状态,因为配置文件参数只接受GATTGATT_SERVER (对于低能耗设备),不支持其他配置文件( HEADSETHEALTHA2DP )。

  1. android.bluetooth.BluetoothAdapter android.bluetooth.BluetoothAdapter

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)

  • There is no way to retrieve a list of connected devices at application startup. 在应用程序启动时无法检索已连接设备的列表。 The Bluetooth API does not allow you to QUERY, instead it allows you to listen to CHANGES. 蓝牙API不允许您进行查询,而是允许您收听更改。
  • A hoaky work around to the above problem would be to retrieve the list of all known/paired devices... then trying to connect to each one (to determine if you're connected). 对上述问题的一个愚蠢的解决方法是检索所有已知/配对设备的列表...然后尝试连接到每个设备(以确定您是否已连接)。 Alternatively, you could have a background service watch the Bluetooth API and write the device states to disk for your application to use at a later date. 或者,您可以让后台服务观看蓝牙API并将设备状态写入磁盘,以便您的应用程序在以后使用。

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

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