How to pair a paired A2dp bluetooth device on Android 4.2 using reflection?


I need to connect a specific BT device with a simple push of a button.
Requirement - The user should not receive any notification dialogs, as in the case of using standard socket methods.
I used this solution in my project . The following code:

/**
 * Return system service to work with A2DP
 *
 * @return bluetooth interface
 */
private static IBluetoothA2dp getIBluetoothA2dp() {
    IBluetoothA2dp ibta = null;
    try {
        final Class serviceManager = Class.forName("android.os.ServiceManager");
        final Method getService = serviceManager.getDeclaredMethod("getService", String.class);
        final IBinder iBinder = (IBinder) getService.invoke(null, "bluetooth_a2dp");
        final Class iBluetoothA2dp = Class.forName("android.bluetooth.IBluetoothA2dp");
        final Class[] declaredClasses = iBluetoothA2dp.getDeclaredClasses();
        final Class c = declaredClasses[0];
        final Method asInterface = c.getDeclaredMethod("asInterface", IBinder.class);

        asInterface.setAccessible(true);
        ibta = (IBluetoothA2dp) asInterface.invoke(null, iBinder);
    } catch (final Exception e) {
        Log.e("Error " + e.getMessage());
    }
    return ibta;
}

      

It worked well until I ran the app on Android 4.2. Now I cannot get the IBluetoothA2dp interface because the getService () method does not return an IBinder with the key "bluetooth_a2dp" to me.

Can anyone help me?

Thanks in advance!

+3


source to share


2 answers


Finally, it took 4.2. See here for details: http://code.google.com/p/a2dp-connect2/

It is very different from 4.1 and earlier.

The first call connects to the interface like this:

    public static void getIBluetoothA2dp(Context context) {

    Intent i = new Intent(IBluetoothA2dp.class.getName());

    if (context.bindService(i, mConnection, Context.BIND_AUTO_CREATE)) {

    } else {
        // Log.e(TAG, "Could not bind to Bluetooth A2DP Service");
    }

}

      

When the interface returns, it will fall back to the following:



    public static ServiceConnection mConnection = new ServiceConnection() {

    @Override
    public void onServiceConnected(ComponentName name, IBinder service) {

        ibta2 = IBluetoothA2dp.Stub.asInterface(service);
    }

    @Override
    public void onServiceDisconnected(ComponentName name) {
        // TODO Auto-generated method stub

    }

};

      

ibta2 above is the IBluetoothA2dp interface.

In a related note, the IBluetooth interface has also changed. I use this to get the device alias name (one that the user can edit). This getRemoteAlias ​​() function used to need a mac address. It now accepts a BluetoothDevice.

Keep in mind that using these hidden interfaces is risky as they can and change too often with newer versions of Android. Several times I was a little against it. You really need to stay on top.

+1


source


If anyone needs an answer to something related to autopilot, I can check my answer here. fooobar.com/questions/1995080 / ...



0


source







All Articles