Android Bluetooth 框架简读 <2>

本文简要分析了Android系统中开启蓝牙和扫描蓝牙的流程。从应用层的BluetoothSettings.java开始,通过BluetoothEnabler进入LocalBluetoothAdapter,再深入BluetoothManagerService,最后到达HAL层的bluedroid协议栈。Bluetooth工程在framework层与应用层间起桥梁作用,并管理蓝牙状态机。Bluetooth使能操作在bluedroid的bluetooth.c中执行。文章还提及LocalBluetoothProfileManager在设备UUID识别和广播注册方面的作用,蓝牙扫描流程类似。
摘要由CSDN通过智能技术生成

根据前一篇的思路大致走一遍.

先从应用层开始,Settings工程的蓝牙部分,这部分还是挺熟悉的,以前在某知名公司负责ROM开发时熟悉了.当然不同系统版本,代码可能有点不同,

设置蓝牙主界面BluetoothSettings.java,但是这里大概分析开启蓝牙流程和扫描蓝牙流程.

开启蓝牙流程:

直接进入BluetoothEnabler这个类查看.

public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        // Show toast message if Bluetooth is not allowed in airplane mode
        if (isChecked &&
                !WirelessSettings.isRadioAllowed(mContext, Settings.Global.RADIO_BLUETOOTH)) {
            Toast.makeText(mContext, R.string.wifi_in_airplane_mode, Toast.LENGTH_SHORT).show();
            // Reset switch to off
            buttonView.setChecked(false);
        }

        if (mLocalAdapter != null) {
            mLocalAdapter.setBluetoothEnabled(isChecked);
        }
        mSwitch.setEnabled(false);
    }

一句:

mLocalAdapter.setBluetoothEnabled(isChecked);

找到这个点时开始点,然后看一下LocalBluetoothAdapter类

public void setBluetoothEnabled(boolean enabled) {
        boolean success = enabled
                ? mAdapter.enable()
                : mAdapter.disable();

        if (success) {
            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);
            }

            syncBluetoothState();
        }
    }

注意:

boolean success = enabled
                ? mAdapter.enable()
                : mAdapter.disable();

开始对蓝牙设备进行了使能操作,打开蓝牙设备mAdapter是BluetoothAdapter的一个对象,而BluetoothAdapter类在framework里面了

public boolean enable() {
        if (isEnabled() == true){
            if (DBG) Log.d(TAG, "enable(
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值