發現網上很少有藍牙解決方案,特别是怎麼控制藍牙音頻,沒人介紹。我寫了個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檔案的接口,
[cpp] view plain copy
- 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部分
[cpp] view plain copy
- 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);
- }
- 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()函數中實作,如下
[cpp] view plain copy
- private void connectA2DP() {
- if(mBTAdp.getProfileConnectionState(BluetoothProfile.A2DP)!=BluetoothProfile.STATE_CONNECTED){
- //在listener中完成A2DP服務的調用
- mBTAdp.getProfileProxy(mContext, new connServListener(), BluetoothProfile.A2DP);
- }
- }
自定義一個回調函數connServerListener()
[cpp] view plain copy
- 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.這步相當于定義函數
- 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
- }
- }
- }
轉載自:http://blog.csdn.net/xzongyuan/article/details/39344953