Android 检查蓝牙是否连接

标签 android bluetooth

我在 Internet 上搜索了很长时间,但找不到我要找的东西。

如果我的设备已经连接到蓝牙设备(/在我启动我的应用程序之前),我如何通过我的应用程序找到它。

我希望有类似 bool BluetoothAdapter.isPaired();

的东西

最佳答案

如果您只对建立与任意蓝牙设备的连接感兴趣,您可以使用 BluetoothAdapter.getProfileConnectionState(profile):

    adapter = BluetoothAdapter.getDefaultAdapter();
    if (adapter != null && adapter.isEnabled()) {
        int[] profiles = {BluetoothProfile.A2DP, BluetoothProfile.HEADSET, BluetoothProfile.HEALTH};
        boolean connectionExists = false;
        for (int profileId : profiles) {
            if (BluetoothAdapter.getProfileConnectionState(profileId) == 
                                    BluetoothProfile.STATE_CONNECTED) {
                connectionExists = true;
                break;
            }
        }
    }

关于Android 检查蓝牙是否连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38050396/

相关文章:

ios - 蓝牙 didConnect 或 didFailToConnect 在 iOS 11.1.2 中未调用 Xcode 9.2

c# - Windows Phone 8.1 中的信标 - 没有可能性?

android - 如何在 fragment 工具栏中设置菜单项?

android - 长按 ListView 后的操作(Android Studio)

javascript - android中的HTML anchor 链接

java - 方法 getApplicationContext() 未定义 - fragment 问题

安卓蓝牙 : Connect()/Disconnect()

java - Android 蓝牙 java.io.IOException : Connection refused?

Android Studio 1.5.1、Gradle 2.8,带移动设备和可穿戴设备,带免费/付费版本

ios - 我如何在不读取 BLE 特性的情况下知道它是否已加密?