bc8android藍牙連接手機,Android 藍牙 HFP 和 A2DP

 2023-12-12 阅读 29 评论 0

摘要:Android 藍牙 HFP 和 A2DP HFP(Hands Free Profile)和 A2DP (Advanced Audio Distribution Profile) 是經典藍牙常用的兩個協議。 HFP 協議一般用來支持耳機打電話、接電話、掛斷電話、拒接電話等操作。 A2DP 協議一般用來聽歌,傳輸音頻立體聲。 Profile

Android 藍牙 HFP 和 A2DP

HFP(Hands Free Profile)和 A2DP (Advanced Audio Distribution Profile) 是經典藍牙常用的兩個協議。

HFP 協議一般用來支持耳機打電話、接電話、掛斷電話、拒接電話等操作。

A2DP 協議一般用來聽歌,傳輸音頻立體聲。

ProfileProxy

Android SDK 使用 BluetoothAdapter 的 getProfileProxy 獲取每個具體的 Profile。

bc8android藍牙連接手機、獲取 HFP:

    public static boolean getHfpProfileProxy(Context context, BluetoothProfile.ServiceListener listener) {BluetoothAdapter defaultAdapter = BluetoothAdapter.getDefaultAdapter();boolean hfp = defaultAdapter.getProfileProxy(context, listener, BluetoothProfile.HEADSET);return hfp;}

獲取 A2DP:

    public static boolean getA2dpProfileProxy(Context context, BluetoothProfile.ServiceListener listener) {BluetoothAdapter defaultAdapter = BluetoothAdapter.getDefaultAdapter();boolean profileProxy = defaultAdapter.getProfileProxy(context, listener, BluetoothProfile.A2DP);return profileProxy;}

HFP 連接

通過反射調用 BluetoothHeadset 的 connect 方法連接 HFP。

    public static boolean connectHfp(BluetoothHeadset hfp, BluetoothDevice device) {boolean ret = false;Method connect = null;try {connect = BluetoothHeadset.class.getDeclaredMethod("connect", BluetoothDevice.class);connect.setAccessible(true);ret = (boolean) connect.invoke(hfp, device);} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {e.printStackTrace();}return ret;}

A2DP 連接

通過反射調用 BluetoothA2dp 的 connect 方法連接 A2DP。

    public static boolean connectA2dp(BluetoothA2dp a2dp, BluetoothDevice device) {boolean ret = false;Method connect = null;try {connect = BluetoothA2dp.class.getDeclaredMethod("connect", BluetoothDevice.class);connect.setAccessible(true);ret = (boolean) connect.invoke(a2dp, device);} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {e.printStackTrace();}return ret;}

HFP 狀態廣播

通過 BluetoothHeadset 的 ACTION_CONNECTION_STATE_CHANGED 監聽 HFP 連接狀態變化。

    /*** Intent used to broadcast the change in connection state of the Headset* profile.** <p>This intent will have 3 extras:* <ul>* <li> {@link #EXTRA_STATE} - The current state of the profile. </li>* <li> {@link #EXTRA_PREVIOUS_STATE}- The previous state of the profile. </li>* <li> {@link BluetoothDevice#EXTRA_DEVICE} - The remote device. </li>* </ul>* <p>{@link #EXTRA_STATE} or {@link #EXTRA_PREVIOUS_STATE} can be any of* {@link #STATE_DISCONNECTED}, {@link #STATE_CONNECTING},* {@link #STATE_CONNECTED}, {@link #STATE_DISCONNECTING}.** <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission to* receive.*/@SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)public static final String ACTION_CONNECTION_STATE_CHANGED ="android.bluetooth.headset.profile.action.CONNECTION_STATE_CHANGED";

A2DP 狀態廣播

a2dp藍牙協議怎么樣、通過 BluetoothA2dp 的 ACTION_CONNECTION_STATE_CHANGED 監聽 A2DP 連接狀態變化。

    /*** Intent used to broadcast the change in connection state of the A2DP* profile.** <p>This intent will have 3 extras:* <ul>* <li> {@link #EXTRA_STATE} - The current state of the profile. </li>* <li> {@link #EXTRA_PREVIOUS_STATE}- The previous state of the profile.</li>* <li> {@link BluetoothDevice#EXTRA_DEVICE} - The remote device. </li>* </ul>** <p>{@link #EXTRA_STATE} or {@link #EXTRA_PREVIOUS_STATE} can be any of* {@link #STATE_DISCONNECTED}, {@link #STATE_CONNECTING},* {@link #STATE_CONNECTED}, {@link #STATE_DISCONNECTING}.** <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission to* receive.*/@SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)public static final String ACTION_CONNECTION_STATE_CHANGED ="android.bluetooth.a2dp.profile.action.CONNECTION_STATE_CHANGED";

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://808629.com/196260.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 86后生记录生活 Inc. 保留所有权利。

底部版权信息