切换A2DP设备(安卓) [英] Toggling A2DP Device (Android)
问题描述
我有两个配对的蓝牙设备(我的车头上,单位电话音频和一个独立的蓝牙接收器支持A2DP)。我的手机上有一个为用于媒体音频,我必须手动切换为我A2DP输出去我的车的喇叭复选框。我的目标是通过编程方式切换此。
I have two paired bluetooth devices (my car's head-unit for phone audio and a separate bluetooth receiver for 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. My goal is to toggle this programmatically.
我试图同时使用AudioManager类去precated setBluetoothA2dpOn和setBluetoothScoOn但是他们俩谁也没有任何效果。我能得到的蓝牙配对设备列表,并得到一个处理我想切换连接,但我似乎无法得到它完全正确。我也尝试获取默认的蓝牙适配器,然后使用getProfileProxy,但我觉得我找错了树那里。
I tried using both the AudioManager class with the deprecated setBluetoothA2dpOn and the setBluetoothScoOn but neither seemed to have any effect. 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.
任何人都可以点我在正确的方向?基本上所有我想要做的是检查用于媒体音频中的等价物。
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.
推荐答案
一个很短的时间以前,我也有类似的问题,试图蓝牙设备连接到Android手机。虽然你的设备配置文件是不同的,我认为解决的办法是一样的。
A short time ago I had a similar problem trying connect a bluetooth device to android phone. Although your device profile being different, I think the solution is the same.
首先,你需要创建一个包在你的项目名为 android.bluetooth
,把下面的 IBluetoothA2dp.aidl 在那里:
First you need create a package in your project named android.bluetooth
and put the following IBluetoothA2dp.aidl in there:
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;
}
}
最后,连接您的A2DP设备,选择一个BluetoothDevice类从配对设备列表,并将其作为的参数连接
方法来发送。一定要选择一个设备与正确的配置文件,否则就会产生异常。
Finally, to connect your A2dp device, pick one BluetoothDevice from a list of paired devices and send it as parameter of connect
method. Be sure to pick a device with the correct profile, otherwise you will have an exception.
我已经测试了这种解决方案在手机中采用Android 2.3版本,它工作得很好。
I have tested this solution in a phone with android version 2.3 and it worked fine.
对不起任何英语的错误。我希望这可以帮助你。
Sorry any English mistake. I hope this can help you.
这篇关于切换A2DP设备(安卓)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!