5

我需要检查 OS 2.0 - 2.3 中当前连接了哪些 BT 耳机(不仅仅是配对)。这种功能直到 API 版本 11 才存在,其中引入了蓝牙耳机类。但在之前的 API 中已经存在一个名为 BluetoothHeadset 的类,但它不能公开访问。这是它的文档:http ://www.kiwidoc.com/java/l/x/android/android/9/p/android.bluetooth/c/BluetoothHeadset 。所以,我试图使用反射来调用“isConnected”方法,但我在反射方面非常糟糕,而且我得到了一个错误java.lang.IllegalArgumentException: object is not an instance of the class

我得到了一个使用 的配对设备列表BluetoothDevice.getBondedDevices(),我尝试isConnected()在每个设备上使用该方法。这是代码:

public boolean isBtDevConnected(BluetoothDevice btDev){
    boolean connected  = false;
    try {
        Class<?> BTHeadset = Class.forName("android.bluetooth.BluetoothHeadset");
        Method isConnected = BTHeadset.getMethod("isConnected", new Class[] {BluetoothDevice.class});
                connected = isConnected.invoke(BTHeadset, new Object[] {btDev});
            }
        }
    } catch (Exception e) {
        WriteToLog(e);
    }
    return connected;
}

我在调用该方法的行上遇到异常,但我不确定我做错了什么。

4

1 回答 1

0

BluetoothHeadset 是一个代理对象,用于通过 IPC 控制蓝牙耳机服务。

使用 getProfileProxy(Context, BluetoothProfile.ServiceListener, int) 获取 BluetoothHeadset 代理对象。使用 closeProfileProxy(int, BluetoothProfile) 关闭服务连接。

Android 一次仅支持连接一个蓝牙耳机。每种方法都受到相应权限的保护。

来源:http: //developer.android.com/reference/android/bluetooth/BluetoothHeadset.html

于 2013-02-03T12:47:53.840 回答