如何使用反射在Android 4.2上连接配对的蓝牙A2dp设备?

如何使用反射在Android 4.2上连接配对的蓝牙A2dp设备?,android,reflection,bluetooth,device,Android,Reflection,Bluetooth,Device,我只需按下按钮即可连接已定义的BT设备。 要求是在使用标准套接字方法的情况下,用户不应收到任何通知对话框。 在我的项目中,我使用了。 接下来是代码: /** * Return system service to work with A2DP * * @return bluetooth interface */ private static IBluetoothA2dp getIBluetoothA2dp() { IBluetoothA2dp ibta = null; tr


我只需按下按钮即可连接已定义的BT设备。
要求是在使用标准套接字方法的情况下,用户不应收到任何通知对话框。
在我的项目中,我使用了。 接下来是代码:

/**
 * 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;
}
在我在安卓4.2上发布我的应用程序之前,它一直运行良好。现在我无法获取IBluetoothA2dp接口,因为getService()方法不会返回带有“bluetooth_a2dp”键的IBinder。

有人能帮我吗?


提前谢谢

终于在4.2上实现了这一点。详情如下:

它与4.1及之前的版本有很大不同

第一次调用如下所示连接到接口:

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

}
当接口返回时,它将回调:

    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是IBluetoothA2dp接口

另一方面,IBluetooth接口也发生了变化。我使用它来获取设备别名(用户可以编辑的名称)。此getRemoteAlias()函数以前需要mac地址。现在需要一个蓝牙设备


请记住,使用这些隐藏的界面是有风险的,因为它们可能会,而且在新的Android版本中经常会发生变化。我已经被这咬了好几次了。你真的需要掌握它

如果有人需要有关自动播放的答案,可以在这里查看我的答案。

你不需要对此进行反思,是吗?这是一个黑客行为,可能不可靠。事实上,我需要反思。我有一个避免任何对话框和通知的要求,这就是为什么我使用这个黑客。是的,它确实不可靠,但它从2.3运行到4.1没有任何问题,直到谷歌改变了BT堆栈。你在谈论哪些对话框?看看我知道的关于它的第二条评论,这就是为什么我要问这个问题。也许有人知道克服这个问题的新方法,并能解释如何使用新机制。