How to programmatically tell if a Bluetooth device is connected?

前端 未结 7 1551
耶瑟儿~
耶瑟儿~ 2020-11-22 13:14

I understand how to get a list of paired devices but how can I tell if they are connected?

It must be possible since I see them listed in my phone\'s Bluetooth devi

7条回答
  •  没有蜡笔的小新
    2020-11-22 13:36

    There is an isConnected function in BluetoothDevice system API in https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/bluetooth/BluetoothDevice.java

    If you want to know if the a bounded(paired) device is currently connected or not, the following function works fine for me:

    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);
        }
    }
    

提交回复
热议问题