首页 > 解决方案 > 检查蓝牙适配器是否连接到可穿戴设备

问题描述

我正在开发一个 android 应用程序,我需要知道蓝牙何时与可穿戴设备连接。我尝试使用带有蓝牙适配器的方法,但效果不佳。我该如何编程这个方法?

public static boolean btConnected() {
    if( /*Bluetooth is connected*/ )
        return true;
    else
        return false;
}

标签: androidbluetooth

解决方案


在您的 Manifest.xml 中添加此权限

<uses-permission android:name="android.permission.BLUETOOTH" />

并创建一个方法来检查蓝牙是否已连接

private boolean isBluetootConnected() {
    BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    return mBluetoothAdapter != null && mBluetoothAdapter.isEnabled()
            && mBluetoothAdapter.getProfileConnectionState(BluetoothHeadset.HEADSET) == BluetoothHeadset.STATE_CONNECTED;
} 

推荐阅读