相关疑难解决方法(0)

在Android API中调用私有(未发布)方法

我需要检查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;
}
Run Code Online (Sandbox Code Playgroud)

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

reflection android bluetooth headset

5
推荐指数
1
解决办法
3841
查看次数

标签 统计

android ×1

bluetooth ×1

headset ×1

reflection ×1