Android wifi 從串連態自動斷開的解決辦法(dhcp導致)【轉】

來源:互聯網
上載者:User

標籤:介面   com   art   get   max   android   增加   csdn   nbsp   

本文轉載自:http://blog.csdn.net/DKBDKBDKB/article/details/38490201

對wifi部分的代碼流程已經看了段時間,前兩天終於解決了工作中遇到的一個wifi問題,問題描述及解決過程如下:

硬體平台:iMx53

軟體平台:Android2.3

bug描述:1,選中熱點,輸入密碼之後,會顯示“正在擷取ip地址。。。”,之後變為已儲存而沒有串連。

                  2,系統在wifi串連的前提下,重新上電,無法自動連接已儲存的wifi熱點。

                  3,系統wifi串連後,過一段時間自動斷開,wifi表徵圖變暗。

 

       首先,感覺wifi串連後自動斷開這種情況,Android上層應該還算是比較智能的,已串連的wifi在斷開後肯定會有嘗試自動重連的邏輯實現,但是從log看,connection是有reset和reconnect但是均失敗了,沒有重連成功。所以開始繼續向下分析重連失敗的原因,最終發現了dhcp的疑點很大:

 DHCP request error:Timed out waiting for dhcpcd to start

       這是出問題的時候log中通常出現的部分:dhcp start 失敗。代碼在/system/core/libnetutils/dhcp_utils.c 裡邊實現。start失敗的原因是由於在指定的時間內,init.svc.dhcpcd_wlan0這個prop沒有變為“running”態,於是,我在wait_for_property函數中做了重新開啟的改動,添加代碼如下:

 

[cpp] view plain copy 
  1. if (strcmp(name, "init.svc.dhcpcd_wlan0") == 0 && \  
  2.     strcmp(desired_value, "running") == 0) {  
  3.     if (maxnaps == 7) {  
  4.         property_set("ctl.start", "dhcpcd_wlan0");    
  5.         LOGD("kevin 1st set init.svc.dhcpcd_wlan0");  
  6.     } else if (maxnaps == 4) {  
  7.         property_set("ctl.start", "dhcpcd_wlan0");    
  8.         LOGD("kevin 2nd set init.svc.dhcpcd_wlan0");  
  9.     }  
  10. }  

 

        問題有了很大改善,此後不會再出現start失敗的問題,但隨之而來的問題出現了:系統重新上電之後,wifi表徵圖顯示已串連,但是點擊串連上的wifi熱點,在彈出的的視窗裡邊沒有ip地址,而此時上層的app確實不能上網,因為點擊熱點的彈窗是通過get_property介面擷取的一系列屬性資訊,所以分析是兩種可能:1,上層沒捕獲到property;2,底層沒有把相應的property set進去。第一種可能性很低,所以直接從第二種情況入手分析,發現這些屬性是在dhcp_start接近尾聲的時候,通過dhcp的一些指令碼執行set進去(/external/dhcpcd/dhcpcd-hooks 指令碼路徑),設定成功會將property “dhcp.wlan0.result” 設定為“OK”, 此時才能保證上層可擷取相應的property, 鑒於此,分析還是dhcp start沒有正常啟動,因此在/system/core/libnetutils/dhcp_utils.c 裡實現的自己的新介面:dhcp_reset(),具體實現如下

 

[cpp] view plain copy 
  1. static const char DHCP_SERVER[]         ="dhcpcd_wlan0";  
  2. static const char DHCP_PROP[]           ="init.svc.dhcpcd_wlan0";  
  3. static const char DHCP_RES[]            ="dhcp.wlan0.result";  
  4.   
  5. static int dhcp_reset()  
  6. {  
  7.     int stop_wait = 5;  
  8.     int start_wait = 10;  
  9.     int result_wait = 20;  
  10.     char kvalue[PROPERTY_VALUE_MAX] = {‘\0‘};  
  11.   
  12.     LOGD("kevin start dhcp_reset");  
  13.   
  14.     property_set("ctl.stop", DHCP_SERVER);  
  15.     while (stop_wait-- > 0) {  
  16.         usleep(500000);  
  17.         if (property_get(DHCP_PROP, kvalue, NULL)) {  
  18.             if (strcmp(kvalue, "stopped") == 0) {  
  19.                 LOGD("kevin: property name is %s get_Value \  
  20.           is %s and desired_value is stopped", DHCP_PROP, kvalue);  
  21.                 break;  
  22.             }  
  23.         }  
  24.     }  
  25.   
  26.     property_set("ctl.start", DHCP_SERVER);  
  27.     while (start_wait-- > 0) {  
  28.         usleep(500000);  
  29.         if (property_get(DHCP_PROP, kvalue, NULL)) {  
  30.             if (strcmp(kvalue, "running") == 0) {  
  31.                 LOGD("kevin: property name is %s get_Value \  
  32.          is %s and desired_value is running", DHCP_PROP, kvalue);  
  33.                 break;  
  34.             }  
  35.         }  
  36.     }  
  37.   
  38.     while (result_wait-- > 0) {  
  39.         usleep(500000);  
  40.         if (property_get(DHCP_RES, kvalue, NULL)) {  
  41.             if (strcmp(kvalue, "ok") == 0) {  
  42.                 LOGD("kevin: property name is %s get_Value \  
  43.              is %s and desired_value is ok", DHCP_RES, kvalue);  
  44.                 return 0;  
  45.             }  
  46.         }  
  47.     }  
  48.   
  49.     return -1; /* failure */  
  50. }  

 



 

此外,wait_for_property()函數需要增加如下代碼:

 

[cpp] view plain copy 
  1. if (strcmp(name, "dhcp.wlan0.result") == 0 \  
  2.   && (maxnaps == 12|| maxnaps == 8)) {  
  3.             if (maxnaps == 12)  
  4.                 LOGD("kevin 1st reset_dhcp");  
  5.             else  
  6.                 LOGD("kevin 2nd reset_dhcp");  
  7.             if (dhcp_reset() == 0)  
  8.                 LOGD("kevin dhcp_reset success");  
  9.             else  
  10.                 LOGD("kevin dhcp_reset failed");  
  11. }  

 

 

 

        實現機制是在等待dhcp.wlan0.result最終有沒有正常set的過程中,伺機dhcp_reset,判斷依據是,超出了正常啟動並設定屬性的時間段後,則調用dhcp_reset重新啟動dhcp服務,注意此時不能盲目的直接利用“ctl.start”開啟dhcp,因為可能會導致系統中同時運行多個dhcp進程,因此需要先stop,然後在start(這是我的個人理解)。

至此,dhcp導致的wifi一系列問題告一段落,宣布解決!

如有任何問題,歡迎討論,如有錯誤,還望高人指點。

 

Android wifi 從串連態自動斷開的解決辦法(dhcp導致)【轉】

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.