日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 综合教程 >内容正文

综合教程

Android 蓝牙基础篇之 —— A2DP

發布時間:2023/12/3 综合教程 57 生活家
生活随笔 收集整理的這篇文章主要介紹了 Android 蓝牙基础篇之 —— A2DP 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

本篇文章主要介紹 A2DP 基礎操作。

  • 介紹

A2DP :Advanced Audio Distribution Profile。高質量音頻數據傳輸的協議,其定義里了傳送單聲道或立體聲等高質量音頻(區別于藍牙SCO鏈路上傳輸的普通語音)信息的協議和過程。A2DP的典型應用是將音樂播放器的音頻數據發送到耳機或音箱。

A2DP 定義了兩種角色:

Audio Source :(音頻源) 音頻的輸入端對音頻數據進行編碼,發送到Sink端。
Audio Sink : ? ? (音頻接收器) 接收到音頻數據后,進行解碼操作還原出音頻。

?

  • 初始化 A2DP 代理對象
  private void initBluetooth() {mBtAdapter = BluetoothAdapter.getDefaultAdapter();if (!mBtAdapter.isEnabled()) {return;}//獲取A2DP代理對象mBtAdapter.getProfileProxy(mContext, mListener, BluetoothProfile.A2DP);}private void initReceiver() {//廣播接收者監聽狀態IntentFilter filter = new IntentFilter(BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED);filter.addAction(BluetoothA2dp.ACTION_PLAYING_STATE_CHANGED);mContext.registerReceiver(mReceiver, filter);}
  • 初始化 A2DP 代理對象
  private void initBluetooth() {mBtAdapter = BluetoothAdapter.getDefaultAdapter();if (!mBtAdapter.isEnabled()) {return;}//獲取A2DP代理對象mBtAdapter.getProfileProxy(mContext, mListener, BluetoothProfile.A2DP);}private void initReceiver() {//廣播接收者監聽狀態IntentFilter filter = new IntentFilter(BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED);filter.addAction(BluetoothA2dp.ACTION_PLAYING_STATE_CHANGED);mContext.registerReceiver(mReceiver, filter);}
  • 廣播接收者,獲取連接狀態
private BroadcastReceiver mReceiver = new BroadcastReceiver() {@Overridepublic void onReceive(Context context, Intent intent) {String action = intent.getAction();//A2DP連接狀態改變if (action != null) {if (action.equals(BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED)) {int state = intent.getIntExtra(BluetoothA2dp.EXTRA_STATE, BluetoothA2dp.STATE_DISCONNECTED);callBackA2dpConnectState(state);} else if (action.equals(BluetoothA2dp.ACTION_PLAYING_STATE_CHANGED)) {//A2DP播放狀態改變int state = intent.getIntExtra(BluetoothA2dp.EXTRA_STATE, BluetoothA2dp.STATE_NOT_PLAYING);Log.i(TAG, "play state=" + state);}}}};
  • 獲取 A2DP 代理對象 proxy?
  private BluetoothProfile.ServiceListener mListener = new BluetoothProfile.ServiceListener() {@Overridepublic void onServiceDisconnected(int profile) {Log.i(TAG, "onServiceDisconnected profile=" + profile);if (profile == BluetoothProfile.A2DP) {mA2dp = null;}}@Overridepublic void onServiceConnected(int profile, BluetoothProfile proxy) {Log.i(TAG, "onServiceConnected profile=" + profile);if (profile == BluetoothProfile.A2DP) {mA2dp = (BluetoothA2dp) proxy; //轉換if (onBluetoothA2dpReadyListener != null) {onBluetoothA2dpReadyListener.onBluetoothA2dpReady();}}}};
  • 連接

?a2dp connect is hide? 需要通過反射獲取,連接成功之后,可以在藍牙設備中播放音樂等音頻

   public void connectA2dp(BluetoothDevice device) {Log.i(TAG, "connect to device :" + device);mConnectDevice = device;setPriority(device, 100); //設置prioritytry {//通過反射獲取BluetoothA2dp中connect方法(hide的),進行連接。Method connectMethod = BluetoothA2dp.class.getMethod("connect",BluetoothDevice.class);connectMethod.invoke(mA2dp, device);} catch (Exception e) {e.printStackTrace();}}
  • 斷開連接
 public void disConnectA2dp(BluetoothDevice device) {mConnectDevice = null;setPriority(device, 0);try {//通過反射獲取BluetoothA2dp中connect方法(hide的),斷開連接。Method connectMethod = BluetoothA2dp.class.getMethod("disconnect",BluetoothDevice.class);connectMethod.invoke(mA2dp, device);} catch (Exception e) {e.printStackTrace();}}
  • 設置優先級,一般 priority = 100
    public void setPriority(BluetoothDevice device, int priority) {if (mA2dp == null) return;try {//通過反射獲取BluetoothA2dp中setPriority方法(hide的),設置優先級Method connectMethod = BluetoothA2dp.class.getMethod("setPriority",BluetoothDevice.class, int.class);connectMethod.invoke(mA2dp, device, priority);} catch (Exception e) {e.printStackTrace();}}

好了,到這里相信大家都明白了,HFP 和 A2DP 的操作流程基本一樣。

?

?

?

總結

以上是生活随笔為你收集整理的Android 蓝牙基础篇之 —— A2DP的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。