转一篇写的比较好的BT enable 过程的文章。


以下是基于Android 4.2代码,对Bluetooth BR/EDR Enableprocess的分析。BluetoothAdapter类代表的是local device Bluetoothadapter,而BluetoothDevice类代表的是remote Bluetooth device。在Android4.3中引入了一个新的类BluetoothManager,它是一个high level manager,被用于”to obtainan instance of an BluetoothAdapter and conduct overall BluetoothManagement“。


Bluetooth Enableprocess比较复杂,层次比较多,最好的分析方法是:对照logcat输出的Bluetooth相关log来阅读代码。首先从总体上介绍以下Enableprocess。UI上的入口是Settings,拨动Bluetooth开关,就启动了Bluetooth Enableprocess,最后由Bluedroid去enable Bluetooth hardware。当Bluetooth hardwareenabled,这个enabled消息会一层层从Bluedroid上传到UI层,Settings收到这个消息就可以更新Bluetooth开关的状态了。具体过程如下图:




  1. Settings的BluetoothEnabler类(对应于UI上看到的Bluetooth开关),得到代表localdevice的BluetoothAdapter,再调用BluetoothAdapter::enable()。
  2. BluetoothAdapter基本上是个wrapper,不做具体的事情的。它直接调用BluetoothManagerService::enable()。
  3. BluetoothManagerService利用Binder机制会去connectAdapterService,最终会导致AdapterService::enable()被调用。BluetoothManagerService还会向AdapterService注册callback函数,用于接收AdapterState Change消息。
  4. AdapterService维护着一个状态机AdapterState,所有工作都是通过驱动状态机来完成的。AdapterState收到AdapterService发过来的USER_TURN_ON消息,就会调用AdapterService::processStart()来启动ProfieServices的初始化和Bluetooth hardware enable process。此时BluetoothAdapter的状态是BluetoothAdapter.STATE_TURNING_ON
  5. 每一个profile都有一个service。每个profileservice启动完成后,都会通知AdapterService。当AdapterService::processProfileServiceStateChanged()确认所有的profileservices都启动完成了,就会给状态机AdapterState发AdapterState.STARTED消息。
  6. 状态机AdapterState::PendingCommandState::processMessage()收到AdapterState.STARTED消息后就立刻调用AdapterService::enableNative()。
  7. AdapterService::enableNative()就是用来enable Bluetooth的BluetoothJNI接口。enableNative()会调用Bluetooth HAL的enable()。
  8. Bluedroid用btif_enable_bluetooth()来实现了BluetoothHAL的enable()。
  9. 当Bluedroid真正完成了enable Bluetoothhardware,就通过btif_enable_bluetooth_evt()中的HAL_CBACK调用BluetoothJNI的adapter_state_change_callback(),这样就把BT_STATE_ON消息传递给了状态机AdapterState。
  10. AdapterState会把BluetoothAdapter的状态转换到BluetoothAdapter.STATE_ON,并通过AdapterState::notifyAdapterStateChanged()通知AdapterService。
  11. AdapterService::updateAdapterState()会通过callback函数通知BluetoothManagerService,Adapter状态改变了。
  12. BluetoothManagerService确认状态发生了改变就会发出一个BluetoothAdapter.ACTION_STATE_CHANGE的intent。
  13. Settings的BluetoothEnabler收到这个intent之后,就会去更新UI上Bluetooth开关的状态。
再贴出图片,方便后面看。

注:以上过程的描述,并不包含这个过程的所有函数调用,只是给出了关键的函数和状态。
From :http://blog.sina.com.cn/s/blog_69b5d2a50101f52r.html

更多相关文章

  1. Android调用手机新浪微博客户端分享
  2. Android(安卓)Wake lock
  3. Android(安卓)文本转语音TextToSpeech (TTS)
  4. android 4.4 沉浸式状态栏实现
  5. Android(安卓)Activity 生命周期(lifecycle)的研究
  6. Android(安卓)Logcat和Debug的使用
  7. android异常:Can not perform this action after onSaveInstanc
  8. Android应用程序安装与Launcher启动机制
  9. AIDL接口描述语言的使用

随机推荐

  1. Android中xml的部分属性
  2. Android(安卓)Studio常用快捷键、Android
  3. 如何一个android工程作为另外一个android
  4. Android创建和使用数据库详细指南
  5. 创建一个ArcGIS for Android(安卓)新项目
  6. Android音频简介
  7. 多款Android播放器源码集锦(附开发教程)
  8. 推翻自己和过往,重学自定义View
  9. Linux内核怎样启动Android
  10. Android中的消息处理实例与分析