前一段时间我们分析了kernelbluetooth的初始化操作,从这一章起,晓东将会和大家一起正式进入到Androidbluetooth的分析。毫无疑问,我们依然需要知道在Android启动的时候,蓝牙究竟有做些什么。

首先我们来回顾一下Android启动的一般流程,有人把它归结为4个步骤,分别为1init进程启动;2Native服务启动;3System serverAndroid服务启动;4Home启动。那么,我们来一个个看在这几个步骤中,都有哪些是和蓝牙有关的。

1init中的bluetooth

[html] view plain copy
  1. #首先是创建了两个文件夹和bluetooth相关
  2. mkdir/data/misc/bluetoothd0770bluetoothbluetooth
  3. mkdir/data/misc/bluetooth0770systemsystem
  4. #新建了一个dbus的service,dbus是用于bluez和jni层交互的
  5. #细心的同学已经发了,他是没有disabled的,所以,会直接启动哦
  6. #不过dbus只是一个通路而已,我们暂时并不准备去详细地分析
  7. servicedbus/system/bin/dbus-daemon--system--nofork
  8. classmain
  9. socketdbusstream660bluetoothbluetooth
  10. userbluetooth
  11. groupbluetoothnet_bt_admin
  12. #新建了bluetoothd的service,这个就是bluez了
  13. servicebluetoothd/system/bin/bluetoothd-n
  14. classmain
  15. socketbluetoothstream660bluetoothbluetooth
  16. socketdbus_bluetoothstream660bluetoothbluetooth
  17. #init.rcdoesnotyetsupportapplyingcapabilities,sorunasrootand
  18. #letbluetoothddropuidtobluetoothwiththerightlinuxcapabilities
  19. groupbluetoothnet_bt_adminmisc
  20. disabled
  21. #这里就是各个文件的权限配置了
  22. chmod666/dev/ttyS0
  23. chmod666/proc/bluetooth/sleep/proto
  24. chmod666/sys/class/rfkill/rfkill0/state
  25. chmod666/sys/class/rfkill/rfkill0/type
  26. chownbluetoothbluetooth/sys/class/rfkill/rfkill0/state
  27. chownbluetoothbluetooth/sys/class/rfkill/rfkill0/type
  28. #一个hciattach的service,hciattach是bluetooth的一个工具,这里可以用来启动蓝牙
  29. #涉及到一些vendor的信息后面就写成***了
  30. servicehciattach/system/bin/logwrapper/system/bin/hciattach-n/dev/ttyS0***
  31. classmain
  32. userbluetooth
  33. groupbluetoothnet_bt_admin
  34. disabled
  35. oneshot

总得来说,init的过程和蓝牙相关的就是启动了dbus service,然后新建了bluetoothdhciattachservice,其它就只有一些文件和文件夹以及他们权限相关的内容修改了。

Native服务启动就和蓝牙没有什么大的关系了,我们直接去看system server中的Android服务启动了

2system server中的bluetooth

