简体   繁体   English

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

[英]How connect paired bluetooth A2dp device on Android 4.2 using reflection?


I need to connect defined BT device by simple pressing the button. 我需要通过简单的按钮连接定义的BT设备。
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. 它运行良好,直到我在Android 4.2上启动我的应用程序。 Now I'm unable to get IBluetoothA2dp interface because getService() method doesn't return me an IBinder with "bluetooth_a2dp" key. 现在我无法获得IBluetoothA2dp接口,因为getService()方法没有返回带有“bluetooth_a2dp”键的IBinder。

Can someone help me? 有人能帮我吗?

Thanks in advance! 提前致谢!

Finally got this working on 4.2. 最后得到了4.2的工作。 See the details here: http://code.google.com/p/a2dp-connect2/ 请在此处查看详细信息: http//code.google.com/p/a2dp-connect2/

It is quite different from 4.1 and before. 它与4.1和之前完全不同。

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. 上面的ibta2是IBluetoothA2dp接口。

On a related note, IBluetooth interface also changed. 在相关的说明中,IBluetooth接口也发生了变化。 I use this to get the device alias name (the one the user can edit). 我用它来获取设备别名(用户可以编辑的名称)。 This getRemoteAlias() function needed the mac address before. 此getRemoteAlias()函数之前需要mac地址。 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. 请记住,使用这些隐藏的界面是有风险的,因为它们可以并且经常使用新的Android版本进行更改。 I have been bit by that several times now. 我现在已经好几次了。 You really need to stay on top of it. 你真的需要保持领先。

If some one needs the answer to something related to autopairing, can check my answer here. 如果有人需要回答与自动驾驶有关的事情,可以在这里查看我的答案。 https://stackoverflow.com/a/30362554/3920157 https://stackoverflow.com/a/30362554/3920157

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM