2014年12月19日 星期五

Bluedroid: Inquiry 流程 (轉)

作者:baimy1985

接上篇打開藍芽繼續,來一起看下藍芽搜索的流程,觸發藍芽搜索的條件形式上有兩種,一是在藍芽設置界面開啟藍芽會直接開始搜索,另一個是先打開藍芽開關在進入藍芽設置界面也會觸發搜索,也可能還有其它觸發方式,但最後都要來到BluetoothSettngs.java的startScanning(),我們分析的起點也從這裡開始,起步代碼如下
  1. private void updateContent(int bluetoothState, boolean scanState) {  
  2.     if (numberOfPairedDevices == 0) {  
  3.          preferenceScreen.removePreference(mPairedDevicesCategory);  
  4.          if (scanState == true) {  
  5.              mActivityStarted = false;  
  6.              startScanning();  
  7.         } else<span style="font-family: Arial, Helvetica, sans-serif;">    ........</span>  
  8. }         
  9. private void startScanning() {  
  10.         if (!mAvailableDevicesCategoryIsPresent) {  
  11.             getPreferenceScreen().addPreference(mAvailableDevicesCategory);  
  12.         }  
  13.         mLocalAdapter.startScanning(true);  
  14.     }  

        其實在這裡藍芽搜索和打開流程是結構上是一致的,利用LocalBluetoothAdapter.java過渡到BluetoothAdapter.java再跳轉至AdapterService.java要稍微留意下的是在這個過渡中startScaning()方法變成了startDiscovery()方法,看下代碼:packages/apps/Settings/src/com/android/settings/bluetooth/LocalBluetoothAdapter.java
[java] view plaincopy
  1. void startScanning(boolean force) {  
  2. if (!mAdapter.isDiscovering()) {  
  3.      if (!force) {  
  4.          // Don't scan more than frequently than SCAN_EXPIRATION_MS,  
  5.          // unless forced  
  6.          if (mLastScan + SCAN_EXPIRATION_MS > System.currentTimeMillis()) {  
  7.              return;  
  8.          }  
  9.          // If we are playing music, don't scan unless forced.  
  10.          A2dpProfile a2dp = mProfileManager.getA2dpProfile();  
  11.          if (a2dp != null && a2dp.isA2dpPlaying()) {  
  12.              return;  
  13.          }  
  14.      }  
  15. //這裡才是我們最關注的,前面限制條件關注一下就行了  
  16.      if (mAdapter.startDiscovery()) {  
  17.          mLastScan = System.currentTimeMillis();  
  18.      }  
  19. }  
       BluetoothAdapter.java的那一段,路徑 /frameworks/base/core/java/android/bluetooth/BluetoothAdapter.java
[java] view plaincopy
  1. public boolean startDiscovery() {  
  2.     .............................  
  3.     AdapterService service = getService();  
  4.     if (service == nullreturn false;  
  5.     return service.startDiscovery();  
  6. }  
      這個service代碼寫得很明白AdapterService,轉了一圈從framework又回到packages了,
      下面的代碼路逕自然是 :packages/apps/Bluetooth/src/com/android/bluetooth/btservice/AdapterService.java,
[java] view plaincopy
  1. boolean startDiscovery() {  
  2.     enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM,  
  3.             "Need BLUETOOTH ADMIN permission");  
  4.   
  5.     return startDiscoveryNative();  
  6. }  
      和打開藍芽根本就是一個套路,上面的流程略過一小步,很簡單的不寫了,下面要怎麼走,估計大家也都猜到了,JNI應該出場了,
       路徑:/packages/apps/Bluetooth/jni/com_android_bluetooth_btservice_AdapterService.cpp
  1. static jboolean startDiscoveryNative(JNIEnv* env, jobject obj) {                                                     
  2.     ALOGV("%s:",__FUNCTION__);                                  
  3.                                                                 
  4.     jboolean result = JNI_FALSE;                                
  5.     if (!sBluetoothInterface) return result;                    
  6.                                                                 
  7.     int ret = sBluetoothInterface->start_discovery();           
  8.     result = (ret == BT_STATUS_SUCCESS) ? JNI_TRUE : JNI_FALSE;  
  9.     return result;                                              
  10. }   
      在下面要去哪?稍微要動下腦筋,不過我們在上一篇android -- 藍芽 bluetooth (二) 打開藍芽已經說過怎麼找了,注意android.mk文件,先找頭文件,再找對應的實現C文件代碼。就是現在回顧下,藍芽打開和搜索的代碼流程我們都看了,跳轉都是一個套路,settings界面發起,LocalBluetoothAdapter.java過渡,去framework的轉轉(BluetoothAdapter.java)後回到packages的AdapterService.java,再走JNI來的external。流程就是這樣的,相信類似的功能跳轉(比如藍芽配對,關閉藍芽,停止掃瞄這些)大家都應該熟悉了,後面再有類似的功能就寫函數名一筆帶過了,還有這裡要注意的就是這個start_discovery()實現代碼的尋找,留意mk文件就是了,不複雜。小結結束,繼續看代碼    路徑:/external/bluetooth/bluedroid/btif/src/bluetooth.c
  1. static int start_discovery(void)  
  2. {  
  3.     /* sanity check */  
  4.     if (interface_ready() == FALSE)  
  5.         return BT_STATUS_NOT_READY;  
  6.   
  7.     return btif_dm_start_discovery();  
  8. }     
        下面代碼直接跳轉就可以找到,路徑external/bluetooth/bluedroid/btif/src/btif_dm.c  
        這個代碼有點多,不過裡面的信息也很多,所以連註釋也一起保留的貼出來了,藍芽的搜索實現並沒有像藍芽打開那樣交由vendor廠商實現,在這裡已經寫出來了,仔細看下那些#if和#else,都是一些查詢條件的調置,#if (BLE_INCLUDED == TRUE)   這個應該就google為藍芽4.0 LE作的準備了,也算是今年google I/O大會上宣佈即將支持藍芽4.0低能耗版一個佐證吧,對於代碼裡面那些字元串的含義看這裡好了external/bluetooth/bluedroid/bta/include/bta_api.h,一個頭文件,大部分字元串和結構體的定義都在這了,多少還有些註釋。
  1. bt_status_t btif_dm_start_discovery(void)                                    
  2. {                                                                            
  3.     tBTA_DM_INQ inq_params;                                                  
  4.     tBTA_SERVICE_MASK services = 0;                                          
  5.                                                                              
  6.     BTIF_TRACE_EVENT1("%s", __FUNCTION__);                                   
  7.     /* TODO: Do we need to handle multiple inquiries at the same time? */    
  8.                                                                              
  9.     /* Set inquiry params and call API */                                    
  10. #if (BLE_INCLUDED == TRUE)                                                   
  11.     inq_params.mode = BTA_DM_GENERAL_INQUIRY|BTA_BLE_GENERAL_INQUIRY;        
  12. #else                                                                        
  13.     inq_params.mode = BTA_DM_GENERAL_INQUIRY;                                
  14. #endif                                                                       
  15.     inq_params.duration = BTIF_DM_DEFAULT_INQ_MAX_DURATION;                  
  16.                                                                              
  17.     inq_params.max_resps = BTIF_DM_DEFAULT_INQ_MAX_RESULTS;                  
  18.     inq_params.report_dup = TRUE;                                            
  19.                                                                              
  20.     inq_params.filter_type = BTA_DM_INQ_CLR;                                 
  21.     /* TODO: Filter device by BDA needs to be implemented here */            
  22.                                                                              
  23.     /* Will be enabled to TRUE once inquiry busy level has been received */  
  24.     btif_dm_inquiry_in_progress = FALSE;                                     
  25.     /* find nearby devices */                                                
  26.     BTA_DmSearch(&inq_params, services, bte_search_devices_evt);             
  27.                                                                              
  28.     return BT_STATUS_SUCCESS;                                                
  29. }   

      BTA_DmSearch()方法是看起來是要搜索了,不過裡面這個傢伙bte_search_devices_evt才是真正幹活的主力,所以我們先看它,在這個函數里
  1. static void bte_search_devices_evt(tBTA_DM_SEARCH_EVT event, tBTA_DM_SEARCH *p_data)                                                                      {                                                                                                                    
  2.        UINT16 param_len = 0;                                                                                           
  3.                                                                                                                         
  4.        if (p_data)                                                                                                      
  5.            param_len += sizeof(tBTA_DM_SEARCH);                                                                         
  6.        /* Allocate buffer to hold the pointers (deep copy). The pointers will point to the end of the tBTA_DM_SEARCH */  
  7.        switch (event)                                                                                                   
  8.        {                                                                                                                
  9.            case BTA_DM_INQ_RES_EVT:                                                                                     
  10.            {                                                                                                            
  11.                if (p_data->inq_res.p_eir)                                                                               
  12.                    param_len += HCI_EXT_INQ_RESPONSE_LEN;                                                               
  13.            }                                                                                                            
  14.            break;                                                                                                              
  15.           
  16.            case BTA_DM_DISC_RES_EVT:
  17.            {
  18.               if (p_data->disc_res.raw_data_size && p_data->disc_res.p_raw_data)
  19.                  param_len += p_data->disc_res.raw_data_size;
  20.            }
  21.            break;
  22.                                                                                                
  23.        }                                                                                                                
  24.        BTIF_TRACE_DEBUG3("%s event=%s param_len=%d", __FUNCTION__, dump_dm_search_event(event), param_len);             
  25.                                                                                                                         
  26.        /* if remote name is available in EIR, set teh flag so that stack doesnt trigger RNR */                          
  27.        if (event == BTA_DM_INQ_RES_EVT)                                                                                 
  28.            p_data->inq_res.remt_name_not_required = check_eir_remote_name(p_data, NULL, NULL);                          
  29.                                                                                                                         
  30.        btif_transfer_context (btif_dm_search_devices_evt , (UINT16) event, (void *)p_data, param_len,                   
  31.            (param_len > sizeof(tBTA_DM_SEARCH)) ? search_devices_copy_cb : NULL);                                       
  32.    }     
         在上面的這個函數里又有這個bte_search_devices_evt,在它裡我們能看一個 HAL_CBACK,這是要往回發消息了,看下這個函數的全貌,說是全貌,不過還是只貼出一個case分支,太長了,大家還是自行還源碼吧。到這裡已經可以知道掃瞄到藍芽設備的mac地址和設備名,那個bdcpy函數就是在解析mac地址,有了這些,藍芽搜索是到應該在界面展示成果的時候了,開始回調,忘記代碼路徑了,這個函數都在這個文件裡:  /external/bluetooth/bluedroid/btif/src/btif_dm.c
  1. static void btif_dm_search_devices_evt (UINT16 event, char *p_param)  
  2.   
  3.    tBTA_DM_SEARCH *p_search_data;  
  4.    BTIF_TRACE_EVENT2("%s event=%s", __FUNCTION__, dump_dm_search_event(event));  
  5.   
  6.    switch (event)  
  7.    {  
  8.        case BTA_DM_DISC_RES_EVT:  
  9.        {  
  10.            p_search_data = (tBTA_DM_SEARCH *)p_param;  
  11.            /* Remote name update */  
  12.            if (strlen((const char *) p_search_data->disc_res.bd_name))  
  13.            {  
  14.                bt_property_t properties[1];  
  15.                bt_bdaddr_t bdaddr;  
  16.                bt_status_t status;  
  17.   
  18.                properties[0].type = BT_PROPERTY_BDNAME;  
  19.                properties[0].val = p_search_data->disc_res.bd_name;  
  20.                properties[0].len = strlen((char *)p_search_data->disc_res.bd_name);  
  21.                bdcpy(bdaddr.address, p_search_data->disc_res.bd_addr);  
  22.   
  23.                status = btif_storage_set_remote_device_property(&bdaddr, &properties[0]);  
  24.                ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device property", status);  
  25.                HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,  
  26.                                 status, &bdaddr, 1, properties);  
  27.            }  
  28.            /* TODO: Services? */  
  29.        }  
  30.        break;  
       一小段log,下面的文字就在上面的函數里打出來的,即便上面的寫的函數沒有,肯定也在附近了。
05-30 13:52:14.890  1578  2612 D bt-btif : bte_search_devices_evt event=BTA_DM_INQ_RES_EVT param_len=524
05-30 13:52:14.890  1578  2612 D bt-btif : search_devices_copy_cb: event=BTA_DM_INQ_RES_EVT
05-30 13:52:14.890  1578  2584 I bt-btif : btif_dm_search_devices_evt event=BTA_DM_INQ_RES_EVT
05-30 13:52:14.890  1578  2584 D bt-btif : btif_dm_search_devices_evt() ec:89:f5:ba:fb:03 device_type = 0x1

        當然回過頭我們還要看下那個BTA_DmSearch(),看它的實現,更應該是起消息發送的作用,代碼在/external/bluetooth/bluedroid/bta/dm/bta_dm_api.c,這個函數具體流程並沒有看多少,當工具方法看了,有時間看看還是沒壞處的。
  1. void BTA_DmSearch(tBTA_DM_INQ *p_dm_inq, tBTA_SERVICE_MASK services, tBTA_DM_SEARCH_CBACK *p_cback)  
  2. {  tBTA_DM_API_SEARCH    *p_msg;  
  3.     if ((p_msg = (tBTA_DM_API_SEARCH *) GKI_getbuf(sizeof(tBTA_DM_API_SEARCH))) != NULL)  
  4.     {  
  5.         memset(p_msg, 0, sizeof(tBTA_DM_API_SEARCH));  
  6.   
  7.         p_msg->hdr.event = BTA_DM_API_SEARCH_EVT;  
  8.         memcpy(&p_msg->inq_params, p_dm_inq, sizeof(tBTA_DM_INQ));  
  9.         p_msg->services = services;  
  10.         p_msg->p_cback = p_cback;  
  11.         p_msg->rs_res  = BTA_DM_RS_NONE;  
  12.         bta_sys_sendmsg(p_msg);  
  13.     }  
  14. }  
      看了上面方法後我們 要回去了看看,代碼通過JNI下來的,回去也是看JNI的回調方法
  1. method_deviceFoundCallback = env->GetMethodID(jniCallbackClass, "deviceFoundCallback""([B)V");  
deviceFoundCallback方法最後會來java層的/packages/apps/Bluetooth/src/com/android/bluetooth/btservice/RemoteDevices.java
[java] view plaincopy
  1. void deviceFoundCallback(byte[] address) {  
  2.        // The device properties are already registered - we can send the intent  
  3.        // now  
  4.        BluetoothDevice device = getDevice(address);  
  5.        debugLog("deviceFoundCallback: Remote Address is:" + device);  
  6.        DeviceProperties deviceProp = getDeviceProperties(device);  
  7.        if (deviceProp == null) {  
  8.            errorLog("Device Properties is null for Device:" + device);  
  9.            return;  
  10.        }  
  11.   
  12.        Intent intent = new Intent(BluetoothDevice.ACTION_FOUND);  
  13.        intent.putExtra(BluetoothDevice.EXTRA_DEVICE, device);  
  14.        intent.putExtra(BluetoothDevice.EXTRA_CLASS,  
  15.                new BluetoothClass(Integer.valueOf(deviceProp.mBluetoothClass)));  
  16.        intent.putExtra(BluetoothDevice.EXTRA_RSSI, deviceProp.mRssi);  
  17.        intent.putExtra(BluetoothDevice.EXTRA_NAME, deviceProp.mName);  
  18.   
  19.        mAdapterService.sendBroadcast(intent, mAdapterService.BLUETOOTH_PERM);  
  20.    }  
        到這裡就是給界面發廣播,應用層收到廣播顯示出來,通過這個handle,這個handle可以在BluetoothEventManager.java的建構子里找到,
[java] view plaincopy
  1. addHandler(BluetoothDevice.ACTION_FOUND, new DeviceFoundHandler());  
  2.     private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {  
  3.     @Override  
  4.     public void onReceive(Context context, Intent intent) {  
  5.         String action = intent.getAction();  
  6.         BluetoothDevice device = intent  
  7.                 .getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);  
  8.   
  9.         Handler handler = mHandlerMap.get(action);  
  10.         if (handler != null) {  
  11.             handler.onReceive(context, intent, device);  
  12.         }  
  13.     }  
  14. };  
         這裡handle對應要看DeviceFoundHandler,也就是下面貼出來的代碼,
[java] view plaincopy
  1.  private class DeviceFoundHandler implements Handler {  
  2.     public void onReceive(Context context, Intent intent,  
  3.             BluetoothDevice device) {  
  4.        ........................  
  5.         // TODO Pick up UUID. They should be available for 2.1 devices.  
  6.         // Skip for now, there's a bluez problem and we are not getting uuids even for 2.1.  
  7.         CachedBluetoothDevice cachedDevice = mDeviceManager.findDevice(device);  
  8.         if (cachedDevice == null) {  
  9.             cachedDevice = mDeviceManager.addDevice(mLocalAdapter, mProfileManager, device);  
  10.             Log.d(TAG, "DeviceFoundHandler created new CachedBluetoothDevice: "  
  11.                     + cachedDevice);  
  12.             // callback to UI to create Preference for new device  
  13.             dispatchDeviceAdded(cachedDevice);  
  14.         }  
  15.       ......................  
  16.     }  
  17. }  
        在if語句中dispatchDeviceAdded()向界面分發消息,最後處理消息的地方在這裡,已經到settings應用裡了
[java] view plaincopy
  1. public void onDeviceAdded(CachedBluetoothDevice cachedDevice) {                   
  2.    if (mDevicePreferenceMap.get(cachedDevice) != null) {                          
  3.        return;                                                                    
  4.    }                                                                              
  5.                                                                                   
  6.    // Prevent updates while the list shows one of the state messages              
  7.    if (mLocalAdapter.getBluetoothState() != BluetoothAdapter.STATE_ON) return;    
  8.                                                                                   
  9.    if (mFilter.matches(cachedDevice.getDevice())) {                               
  10.        createDevicePreference(cachedDevice);                                      
  11.    }                                                                              
  12. }                     
         上面代碼中最後一個分支就是界面顯示要做的事了,從settings界面開始再到settings界面顯示出搜索到藍芽結束,後面的代碼不再寫了,本文關心的東東到此結束。
[java] view plaincopy
  1.   void createDevicePreference(CachedBluetoothDevice cachedDevice) {  
  2.     BluetoothDevicePreference preference = new BluetoothDevicePreference(  
  3.             getActivity(), cachedDevice);  
  4.   
  5.     initDevicePreference(preference);  
  6.     mDeviceListGroup.addPreference(preference);  
  7.     mDevicePreferenceMap.put(cachedDevice, preference);  
  8. }  
        到目前為止,包括前面的打開流程分析,還僅是針對代碼流程做的分析,對於藍芽協議方面東西還沒有涉及,比如藍芽是如何發現其它藍芽設備,這個流程究竟是怎麼工作還不是很清楚,後續會儘量關注這些問題,估計看起來就沒那麼容易,歡迎有經驗的朋友指點一二,當然對於本文不足,歡迎拍磚討論。分享是快樂的,謝謝!