4.3 android bluetooth hfp分析

本文主要探讨了在Android系统中,当蓝牙设备(如蓝牙耳机)已连接时,如何通过设置界面操作来触发蓝牙耳机的连接流程。从Settings应用的源代码开始,经过LocalBluetoothProfile的子类HeadsetProfile,再到BluetoothHeadset,然后通过Binder机制进入Bluetooth模块。连接过程涉及HeadsetStateMachine的状态机,最终在btif_hf.c中实现Handsfree的连接。整个过程中,GKI_send_msg用于在不同组件间传递消息。
摘要由CSDN通过智能技术生成

所有程序执行的代码都是有入口的,在这里我们暂时分析一种情景,蓝牙打开着,蓝牙耳机连接。

在设置界面点击蓝牙耳机操作:

packages/apps/Settings/src/com/android/settings/bluetooth/BluetoothDevicePreference.java

    void onClicked() {
        int bondState = mCachedDevice.getBondState();

        if (mCachedDevice.isConnected()) {
            askDisconnect();
        } else if (bondState == BluetoothDevice.BOND_BONDED) { //已经配对,但是未连接
            mCachedDevice.connect(true);
        } else if (bondState == BluetoothDevice.BOND_NONE) { //没有配对
            pair();
        }
    }
	
mCachedDevice.connect(true);方法会直接调用CachedBluetoothDevice.java的connect的方法。

packages/apps/Settings/src/com/android/settings/bluetooth/CachedBluetoothDevice.java

   void connect(boolean connectAllProfiles) {
        if (!ensurePaired()) {  //配对处理暂时不关注
            return;
        }
        mConnectAttempted = SystemClock.elapsedRealtime();
        connectWithoutResettingTimer(connectAllProfiles);
    }
代码执行到connectWithoutResettingTimer,注释就不粘贴了。 
    private void connectWithoutResettingTimer(boolean connectAllProfiles) {
         ......
        // Reset the only-show-one-error-dialog tracking variable
        mIsConnectingErrorPossible = true;
        int preferredProfiles = 0;
        for (LocalBluetoothProfile profile : mProfiles) {
            if (connectAllProfiles ? profile.isConnectable() : profile.isAutoConnectable()) {
                if (profile.isPreferred(mDevice)) {
                    ++preferredProfiles;
                    connectInt(profile);
                }
            }
        }
        if (DEBUG) Log.d(TAG, "Preferred profiles = " + preferredProfiles);

        if (preferredProfiles == 0) {
            connectAutoConnectableProfiles();
        }
    }

不同的协议实现类,继承于LocalBluetoothProfile,以HeadsetProfile为例。

    synchronized void 
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值