简体   繁体   English

拨动A2DP设备(Android)

[英]Toggling A2DP Device (Android)

I have two paired bluetooth devices (my car's head-unit for phone audio and a separate bluetooth receiver for A2DP). 我有两个配对的蓝牙设备(我的汽车用于手机音频的头部装置和用于A2DP的独立蓝牙接收器)。 On my phone there's a checkbox for "Use for media audio" that I have to manually toggle for my A2DP output to go to my car's speakers. 在我的手机上有一个“用于媒体音频”的复选框,我必须手动切换我的A2DP输出才能转到我的汽车扬声器。 My goal is to toggle this programmatically. 我的目标是以编程方式切换。

I tried using both the AudioManager class with the deprecated setBluetoothA2dpOn and the setBluetoothScoOn but neither seemed to have any effect. 我尝试使用AudioManager类和不推荐使用的setBluetoothA2dpOn以及setBluetoothScoOn,但似乎都没有任何效果。 I was able to get a list of the bluetooth paired devices and get a handle to the connection I want to toggle but I couldn't seem to get it quite right. 我能够获得蓝牙配对设备的列表,并获得我想要切换的连接的句柄,但我似乎无法让它完全正确。 I also tried getting the default bluetooth adapter and then using getProfileProxy but I feel like I'm barking up the wrong tree there. 我也尝试使用默认的蓝牙适配器,然后使用getProfileProxy,但我觉得我在那里咆哮错误的树。

Can anyone point me in the right direction? 谁能指出我正确的方向? Basically all I want to do is the equivalent of checking that "Use for media audio" box. 基本上我想做的就是检查“用于媒体音频”框。

A short time ago I had a similar problem trying connect a bluetooth device to android phone. 不久前我尝试将蓝牙设备连接到Android手机时遇到了类似的问题。 Although your device profile being different, I think the solution is the same. 虽然您的设备配置文件不同,但我认为解决方案是相同的。

First you need create a package in your project named android.bluetooth and put the following IBluetoothA2dp.aidl in there: 首先,您需要在名为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);
}

Then, to access those functionalities, put the following class in your project: 然后,要访问这些功能,请在项目中添加以下类:

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

} }

Finally, to connect your A2dp device, pick one BluetoothDevice from a list of paired devices and send it as parameter of connect method. 最后,要连接A2dp设备,从配对设备列表中选择一个BluetoothDevice并将其作为connect方法的参数发送。 Be sure to pick a device with the correct profile, otherwise you will have an exception. 请务必选择具有正确配置文件的设备,否则您将遇到例外情况。

I have tested this solution in a phone with android version 2.3 and it worked fine. 我已经在Android 2.3版的手机中测试了这个解决方案,它运行良好。

Sorry any English mistake. 对不起任何英文错误。 I hope this can help you. 我希望这可以帮到你。

First you need to set the program to activate the Bluetooth on the phone and select the device that it should pair with via 首先,您需要设置程序以激活手机上的蓝牙,然后选择应与之配对的设备

bluetoothAdapter.disable() / enable() 

(im not sure about the pairing but this must be done via some config activity) (我不确定配对,但必须通过一些配置活动来完成)

Then you should set A2DP to connect to the car's stereo 然后你应该设置A2DP连接到汽车的立体声

follow this link to try and find the code for it, if i have time i will try and find it for you but its a start =] 按照这个链接尝试找到它的代码,如果我有时间我会尝试找到它,但它的开始=]

hidden & internal api's 隐藏和内部api

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

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