system server会启动ServerThread,至于为什么会走到这里,应该会有无数的文章详细介绍这个吧,晓东就不献丑了。我们直接来看蓝牙相关的内容:(代码位于framework/base/services/java/com/android/server/systemserver.java

[cpp] view plain copy
  1. classServerThreadextendsThread{
  2. ……
  3. @Override
  4. publicvoidrun(){
  5. ……
  6. //SkipBluetoothifwehaveanemulatorkernel
  7. //TODO:Useamorereliablechecktoseeifthisproductshould
  8. //supportBluetooth-seebug988521
  9. //若是模拟器,就不用bluetooth了
  10. if(SystemProperties.get("ro.kernel.qemu").equals("1")){
  11. Slog.i(TAG,"NoBluetoohService(emulator)");
  12. //lowlevel的工厂测试模式,也不启动bluetooth
  13. }elseif(factoryTest==SystemServer.FACTORY_TEST_LOW_LEVEL){
  14. Slog.i(TAG,"NoBluetoothService(factorytest)");
  15. }else{
  16. //上面两种都是非正常的启动,我们可以忽略
  17. Slog.i(TAG,"BluetoothService");
  18. //新建bluetoothservice,详细见2.1
  19. //加入到servicemanager中,是BluetoothAdapter.BLUETOOTH_SERVICE
  20. bluetooth=newBluetoothService(context);
  21. ServiceManager.addService(BluetoothAdapter.BLUETOOTH_SERVICE,bluetooth);
  22. //注册之后的init,详细见2.2
  23. bluetooth.initAfterRegistration();
  24. //新建a2dpservice,并同样加入到servicemanager中,详细见2.3
  25. bluetoothA2dp=newBluetoothA2dpService(context,bluetooth);
  26. ServiceManager.addService(BluetoothA2dpService.BLUETOOTH_A2DP_SERVICE,
  27. bluetoothA2dp);
  28. //a2dp的init
  29. bluetooth.initAfterA2dpRegistration();
  30. //得到是否是飞行模式
  31. intairplaneModeOn=Settings.System.getInt(mContentResolver,
  32. Settings.System.AIRPLANE_MODE_ON,0);
  33. //看bluetooth在上次关闭的时候是否on的
  34. intbluetoothOn=Settings.Secure.getInt(mContentResolver,
  35. Settings.Secure.BLUETOOTH_ON,0);
  36. //若是非飞行模式,上次关闭前是打开,则使能bt
  37. //使能bt的过程这篇文章就不介绍了,后面会另开一篇介绍
  38. if(airplaneModeOn==0&&bluetoothOn!=0){
  39. bluetooth.enable();
  40. }
  41. }

2.1 bluetoothservice的构造

bluetoothservice可以说是bluetooth的一个核心组成了,很多初始化都是由它这里发起的,我们来详细阅读一下:(代码位于/framework/base/core/java/android/server/bluetoothservice.java

[cpp] view plain copy
  1. publicBluetoothService(Contextcontext){
  2. mContext=context;
  3. //Needtodothisinplaceof:
  4. //mBatteryStats=BatteryStatsService.getService();
  5. //SincewecannotimportBatteryStatsServicefromhere.Thisclassreallyneedstobe
  6. //movedtojava/services/com/android/server/
  7. //得到battery的状态
  8. mBatteryStats=IBatteryStats.Stub.asInterface(ServiceManager.getService("batteryinfo"));
  9. //是否是btwifi共存
  10. if(SystemProperties.get("ro.btwifi.coexist","true").equals("false")){
  11. supportBtWifiCoexit=false;
  12. }
  13. //jni层的nativedata的一些初始化。最主要的是检查一下dbus是否能够用
  14. //这里就不详细介绍了,很简单,代码位于frameworks/base/core/jni/android_server_bluetoothservice.cpp中
  15. initializeNativeDataNative();
  16. //检查bt是否已经enable了,理论是不应该enable的
  17. if(isEnabledNative()==1){
  18. Log.w(TAG,"Bluetoothdaemonsalreadyrunning-runtimerestart?");
  19. disableNative();
  20. }
  21. //这个是用来表示配对的一些状态的
  22. //主要是注册一个BluetoothDevice.ACTION_PAIRING_REQUEST的receiver,这样在有pair的请求的时候,他会把pair的device加入到mPairingRequestRcvd列表中
  23. mBondState=newBluetoothBondState(context,this);
  24. //主要就是建了一个mPropertiesMap的hash表,保存adapter的propertiesmAdapterProperties=newBluetoothAdapterProperties(context,this);
  25. //新建了一个mPropertiesMap的hash表,用来保存device的properties
  26. mDeviceProperties=newBluetoothDeviceProperties(this);
  27. //新建一个deviceservicechannel的hash表,还有别的hash表的建立,等用到的时候再一一详细介绍
  28. mDeviceServiceChannelCache=newHashMap<String,Map<ParcelUuid,Integer>>();
  29. mDeviceOobData=newHashMap<String,Pair<byte[],byte[]>>();
  30. mUuidIntentTracker=newArrayList<String>();
  31. mUuidCallbackTracker=newHashMap<RemoteService,IBluetoothCallback>();
  32. mServiceRecordToPid=newHashMap<Integer,ServiceRecordClient>();
  33. mDeviceProfileState=newHashMap<String,BluetoothDeviceProfileState>();
  34. //初始化两个profiles的状态机。BluetoothProfileState是用来统一管理profile的状态的,这里注册了A2DP和HFP两个profile
  35. mA2dpProfileState=newBluetoothProfileState(mContext,BluetoothProfileState.A2DP);
  36. mHfpProfileState=newBluetoothProfileState(mContext,BluetoothProfileState.HFP);
  37. //启动这两个状态机,开始接收各种message
  38. mHfpProfileState.start();
  39. mA2dpProfileState.start();
  40. mAlarmManager=(AlarmManager)mContext.getSystemService(Context.ALARM_SERVICE);
  41. Intentintent=newIntent(DISCOVERABLE_ALARM);
  42. mPendingIntent=PendingIntent.getBroadcast(mContext,0,intent,0);
  43. IntentFilterfilter=newIntentFilter();
  44. //就是监听ACTION_AIRPLANE_MODE_CHANGED
  45. registerForAirplaneMode(filter);
  46. //加入DISCOVERABLE_ALARM,ACTION_DOCK_EVENT,ACTION_BOOT_COMPLETED的监听
  47. filter.addAction(DISCOVERABLE_ALARM);
  48. filter.addAction(Intent.ACTION_DOCK_EVENT);
  49. //这里说明一下ACTION_BOOT_COMPLETED的处理,就是置一个mBootCompleted=true;所以,就不另外特别说明了
  50. filter.addAction(Intent.ACTION_BOOT_COMPLETED);
  51. mContext.registerReceiver(mReceiver,filter);
  52. //构造几个profile的处理,有hid,pan和health,没有什么好说的
  53. mBluetoothInputProfileHandler=BluetoothInputProfileHandler.getInstance(mContext,this);
  54. mBluetoothPanProfileHandler=BluetoothPanProfileHandler.getInstance(mContext,this);
  55. mBluetoothHealthProfileHandler=BluetoothHealthProfileHandler.getInstance(mContext,this);
  56. //再新建两个hash表
  57. mIncomingConnections=newHashMap<String,Pair<Integer,String>>();
  58. mProfileConnectionState=newHashMap<Integer,Pair<Integer,Integer>>();
  59. }

2.2initAfterRegistration

这个函数是用来在注册之后进行初始化的,所以,可以说是bluetoothservice中被调用的第一个函数了,我们来看看它做了一些什么:

[cpp] view plain copy
  1. publicsynchronizedvoidinitAfterRegistration(){
  2. /新建了一个bluetoothadapter,这很重要,adapter和bluetoothservice是绑定的,后期上层ui都是同adapter那边来找到bluetoothservice的
  3. mAdapter=BluetoothAdapter.getDefaultAdapter();
  4. /新建bluetoothadapter的状态机,后面我会另外开一篇文章来详细介绍状态机
  5. mBluetoothState=newBluetoothAdapterStateMachine(mContext,this,mAdapter);
  6. //start状态机
  7. mBluetoothState.start();
  8. //看是否支持qucikswitch,若是支持,我们会在开机的时候就默认为hot状态
  9. //这里就是4.0和2.3中最大的不同,就是其实默认支持quickswitch的情况下,蓝牙是打开的,只是没有显示在ui界面上而已。关于这里,我会另开一篇文章来介绍
  10. if(mContext.getResources().getBoolean
  11. (com.android.internal.R.bool.config_bluetooth_adapter_quick_switch)){
  12. mBluetoothState.sendMessage(BluetoothAdapterStateMachine.TURN_HOT);
  13. }
  14. //得到bluetooth的eventloop,eventloop在adapterstatemachine构造的时候创建的,这个很重要,不过我们也放到另外一篇文章中去详细介绍
  15. mEventLoop=mBluetoothState.getBluetoothEventLoop();
  16. }

2.3 BluetoothA2dpService

这个是a2dp service的构造

[cpp] view plain copy
  1. publicBluetoothA2dpService(Contextcontext,BluetoothServicebluetoothService){
  2. mContext=context;
  3. //得到audioservice
  4. mAudioManager=(AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
  5. //和bluetoothservice关联
  6. mBluetoothService=bluetoothService;
  7. if(mBluetoothService==null){
  8. thrownewRuntimeException("PlatformdoesnotsupportBluetooth");
  9. }
  10. /和bluetoothservice类似,就是初始化native的data和检查dbus
  11. if(!initNative()){
  12. thrownewRuntimeException("CouldnotinitBluetoothA2dpService");
  13. }
  14. mAdapter=BluetoothAdapter.getDefaultAdapter();
  15. //几个action的处理
  16. mIntentFilter=newIntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
  17. mIntentFilter.addAction(BluetoothDevice.ACTION_ACL_CONNECTED);
  18. mIntentFilter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED);
  19. mIntentFilter.addAction(AudioManager.VOLUME_CHANGED_ACTION);
  20. mContext.registerReceiver(mReceiver,mIntentFilter);
  21. mAudioDevices=newHashMap<BluetoothDevice,Integer>();
  22. /看bluetooth是否on
  23. if(mBluetoothService.isEnabled())
  24. onBluetoothEnable();
  25. mTargetA2dpState=-1;
  26. /使得bluetoothservice那边能找到a2dpservice
  27. mBluetoothService.setA2dpService(this);
  28. }

至此,android启动时,bluetooth的一些操作就已经都比较明了了。在init过程中:“init的过程和蓝牙相关的就是启动了dbus service,然后新建了bluetoothdhciattachservice,其它就只有一些文件和文件夹以及他们权限相关的内容修改了。”

system service过程中,启动了bluetooth servicea2dp service。这之中比较重要的是构造了bluetoothadapater的状态机,这个是后期蓝牙各种状态变化的基础,同时启动了eventloop用于处理从dbus过来的各种event,这两者晓东会另开篇幅和大家详细介绍。

更多相关文章

  1. 编译移植android 2.3到tiny210
  2. Android启动脚本init.rc
  3. Android(安卓)启动过程详解
  4. Android蓝牙开发
  5. Android底层开发之旅—蓝牙系统分析
  6. android蓝牙BLE(三) —— 广播
  7. Android(安卓)的启动流程-转
  8. Android(安卓)4.2启动代码分析(一)
  9. android系统启动过程

随机推荐

  1. Android锁屏 DevicePolicyManager介绍
  2. Android(安卓)中文 API (93) —— BaseExpa
  3. Myeclispe10+ Android4.4 环境搭建
  4. 边缘图片android布局属性详解
  5. Eclipse / Android(安卓): “Errors runn
  6. Android(安卓)XML解析
  7. Android(安卓)Dialog无法去除遮罩
  8. Android取消EditText自动获取焦点默认行
  9. androdi ImageView.ScaleType
  10. Android(安卓)TextView 设置中划线 下划