发现网上很少有蓝牙解决方案,特别是怎么控制蓝牙音频,没人介绍。我写了个apk,可以通过手机控制开发板的音频Codec,既可以同时传控制参数,又可以传输音频。
我的经验如下:
问题:利用官网的APP Sample,通过UUID实现了SPP通信,控制音频开发板,但是发现,只能控制开发板,却不能同时传输音频。
分析:A2DP是Android自带的profile,开发者不需要接触到UUID,但肯定也是通过UUID去建立Socket接口的,只不过不需要开发者维护这一块。上一篇有解释道A2DP的uuid是:
static final String SPP_UUID = "00001101-0000-1000-8000-00805F9B34FB";
static final String A2DP_SRC_UUID = "0000110A-0000-1000-8000-00805F9B34FB";
static final String A2DP_SINK_UUID = "0000110B-0000-1000-8000-00805F9B34FB";
《android蓝牙编程 重点知识 SPP A2DP UUID》:http://blog.csdn.net/xzongyuan/article/details/39319691
尝试:因此,问题就是怎么利用A2DP的API了,但是官网API只提供几个状态相关的API,没有操作,怎么办?查看BluetoothA2DP源码,发现hide了一个connect方法,利用java的反射机制,就可以调用这个方法,就可以让系统自动连接了,而且是系统自动管理音频,即你自己设计的APP中connect了这个Socket,在其它播放音乐的APP中播放音乐时,它会共用这个Socket。官网说这个API是通过IPC机制实现的,那个connect函数,实际上是调用了IBluetoothA2dp.aidl文件的接口,
interface IBluetoothA2dp {
// Public API
boolean connect(in BluetoothDevice device); 。。。。。。。。}
这个接口会根据你传入的BluetoothDevice去打开对应的蓝牙设备的A2DP服务,并自己读写Socket接口,传输音频。所以,调用A2DP profile,你就不需要读写Socket口了,直接打开就行,它会自动把本地音频传到远程蓝牙设备。
具体怎么实现,自己看代码吧,有注释了,就不详述了。
本文链接:http://blog.csdn.net/xzongyuan/article/details/39344953(Norton的专栏)
SPP部分
public class ConnectThread extends Thread{
String mAddr;
BluetoothDevice mBTDevInThread =null ;
public void setAddress(String pAddr){
mAddr = pAddr;
}
@Override
public void run() {
UUID uuid = UUID.fromString(SPP_UUID);
//获取蓝牙设备对象
if(mAddr!=null){
Log.e(TAG,"mAddr != null");
mBTDevInThread = mBTAdp.getRemoteDevice(mAddr);
} else {
Log.e(TAG,"mAddr = null");
mBTDevInThread = getBondDev();
}
Log.e(TAG, "ConnectThread");
try {
//获取Socket
if(mBTDevInThread!=null){
Log.e(TAG,"btDev != null");
//btDev.createBond();
mBTSocket = null;
mBTSocket = mBTDevInThread.createRfcommSocketToServiceRecord(uuid);
//btDev.connectGatt(mContext, true, null);
/*if(mBTAdp.getProfileConnectionState(BluetoothProfile.A2DP)!=BluetoothProfile.STATE_CONNECTED){
mBTAdp.getProfileProxy(mContext, new connServListener(), BluetoothProfile.A2DP);
}*/
}
else
{
Log.e(TAG,"btDev == null");
mUIHandler.sendEmptyMessage(NO_BINDING_DEV);
return;
}
//阻塞链接
if(mBTSocket!=null){
Log.e(TAG,"mBTSocket != null");
mUIHandler.sendEmptyMessage(SHOW_LOADING_BAR);
mBTSocket.connect();
connectA2DP();
}
} catch (IOException e) {
//Toast.makeText(mContext, "ConnectFailed", Toast.LENGTH_SHORT).show();
Log.e(TAG, "connected failed");
try {
if(mBTSocket!=null){
mBTSocket.close();
}
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
e.printStackTrace();
}
mUIHandler.sendEmptyMessage(CLOSE_LOADING_BAR);
//To control UI
Log.e(TAG, "renew checkbox status");
mUIHandler.sendEmptyMessage(CONNECT_STATUS_HANDLER);
}
A2DP部分,在connectA2DP()函数中实现,如下
private void connectA2DP() {
if(mBTAdp.getProfileConnectionState(BluetoothProfile.A2DP)!=BluetoothProfile.STATE_CONNECTED){
//在listener中完成A2DP服务的调用
mBTAdp.getProfileProxy(mContext, new connServListener(), BluetoothProfile.A2DP);
}
}
自定义一个回调函数connServerListener()
public class connServListener implements ServiceListener {
@Override
public void onServiceConnected(int profile, BluetoothProfile proxy) {
//use reflect method to get the Hide method "connect" in BluetoothA2DP
BluetoothA2dp a2dp = (BluetoothA2dp) proxy;
//a2dp.isA2dpPlaying(mBTDevInThread);
Class extends BluetoothA2dp> clazz = a2dp.getClass();
Method method_Connect;
//通过BluetoothA2DP隐藏的connect(BluetoothDevice btDev)函数,打开btDev的A2DP服务
try {
/*
* 1.Reflect this method
public boolean connect(BluetoothDevice device);
*
* 2.function definition
getMethod(String methodName, Class >... paramType)
*/
//1.这步相当于定义函数
method_Connect = clazz.getMethod("connect",BluetoothDevice.class);
//invoke(object receiver,object... args)
//2.这步相当于调用函数,invoke需要传入args:BluetoothDevice的实例
method_Connect.invoke(a2dp, mBTDevInThread);
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void onServiceDisconnected(int profile) {
// TODO Auto-generated method stub
}
}
}