Android 藍牙 HFP 和 A2DP

Android 藍牙 HFP 和 A2DP

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

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

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

ProfileProxy

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

獲取 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 狀態廣播

通過 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";
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章