首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >切换A2DP设备(安卓)

切换A2DP设备(安卓)
EN

Stack Overflow用户
提问于 2012-10-22 23:19:19
回答 2查看 4.5K关注 0票数 12

我有两个成对的蓝牙设备(我的汽车音响主机和一个用于A2DP的单独蓝牙接收器)。在我的手机上有一个“用于媒体音频”的复选框,我必须手动切换才能将我的A2DP输出发送到我汽车的扬声器。我的目标是以编程的方式切换这一点。

我尝试将AudioManager类与不推荐使用的setBluetoothA2dpOn和setBluetoothScoOn一起使用,但似乎都没有任何效果。我能够获得蓝牙配对设备的列表,并获得我想要切换的连接的句柄,但我似乎无法完全正确地完成。我也尝试了获取默认的蓝牙适配器,然后使用getProfileProxy,但我觉得我找错了地方。

有谁能给我指个方向吗?基本上,我想做的就是勾选“用于媒体音频”框。

EN

回答 2

Stack Overflow用户

发布于 2012-11-01 02:45:46

不久前,我尝试将蓝牙设备连接到android手机时遇到了类似的问题。虽然你的设备配置文件不同,但我认为解决方案是相同的。

首先,您需要在项目中创建一个名为android.bluetooth的包,并将以下IBluetoothA2dp.aidl放入其中:

代码语言:javascript
复制
package android.bluetooth;

import android.bluetooth.BluetoothDevice;

/**
 * System private API for Bluetooth A2DP service
 *
 * {@hide}
 */
interface IBluetoothA2dp {
    boolean connectSink(in BluetoothDevice device);
    boolean disconnectSink(in BluetoothDevice device);
    boolean suspendSink(in BluetoothDevice device);
    boolean resumeSink(in BluetoothDevice device);
    BluetoothDevice[] getConnectedSinks(); 
    BluetoothDevice[] getNonDisconnectedSinks();
    int getSinkState(in BluetoothDevice device);
    boolean setSinkPriority(in BluetoothDevice device, int priority);
    int getSinkPriority(in BluetoothDevice device);

    boolean connectSinkInternal(in BluetoothDevice device);
    boolean disconnectSinkInternal(in BluetoothDevice device);
}

然后,要访问这些功能,请将以下类放入项目中:

代码语言:javascript
复制
public class BluetoothA2dpConnection {

private IBluetoothA2dp mService = null;

public BluetoothA2dpConnection() {

    try {
        Class<?>  classServiceManager = Class.forName("android.os.ServiceManager");
        Method methodGetService = classServiceManager.getMethod("getService", String.class);
        IBinder binder = (IBinder) methodGetService.invoke(null, "bluetooth_a2dp");
        mService = IBluetoothA2dp.Stub.asInterface(binder); 
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (SecurityException e) {
        e.printStackTrace();
    } catch (NoSuchMethodException e) {
        e.printStackTrace();
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    } catch (InvocationTargetException e) {
        e.printStackTrace();
    }
}

public boolean connect(BluetoothDevice device) {
    if (mService == null || device == null) {
        return false;
    }
    try {
        mService.connectSink(device);
    } catch (RemoteException e) {
        e.printStackTrace();
        return false;
    }
    return true;
}

public boolean disconnect(BluetoothDevice device) {
    if (mService == null || device == null) {
        return false;
    }
    try {
        mService.disconnectSink(device);
    } catch (RemoteException e) {
        e.printStackTrace();
        return false;
    }
    return true;
}

}

最后,为了连接您的A2dp设备,从配对的设备列表中选择一个BluetoothDevice,并将其作为connect方法的参数发送。请务必选择具有正确配置文件的设备,否则将出现异常。

我在安卓2.3版本的手机上测试了这个解决方案,它工作得很好。

抱歉,有任何英语错误。我希望这能对你有所帮助。

票数 4
EN

Stack Overflow用户

发布于 2012-10-31 21:44:35

首先,您需要将程序设置为激活电话上的蓝牙,并通过选择与之配对的设备

代码语言:javascript
复制
bluetoothAdapter.disable() / enable() 

(我不确定配对,但这必须通过一些配置活动来完成)

然后你应该设置A2DP连接到汽车的立体声音响

点击这个链接来尝试找到它的代码,如果我有时间,我会试着为你找到它,但它是一个开始=]

hidden & internal api's

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13014509

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档