HFP(Hands Free Profile)和 A2DP (Advanced Audio Distribution Profile) 是經典藍牙常用的兩個協議。
HFP 協議一般用來支持耳機打電話、接電話、掛斷電話、拒接電話等操作。
A2DP 協議一般用來聽歌,傳輸音頻立體聲。
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;}
通過反射調用 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;}
通過反射調用 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;}
通過 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藍牙協議怎么樣、通過 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";
版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。
工作时间:8:00-18:00
客服电话
电子邮件
admin@qq.com
扫码二维码
获取最新动态