3


I need to connect defined BT device by simple pressing the button.
The requirement is user shouldn't receive any notification dialogs as in case of using standard socket methods.
In my project I used this solution. Code was next:

/**
 * 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've launched my app on Android 4.2. Now I'm unable to get IBluetoothA2dp interface because getService() method doesn't return me an IBinder with "bluetooth_a2dp" key.

Can someone help me?

Thanks in advance!

Community
  • 1
  • 1
dmitriyzaitsev
  • 716
  • 9
  • 17
  • You don't need reflection for this, do you? It's a hack and potentially unreliable. – Mister Smith Feb 05 '13 at 11:08
  • Actually I need reflection. I have a requirement to avoid any dialogs and notifications, that's why I used this hack. Yes, it's definitely unreliable but it worked from 2.3 to 4.1 without any problems until Google changed BT stack. – dmitriyzaitsev Feb 05 '13 at 11:15
  • Which dialogs are you talking about? – Mister Smith Feb 05 '13 at 11:16
  • Have a look at the second comment [in this answer](http://stackoverflow.com/a/7680763/813951) – Mister Smith Feb 05 '13 at 11:26
  • I know about it, that's why I'm asking this question. Maybe someone knows new way to overcome this problem and can explain how use new mechanism. – dmitriyzaitsev Feb 05 '13 at 11:35
  • I am having the same problem getting an interface to IBluetoothA2dp using an IADL. I did this and it all worked good until 4.2 I have my investigation documented here: http://code.google.com/p/a2dp-connect/issues/detail?id=11 . – jroal Feb 16 '13 at 17:01

2 Answers2

1

Finally got this working on 4.2. See the details here: http://code.google.com/p/a2dp-connect2/

It is quite different from 4.1 and before.

First call connect 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 is returned it will call back to this:

    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.

On a related note, IBluetooth interface also changed. I use this to get the device alias name (the one the user can edit). This getRemoteAlias() function needed the mac address before. Now it takes a BluetoothDevice.

Keep in mind using these hidden interfaces is risky as they can and too often do change with new Android versions. I have been bit by that several times now. You really need to stay on top of it.

jroal
  • 547
  • 6
  • 16
0

If some one needs the answer to something related to autopairing, can check my answer here. https://stackoverflow.com/a/30362554/3920157

Community
  • 1
  • 1
Rodolfo Abarca
  • 565
  • 7
  • 15