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

标签 android reflection bluetooth headset

我需要检查当前在 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;
}

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

最佳答案

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

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

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

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

关于android - 在 Android API 中调用私有(private)(未发布)方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6020421/

相关文章:

c# - 如果在运行时只知道类型参数,如何调用泛型方法?

ios - 带有 iBeacon 的位置

android - BluetoothLE BluetoothAdapter:startLeScan() : null

android - 打开外部 XML 文件

java - 从我的自定义图库 android 中选择图像时出错

android - Surface和Canvas的关系 : Android

c# - 了解 'Attributes' 的使用

java - 代码生成: create interface for class

android - 如何在android中通过蓝牙向配对设备发送短信?

java - 如何处理像 android.content.res.Resources$NotFoundException : String resource ID #0x0 这样的错误