android获取私有方法,在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 a班级的实例.

我使用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;

}

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值