[Android源码分析]蓝牙打开流程分析——jni层之上的方方面面

本文主要分析Android蓝牙打开流程,从BluetoothEnabler的onCheckedChanged开始,探讨setBluetoothEnabled、bluetoothAdapter.enable函数,深入理解蓝牙状态变化的工作,包括状态转换广播处理、prepareBluetooth函数的执行,涉及JNI层的交互。分析了BluetoothEventManager、BluetoothAdapter.ACTION_STATE_CHANGED的receiver及其影响,以及BluetoothService中PrepareBluetooth的相关操作。
摘要由CSDN通过智能技术生成

         在前面的UI分析的文章中我们已经发现,其实不管是设置中的开关和fragment之后的开关最终都是关联到BluetoothEnabler中去的,所以,我们直接去看这个里面对于开关的处理,开关的处理当然就是onCheckedChanged这个函数了,哈哈~~直接分析。。

1、蓝牙打开的按键处理

public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        // Show toast message if Bluetooth is not allowed in airplane mode
//其实这个就是飞行模式打开的时候,就显示一个“正处于飞行模式下”
//不过这种情况很少遇到的,因为在飞行模式的时候,其实按钮时反灰的,基本走不到这里
//不过,在那个下拉菜单中点击打开蓝牙的时候还是会遇到的,大家可以去试试看
        if (isChecked &&
                !WirelessSettings.isRadioAllowed(mContext, Settings.System.RADIO_BLUETOOTH)) {
            Toast.makeText(mContext, R.string.wifi_in_airplane_mode, Toast.LENGTH_SHORT).show();
            // Reset switch to off
            buttonView.setChecked(false);
        }
//只要蓝牙是正常的,这里必然就不是null喽。。
        if (mLocalAdapter != null) {
            mLocalAdapter.setBluetoothEnabled(isChecked);
        }
		//把switch反灰,在打开的过程中,开关一直是灰的
        mSwitch.setEnabled(false);
    }

 

1.1 setBluetoothEnabled分析

         在按钮打开的时候,就是通过这个函数来和framework层的adapter进行交互的。

public void setBluetoothEnabled(boolean enabled) {
		//根据传入的enabled值,决定是打开还是关闭
		//我们这边必然就是调用enable了
		//具体分析见1.1.1
        boolean success = enabled
                ? mAdapter.enable()
                : mAdapter.disable();
		//这里就是设置状态为正在打开
		//注意的是这里success是enable或者disable的返回值,不是enabled的值哦,呵呵~~
        if (success) {
			//所以,会在enable或者disable返回后面设置状态。
			//当然这个返回并不是说蓝牙打开成功了,因为蓝牙打开的操作是异步的
			//详细见1.1.2
            setBluetoothStateInt(enabled
                ? BluetoothAdapter.STATE_TURNING_ON
                : BluetoothAdapter.STATE_TURNING_OFF);
        } else {
            if (Utils.V) {
                Log.v(TAG, "setBluetoothEnabled call, manager didn't return " +
                        "success for enabled: " + enabled);
            }
			//若是失败了,还是要同步一下状态的。
			//就是把adapter的状态和当前状态进行一下同步
            syncBluetoothState();
        }
}

1.1.1 bluetoothAdapterenable函数分析

                  其实我们会发现,这个函数最终还是调用的bluetoothService中的enable函数,所以,这里就不详细说了,直接去看bluetoothService中的enable

/** Bring up BT and persist BT on in settings */
    public boolean enable() {
			//这个没什么好说的,直接看下面
        return enable(true);
    }

    /**
     * Enable this Bluetooth device, asynchronously.
     * This turns on/off the underlying hardware.
     *
     * @param saveSetting If true, persist the new state of BT in settings
     * @return True on success (so far)
     */
		//注意这里是异步的打开蓝牙
    public synchronized boolean enable(boolean saveSetting) {
		//检查权限,这就是我们为什么要在AndroidManifest中加入各种权限了
        mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM,
                                                "Need BLUETOOTH_ADMIN permission");

        // Airplane mode can prevent Bluetooth radio from being turned on.
			//看是否是飞行模式打开了
        if (mIsAirplaneSensitive && isAirplaneModeOn() && !mIsAirplaneToggleable) {
            return false;
        }
//就是wifi和bt能否共存,若不能,则需要检查wifi是否打开,打开了就不能打开bt了,一般情况下,我们不会进入这样的case,后面类似的内容不再解释
        if (mBootCompleted && !supportBtWifiCoexit) {
            if (mWifiManager == null)
                mWifiManager = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);
            if (mWifiManager != null
                    && (WifiManager.WIFI_STATE_DISABLED != mWifiManager.getWifiState()
                    || WifiManager.WIFI_AP_STATE_DISABLED != mWifiManager.getWifiApState())) {
                return false;
            }
        }
	//发送USER_TURN_ON的msg,哈哈,这个我们在蓝牙状态机改变的那篇文章中已经相信解释过了,所以这里就和那边关联起来了。
        mBl
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值