2014-09-11 108 views
8

我可以枚举配对的Bluetooth设备,我需要检查应用程序启动时的当前连接状态。如何以编程方式检查Android中蓝牙设备的连接状态?

但是,我发现没有办法获得连接状态?!

这里有一些努力,我尝试过但失败了:

  1. android.bluetooth.BluetoothDevice

它获得远程设备的结合状态的方法,以及可能的值。键态为:BOND_NONE,BOND_BONDINGBOND_BONDED。但这不是连接状态。

  • android.bluetooth.BluetoothProfile
  • 的方法可以用于获得连接状态,但必须首先获得BluetoothProfile对象。

    public abstract int getConnectionState (BluetoothDevice device); 
    

    如描述文档:“客户来电垂询getProfileProxy(上下文,BluetoothProfile.ServiceListener,INT),获取配置文件代理”,它只能在调用时的状态变化ServiceListener进行检索。

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

  • android.bluetooth.BluetoothManager
  • 它提供了一种方法,包括:

    public int getConnectionState (BluetoothDevice device, int profile); 
    

    但是它不能被用于检测Bluetooth扬声器的连接状态,因为配置参数只接受GATT或​​(对于低能量设备),不支持其他配置文件(HEADSET,HEALTHA2DP)。

  • android.bluetooth.BluetoothAdapter
  • 它提供了一种方法,包括:

    public int getProfileConnectionState (int profile); 
    

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

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

    在此先感谢。

    +0

    您是否找到任何答案? – 2015-09-02 09:54:34

    +0

    在这两天的帮助可以不胜感激。找到任何解决方案。我想检查蓝牙gatt连接状态,如果连接,则需要等待,直到它移动到断开状态,但我使用getConnectionState()它会引发异常。 – prasanthMurugan 2017-10-23 13:41:44

    回答

    1

    看到答案的音符从类似的问题How to programmatically tell if a Bluetooth device is connected? (Android 2.2)

    • 没有办法来检索应用程序启动时连接设备的列表。 Bluetooth API不允许您进行QUERY,而是允许您聆听CHANGES。
    • 解决上述问题的一个窍门是检索所有已知/配对设备的列表...然后尝试连接到每个设备(以确定您是否已连接)。 或者,您可以让后台服务人员观看蓝牙API并将设备状态写入磁盘,供应用程序在以后使用。