android -- 蓝牙 bluetooth (二) 打开蓝牙
4.2的蓝牙打开流程这一部分还是有些变化的,从界面上看蓝牙开关就是设置settings里那个switch开关,widget开关当然也可以,起点不 同而已,后续的流程是一样的。先来看systemServer.java的代码,蓝牙服务开启的地方,最后一个else分支是我们关心的,前两个是模拟器 的一个测试模式的。
- if (SystemProperties.get("ro.kernel.qemu").equals("1")) {
- Slog.i(TAG, "No Bluetooh Service (emulator)");
- } else if (factoryTest == SystemServer.FACTORY_TEST_LOW_LEVEL) {
- Slog.i(TAG, "No Bluetooth Service (factory test)");
- } else {
- Slog.i(TAG, "Bluetooth Manager Service");
- bluetooth = new BluetoothManagerService(context);
- ServiceManager.addService(BluetoothAdapter.BLUETOOTH_MANAGER_SERVICE, bluetooth);
- }
暂且看下bluetoothManagerService的构造方法,代码有点多,我们只看两个地 方, loadStoredNameAndAddress()是读取蓝牙打开默认名称的地 方,isBluetoothPersistedStateOn()是判断是否已打开蓝牙的,如果已打开,后续操作要执行开启蓝牙的动作,前面那几行注册广 播其中就有这个作用。
- BluetoothManagerService(Context context) {
- ...一些变量声明初始化...
- IntentFilter filter = new IntentFilter(Intent.ACTION_BOOT_COMPLETED);
- filter.addAction(BluetoothAdapter.ACTION_LOCAL_NAME_CHANGED);
- filter.addAction(Intent.ACTION_USER_SWITCHED);
- registerForAirplaneMode(filter);
- mContext.registerReceiver(mReceiver, filter);
- loadStoredNameAndAddress();
- if (isBluetoothPersistedStateOn()) {
- mEnableExternal = true;
- }
- }
回到界面开关那个看得着的地方,界面上开关就是BluetoothEnabler.java这个类了,而setBluetoothEnabled()则是具体开关动作。看下代码
- 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);
- }
这里在判断是飞行模式不知道为什么没有return,如果是飞行模式会有提示toast弹出,既然这样源码为什么还要执行下面打开流程呢,也许是个 bug?不细究这个了,继续看setBluetoothEnabled()方法做什么了,很明显 mLocalAdapter(LocalBluetoothAdapter )只是个过渡,里面的 mAdapter(BluetoothAdapter)才是真正的主角,代码如下:
- 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 {
- .........
- }
- }
拿到BluetoothAdapter对象,代码也简单看下吧,里面是典型的binder应用。
- public static synchronized BluetoothAdapter getDefaultAdapter() {
- if (sAdapter == null) {
- IBinder b = ServiceManager.getService(BLUETOOTH_MANAGER_SERVICE);
- if (b != null) {
- IBluetoothManager managerService = IBluetoothManager.Stub.asInterface(b);
- sAdapter = new BluetoothAdapter(managerService);
- } else {
- Log.e(TAG, "Bluetooth binder is null");
- }
- }
- return sAdapter;
方法,后面要跳转到新类了,贴出来一起看下,这部分好像不同版本还有些出入,不过核心的启动service是一样的,不影响理解。
- private void handleEnable(boolean persist, boolean quietMode) {
- synchronized(mConnection) {
- if ((mBluetooth == null) && (!mBinding)) {
- //Start bind timeout and bind
- Message timeoutMsg=mHandler.obtainMessage(MESSAGE_TIMEOUT_BIND);
- mHandler.sendMessageDelayed(timeoutMsg,TIMEOUT_BIND_MS);
- mConnection.setGetNameAddressOnly(false);
- Intent i = new Intent(IBluetooth.class.getName());
- if (!mContext.bindService(i, mConnection,Context.BIND_AUTO_CREATE,
- UserHandle.USER_CURRENT)) {
- mHandler.removeMessages(MESSAGE_TIMEOUT_BIND);
- Log.e(TAG, "Fail to bind to: " + IBluetooth.class.getName());
- } else {
- mBinding = true;
- }
- }
这样的信息,那就是去AdapterService里看看,里面一共有三个enable(),跳转关系不复杂,我们直接看最后一个关键的。
- public synchronized boolean enable(boolean quietMode) {
- enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM,
- "Need BLUETOOTH ADMIN permission");
- if (DBG)debugLog("Enable called with quiet mode status = " + mQuietmode);
- mQuietmode = quietMode;
- Message m =
- mAdapterStateMachine.obtainMessage(AdapterState.USER_TURN_ON);
- mAdapterStateMachine.sendMessage(m);
- return true;
- }
处理在同文件下面的代码里
- case STARTED: {
- if (DBG) Log.d(TAG,"CURRENT_STATE=PENDING, MESSAGE = STARTED, isTurningOn=" + isTurningOn + ", isTurningOff=" + isTurningOff);
- //Remove start timeout
- removeMessages(START_TIMEOUT);
- //Enable
- boolean ret = mAdapterService.enableNative();
- if (!ret) {
- Log.e(TAG, "Error while turning Bluetooth On");
- notifyAdapterStateChange(BluetoothAdapter.STATE_OFF);
- transitionTo(mOffState);
- } else {
- sendMessageDelayed(ENABLE_TIMEOUT, ENABLE_TIMEOUT_DELAY);
- }
看到那个enableNative()函数调用了吧,又要用到JNI了,稍微回头看下前面的代码,我们先从应用界面开关BluetoothEnabler 走到framework的BluetoothAdapter,又回到package的adapterService,现在又要去JNI的C++代码了,往 常一般是packages -->framework-->下面一层,这次顺序有些颠倒了,不过这不能影响我们跟踪代码,最后
还是要到下面去的。一起往下看吧。
根据android JNI的函数命名惯例很容易找到enableNative对应的C++函数在packages/apps/Bluetooth/jni /com_android_bluetooth_btservice_AdapterService.cpp里面
- static jboolean enableNative(JNIEnv* env, jobject obj) {
- ALOGV("%s:",__FUNCTION__);
- jboolean result = JNI_FALSE;
- if (!sBluetoothInterface) return result;
- int ret = sBluetoothInterface->enable();
- result = (ret == BT_STATUS_SUCCESS) ? JNI_TRUE : JNI_FALSE;
- return result;
- }
代码瞬间简洁了不少,看来更多的故事还在下面,sBluetoothInterface这是什么,直接关系到下一步去哪的问题,看下变量声明,原来是
Const bt_interface_t *sBluetoothInterface = NULL; 再去找在哪初始化,搜索external目录可以找到/external/bluetooth/bluedroid/btif/src/bluetooth.c
- static const bt_interface_t bluetoothInterface = {
- sizeof(bt_interface_t),
- init,
- enable,
- disable,
- .............
- start_discovery,
- cancel_discovery,
- create_bond,
- remove_bond,
- cancel_bond,
- ...............
- };
- static int enable( void )
- {
- ALOGI("enable");
- /* sanity check */
- if (interface_ready() == FALSE)
- return BT_STATUS_NOT_READY;
- return btif_enable_bluetooth();
- }
- bt_status_t btif_enable_bluetooth(void)
- {
- BTIF_TRACE_DEBUG0("BTIF ENABLE BLUETOOTH");
- if (btif_core_state != BTIF_CORE_STATE_DISABLED)
- {
- ALOGD("not disabled\n");
- return BT_STATUS_DONE;
- }
- btif_core_state = BTIF_CORE_STATE_ENABLING;
- /* Create the GKI tasks and run them */
- bte_main_enable(btif_local_bd_addr.address);
- return BT_STATUS_SUCCESS;
- }
- void bte_main_enable(uint8_t *local_addr)
- {
- APPL_TRACE_DEBUG1("%s", __FUNCTION__);
- ........................
- #if (defined (BT_CLEAN_TURN_ON_DISABLED) && BT_CLEAN_TURN_ON_DISABLED == TRUE)
- APPL_TRACE_DEBUG1("%s Not Turninig Off the BT before Turninig ON", __FUNCTION__);
- #else
- /* toggle chip power to ensure we will reset chip in case
- a previous stack shutdown wasn't completed gracefully */
- bt_hc_if->set_power(BT_HC_CHIP_PWR_OFF);
- #endif
- bt_hc_if->set_power(BT_HC_CHIP_PWR_ON);
- bt_hc_if->preload(NULL);
- }
- .............................
- }
路径在这里/external/bluetooth/bluedroid/hci/src/bt_hci_bdroid.c,看看set_power里面有什么,快到头了
- static void set_power(bt_hc_chip_power_state_t state)
- {
- int pwr_state;
- BTHCDBG("set_power %d", state);
- /* Calling vendor-specific part */
- pwr_state = (state == BT_HC_CHIP_PWR_ON) ? BT_VND_PWR_ON : BT_VND_PWR_OFF;
- if (bt_vnd_if)
- bt_vnd_if->op(BT_VND_OP_POWER_CTRL, &pwr_state);
- else
- ALOGE("vendor lib is missing!");
- }
- /* Entry point of DLib --
- * Vendor library needs to implement the body of bt_vendor_interface_t
- * structure and uses the below name as the variable name. HCI library
- * will use this symbol name to get address of the object through the
- * dlsym call.
- */
- extern const bt_vendor_interface_t BLUETOOTH_VENDOR_LIB_INTERFACE;
- bt_vendor_interface_t *bt_vnd_if=NULL;
google定义好了接口,具体实现要看vendor厂商来做了,这后面怎么实现就看各家芯片商怎么写了,肯定各有不同,而且这一部分代码一般是不会公 开,当然授权购买后除外了。所以在4.2的源码中我们只跟到这里了,那后面会做什么呢,加载驱动和上电这两项肯定要有了,打开蓝牙没这两步怎么行,类似下 面的字符串
- static const char* BT_DRIVER_MODULE_PATH = "/system/lib/modules/mbt8xxx.ko";
- static const char* BT_DRIVER_MODULE_NAME = "bt8xxx";
- static const char* BT_DRIVER_MODULE_INIT_ARG = " init_cfg=";
- static const char* BT_DRIVER_MODULE_INIT_CFG_PATH = "bt_init_cfg.conf";
在有类似下面的动作,insmod加载驱动,rfkill控制上下电,具体厂商具体做法也不同。
- ret = insmod(BT_DRIVER_MODULE_PATH, arg_buf);
- ret = system("/system/bin/rfkill block all");
- 12楼 reality_jie 前天 22:31发表 [回复]
- 顶一下表示支持!
- 9楼 syngalon螳螳 2013-10-26 15:35发表 [回复]
- 今天发现了一篇,看看是不是很像哈哈
http://blog.csdn.net/yutao52shi/article/details/12690353
- 4楼 windjkl 2013-06-20 10:38发表 [回复]
- 设置-打开蓝牙:出现以下错误是什么原因:
D/BluetoothAdapter( 3463): 657778856: getState() : mService = null. Returning STATE_OFF
D/LocalBluetoothManager( 3463): setting foreground activity to non-null context
D/BluetoothAdapter( 3463): 657778856: getState() : mService = null. Returning STATE_OFF
- Re: balmy 2013-06-20 21:37发表 [回复]
- 回 复windjkl:这个没遇到过呢,不了解你当时的环境,没啥好想法。另外答复你添加USB外置蓝牙模块的问题,也是我咨询来的,仅供参考吧。usb设备 应该都会有注册流程吧,这一部分好像android部分的源码没有,在linux的源码是有的,也就是android源码里kernel。目录下的类似这 个路径:kernel/drivers/bluetooth/btusb.c,按你说的已经识别出来是不是可以认为注册成功了?后续代码没见过,可能没有 也可能是没发现的,目前接触到都是走串口的,目前只知道这些,希望不会误导你。
- Re: windjkl 2013-06-21 08:29发表 [回复]
- 回复baimy1985:谢谢楼主,注册已经成功,现在疑问是service的启动?jb4.2是否还需要在初始化时启动d-bus,hciattach这些服务呢?我看有些方案商的init.rc中并没有这些服务?是不是换成别的方式呢?
- Re: xuexingyang 2013-06-20 19:27发表 [回复]
- 回复baimy1985:4.2中hcidump没有了,只有靠confi文件来开关了。