切换A2DP设备(Android)



我有两个配对的蓝牙设备(我的汽车的车顶单位用于电话音频和一个单独的A2DP蓝牙接收器)。在我的手机上,有一个复选框,用于"用于媒体音频",我必须手动为A2DP输出切换到我的汽车扬声器。我的目标是以编程方式切换此。

我尝试使用defered setBluetootha2dpon和setBluetoothScoon使用AudioManager类,但似乎都没有任何效果。我能够获取蓝牙配对设备的列表,并获得了要切换的连接的处理,但我似乎无法完全正确。我还尝试获得默认的蓝牙适配器,然后使用getProfileproxy,但我觉得我在吠叫错误的树。

有人可以指向正确的方向吗?基本上,我要做的就是相当于检查"用于媒体音频"框。

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

首先,您需要在项目名为android.bluetooth的项目中创建一个软件包,然后将以下 ibluetootha2dp.aidl 放在其中:

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

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

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设备,请从配对设备列表中选择一个蓝牙词,然后将其作为connect方法的参数发送。确保选择具有正确配置文件的设备,否则您将有一个例外。

我已经在使用Android 2.3的手机中测试了该解决方案,并且效果很好。

对不起,任何英语错误。我希望这可以帮助您。

首先,您需要设置程序以激活手机上的蓝牙,然后选择应与Via

配对的设备
bluetoothAdapter.disable() / enable() 

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

然后,您应该设置A2DP以连接到汽车的立体声

请按照此链接尝试找到它的代码,如果我有时间我会尝试为您找到它,但是它的开始=]

隐藏&amp;内部API的

最新更新