Android 蓝牙 hfp音频连接

本文详细介绍了Android设备通过蓝牙HFP协议进行音频连接的过程,包括连接音频时的源代码路径,如BluetoothRouteManager、BluetoothDeviceManager和HeadsetStateMachine等。在音频连接状态改变时,系统会回调com_android_bluetooth_hfp.cpp的AudioStateCallback函数,并通过messageFromNative传递给状态机,最终在AudioConnecting状态下完成音频连接,转变为mAudioOn状态,并发送ACTION_AUDIO_STATE_CHANGED广播。
摘要由CSDN通过智能技术生成

Android 蓝牙 hfp音频连接


该文章基于Android Q

1 连接音频

在手机音频正常连接时,接通电话,点选蓝牙通话。mDeviceManager.connectAudio返回true。
如果是之前默认蓝牙mDeviceManager.connectAudio返回false。
packages/services/Telecomm/src/com/android/server/telecom/bluetooth/BluetoothRouteManager.java

    private String connectBtAudio(String address, int retryCount) {
   
        ......
        if (!mDeviceManager.connectAudio(actualAddress)) {
   
            boolean shouldRetry = retryCount < MAX_CONNECTION_RETRIES;
            Log.w(LOG_TAG, "Could not connect to %s. Will %s", actualAddress,
                    shouldRetry ? "retry" : "not retry");
            if (shouldRetry) {
   
                SomeArgs args = SomeArgs.obtain();
                args.arg1 = Log.createSubsession();
                args.arg2 = actualAddress;
                args.argi1 = retryCount + 1;
                sendMessageDelayed(RETRY_HFP_CONNECTION, args,
                        mTimeoutsAdapter.getRetryBluetoothConnectAudioBackoffMillis(
                                mContext.getContentResolver()));
            }
            return null;
        }

        return actualAddress;
    }

packages/services/Telecomm/src/com/android/server/telecom/bluetooth/BluetoothDeviceManager.java

    public boolean connectAudio(String address) {
   
        if (mHearingAidDevicesByAddress.containsKey(address)) {
   
            ......
        } else if (mHfpDevicesByAddress.containsKey(address)) {
   
            BluetoothDevice device = mHfpDevicesByAddress.get(address);
            if (mBluetoothHeadsetService == null) {
   
                Log.w(this, "Attempting to turn on audio when the headset service is null");
                return false;
            }
            boolean success = mBluetoothHeadsetService.setActiveDevice(device);
            if (!success) {
   
                Log.w(this, "Couldn't set active device to %s", address);
                return false;
            }
            if (!mBluetoothHeadsetService.isAudioOn()) {
   
                return mBluetoothHeadsetService.connectAudio();
            }
            return true;
        } else {
   
            Log.w(this, "Attempting to turn on audio for a disconnected device");
            return false;
        }
    }

packages/services/Telecomm/src/com/android/server/telecom/BluetoothHeadsetProxy.java

    public boolean connectAudio() {
   
        return mBluetoothHeadset.connectAudio();
    }

frameworks/base/core/java/android/bluetooth/BluetoothHeadset.java

    public boolean connectAudio() {
   
        final IBluetoothHeadset service = mService;
        if (service != null && isEnabled()) {
   
            try {
   
                return service.connectAudio();
            } catch (RemoteException e) {
   
                Log.e(
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值