【问题标题】:Toggling A2DP Device (Android)切换 A2DP 设备 (Android)
【发布时间】:2012-10-12 10:40:04
【问题描述】:

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

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

谁能指出我正确的方向?基本上我想做的就是选中“用于媒体音频”框。

【问题讨论】:

  • 这是什么手机,nexus 系列设备上是否存在设置?
  • 我使用的手机是带有 AT&T 的 Galaxy S3。我不能确定它是否会出现在那里,但我想它会出现。

标签: java android bluetooth a2dp


【解决方案1】:

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

首先,您需要在项目中创建一个名为 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 设备,请从配对设备列表中选择一个 BluetoothDevice 并将其作为connect 方法的参数发送。请务必选择具有正确配置文件的设备,否则您将遇到异常。

我已经在安卓 2.3 的手机上测试了这个解决方案,效果很好。

抱歉,有任何英文错误。我希望这可以帮助你。

【讨论】:

  • 我没有看到“Stub”在 IBluetoothA2dp 类型中的实现位置,请澄清一下?
【解决方案2】:

首先您需要设置程序以激活手机上的蓝牙并选择要与之配对的设备

bluetoothAdapter.disable() / enable() 

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

那你应该设置A2DP连接汽车音响

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

hidden & internal api's

【讨论】:

    猜你喜欢
    • 2017-08-06
    • 1970-01-01
    • 2015-03-02
    • 1970-01-01
    • 2012-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多