如何以编程方式判断蓝牙设备是否已连接?(Android 2.2)

dch*_*lle 77 android android-bluetooth

我知道如何获得配对设备列表,但我怎么知道它们是否已连接?

它必须是可能的,因为我看到它们列在我手机的蓝牙设备列表中,并说明了它们的连接状态.

Sky*_*ton 139

使用intent过滤器来侦听ACTION_ACL_CONNECTED,ACTION_ACL_DISCONNECT_REQUESTED和ACTION_ACL_DISCONNECTED广播:

<uses-permission android:name="android.permission.BLUETOOTH" />
Run Code Online (Sandbox Code Playgroud)

几点说明:

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

  • 在应用程序启动时无法检索已连接设备的列表.蓝牙API只允许您收听连接更改.所以是的,唯一的方法是创建一个长期运行的服务并添加/删除到公共列表.这是许多开发人员的一个很大抱怨.根据您的性能需求,您可以检索配对设备列表并尝试连接到每个设备.如果失败,则无法使用.但是警告的话:.connect()是一个阻塞操作. (10认同)
  • 我可以听取意图,但我怎样才能获得连接蓝牙设备的初始列表?如果在我的活动或服务启动时已经连接任何已连接,我将不知道.我想(希望)这个数据可以在哪里找到?我不想创建一个服务(只要手机打开就会持续运行)只是为了听取这些意图. (5认同)
  • 只要我的应用程序正在运行,这就很好,但是如何获得当前列表呢? (2认同)
  • 您打算如何在不“运行”应用程序的情况下执行代码?如果您的意思是您需要从 Activity 以外的其他东西访问它......去谷歌 Android 服务,构建其中一个来收听广播,并将其保存到列表中。 (2认同)

job*_*ert 23

在我的使用案例中,我只想查看是否为VoIP应用程序连接了蓝牙耳机.以下解决方案适合我:

public static boolean isBluetoothHeadsetConnected() {
    BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    return mBluetoothAdapter != null && mBluetoothAdapter.isEnabled()
            && mBluetoothAdapter.getProfileConnectionState(BluetoothHeadset.HEADSET) == BluetoothHeadset.STATE_CONNECTED;
} 
Run Code Online (Sandbox Code Playgroud)

当然你需要蓝牙许可:

<uses-permission android:name="android.permission.BLUETOOTH" />
Run Code Online (Sandbox Code Playgroud)

  • 这是我寻求的那个.只是为了检查启动蓝牙是否已连接.谢谢它的工作! (2认同)

pmo*_*ont 11

非常感谢Skylarsutton的回答.我发布此作为对他的回复,但因为我发布的代码我无法回复评论.我已经提出了他的答案,所以我不是在寻找任何积分.只需支付它.

由于某些原因,Android Studio无法解析BluetoothAdapter.ACTION_ACL_CONNECTED.也许它在Android 4.2.2中被弃用了?这是他的代码的修改.注册码是一样的; 接收者代码稍有不同.我在一个服务中使用它来更新应用程序的其他部分引用的蓝牙连接标志.

    public void onCreate() {
        //...
        IntentFilter filter1 = new IntentFilter(BluetoothDevice.ACTION_ACL_CONNECTED);
        IntentFilter filter2 = new IntentFilter(BluetoothDevice.ACTION_ACL_DISCONNECT_REQUESTED);
        IntentFilter filter3 = new IntentFilter(BluetoothDevice.ACTION_ACL_DISCONNECTED);
        this.registerReceiver(mReceiver, filter1);
        this.registerReceiver(mReceiver, filter2);
        this.registerReceiver(mReceiver, filter3);
    }

    //The BroadcastReceiver that listens for bluetooth broadcasts
    private final BroadcastReceiver BTReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();

        if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)) {
            //Do something if connected
            Toast.makeText(getApplicationContext(), "BT Connected", Toast.LENGTH_SHORT).show();
        }
        else if (BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(action)) {
            //Do something if disconnected
            Toast.makeText(getApplicationContext(), "BT Disconnected", Toast.LENGTH_SHORT).show();
        }
        //else if...
    }
};
Run Code Online (Sandbox Code Playgroud)


小智 8

https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/bluetooth/BluetoothDevice.java中的BluetoothDevice 系统API 中有一个isConnected函数。

如果您想知道当前是否连接了有界(配对)设备,以下功能对我来说很好用:

public static boolean isConnected(BluetoothDevice device) {
    try {
        Method m = device.getClass().getMethod("isConnected", (Class[]) null);
        boolean connected = (boolean) m.invoke(device, (Object[]) null);
        return connected;
    } catch (Exception e) {
        throw new IllegalStateException(e);
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 谢谢,正是我需要的!这是此方法的 kotlin 等效项: `fun isConnected(device: BluetoothDevice): Boolean { return try { val m: Method = device.javaClass.getMethod( "isConnected" ) m.invoke(device) as Boolean } catch (e : 异常) { 抛出 IllegalStateException(e) } }` (3认同)