开发者

Invoking a private (unpublished) method in Android API

开发者 https://www.devze.com 2023-03-06 01:47 出处:网络
I need to check which BT headsets are currently connected (not just paired) in OS 2.0 - 2.3. Such functionality doesn\'t exist until API version 11, where a Bluetooth Headset class was introduced. But

I need to check which BT headsets are currently connected (not just paired) in OS 2.0 - 2.3. Such functionality doesn't exist until API version 11, where a Bluetooth Headset class was introduced. But there already existed a class called BluetoothHeadset in prior APIs, but it wasn't publicly accessible. Here's the documentation for it: http://www.kiwidoc.com/java/l/x/android/android/9/p/android.bluetooth/c/BluetoothHeadset. So, I was trying to use reflection to invoke the "isConnected" method, but I'm pretty horrible at reflection, and I'm getting an error java.lang.IllegalArgumentException: object is not an instance of the class.

I got a list of paired devices using BluetoothDevice.getBondedDevices(), and I try to use the isConnected() method on each one. Here's the code:

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

I get the exception on the line that invokes the method, but I'm not sure what I'm doing wrong.


BluetoothHeadset is a proxy object for controlling the Bluetooth Headset Service via IPC.

Use getProfileProxy(Context, BluetoothProfile.ServiceListener, int) to get the BluetoothHeadset proxy object. Use closeProfileProxy(int, BluetoothProfile) to close the service connection.

Android only supports one connected Bluetooth Headset at a time. Each method is protected with its appropriate permission.

source: http://developer.android.com/reference/android/bluetooth/BluetoothHeadset.html

0

精彩评论

暂无评论...
验证码 换一张
取 消