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

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 综合教程 >内容正文

综合教程

Android 蓝牙基础篇之 —— A2DP

發(fā)布時(shí)間:2023/12/3 综合教程 46 生活家
生活随笔 收集整理的這篇文章主要介紹了 Android 蓝牙基础篇之 —— A2DP 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

本篇文章主要介紹 A2DP 基礎(chǔ)操作。

  • 介紹

A2DP :Advanced Audio Distribution Profile。高質(zhì)量音頻數(shù)據(jù)傳輸?shù)膮f(xié)議,其定義里了傳送單聲道或立體聲等高質(zhì)量音頻(區(qū)別于藍(lán)牙SCO鏈路上傳輸?shù)钠胀ㄕZ(yǔ)音)信息的協(xié)議和過(guò)程。A2DP的典型應(yīng)用是將音樂(lè)播放器的音頻數(shù)據(jù)發(fā)送到耳機(jī)或音箱。

A2DP 定義了兩種角色:

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

?

  • 初始化 A2DP 代理對(duì)象
  private void initBluetooth() {mBtAdapter = BluetoothAdapter.getDefaultAdapter();if (!mBtAdapter.isEnabled()) {return;}//獲取A2DP代理對(duì)象mBtAdapter.getProfileProxy(mContext, mListener, BluetoothProfile.A2DP);}private void initReceiver() {//廣播接收者監(jiān)聽狀態(tài)IntentFilter filter = new IntentFilter(BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED);filter.addAction(BluetoothA2dp.ACTION_PLAYING_STATE_CHANGED);mContext.registerReceiver(mReceiver, filter);}
  • 初始化 A2DP 代理對(duì)象
  private void initBluetooth() {mBtAdapter = BluetoothAdapter.getDefaultAdapter();if (!mBtAdapter.isEnabled()) {return;}//獲取A2DP代理對(duì)象mBtAdapter.getProfileProxy(mContext, mListener, BluetoothProfile.A2DP);}private void initReceiver() {//廣播接收者監(jiān)聽狀態(tài)IntentFilter filter = new IntentFilter(BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED);filter.addAction(BluetoothA2dp.ACTION_PLAYING_STATE_CHANGED);mContext.registerReceiver(mReceiver, filter);}
  • 廣播接收者,獲取連接狀態(tài)
private BroadcastReceiver mReceiver = new BroadcastReceiver() {@Overridepublic void onReceive(Context context, Intent intent) {String action = intent.getAction();//A2DP連接狀態(tài)改變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播放狀態(tài)改變int state = intent.getIntExtra(BluetoothA2dp.EXTRA_STATE, BluetoothA2dp.STATE_NOT_PLAYING);Log.i(TAG, "play state=" + state);}}}};
  • 獲取 A2DP 代理對(duì)象 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; //轉(zhuǎn)換if (onBluetoothA2dpReadyListener != null) {onBluetoothA2dpReadyListener.onBluetoothA2dpReady();}}}};
  • 連接

?a2dp connect is hide? 需要通過(guò)反射獲取,連接成功之后,可以在藍(lán)牙設(shè)備中播放音樂(lè)等音頻

   public void connectA2dp(BluetoothDevice device) {Log.i(TAG, "connect to device :" + device);mConnectDevice = device;setPriority(device, 100); //設(shè)置prioritytry {//通過(guò)反射獲取BluetoothA2dp中connect方法(hide的),進(jìn)行連接。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 {//通過(guò)反射獲取BluetoothA2dp中connect方法(hide的),斷開連接。Method connectMethod = BluetoothA2dp.class.getMethod("disconnect",BluetoothDevice.class);connectMethod.invoke(mA2dp, device);} catch (Exception e) {e.printStackTrace();}}
  • 設(shè)置優(yōu)先級(jí),一般 priority = 100
    public void setPriority(BluetoothDevice device, int priority) {if (mA2dp == null) return;try {//通過(guò)反射獲取BluetoothA2dp中setPriority方法(hide的),設(shè)置優(yōu)先級(jí)Method connectMethod = BluetoothA2dp.class.getMethod("setPriority",BluetoothDevice.class, int.class);connectMethod.invoke(mA2dp, device, priority);} catch (Exception e) {e.printStackTrace();}}

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

?

?

?

總結(jié)

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

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。