ConnectivityManager ConnectivityService in Android

 connectivity manager (封裝了ConnectivityService)

 
管理多種連接方式 mobile/wifi/ether/bt/usb-tether/etc   要新加一個type,需要很複雜的改動(patch)
同一時刻只有一個active的數據連接 見NetworkInfo  getActiveNetworkInfo()  (也就是說不支持3G wifi同時開着)    
(但是,將來很可能會改成支持多個網絡同時連接,見:
    private static class RadioAttributes {
        public int mSimultaneity;
...
 
connection type的優先順序 
 
ConncectionManager.java 提供了getNetworkPreference setNetworkPreference方法
preference的網絡可以是任何type,比如mobile, wifi, bt都可以。  
目前preference網絡只能有一個  也就是不支持優先級列表,比如最prefer wifi,其次prefer ether,再次prefer mobile 3G
(但是,將來很可能改成支持優先列表,見:    
// priority order of the nettrackers
// (excluding dynamically set mNetworkPreference)
// TODO - move mNetworkTypePreference into this
   private int[] mPriorityList;、
 
 
當調用setNetworkPreference時,perfer的網絡會被持久化,記錄在數據庫中
當調用getNetworkPreference時,會從數據庫中取回,如果沒有記錄,就默認wifi
(詳見ConnectivityManager.java的hard code : public static final int DEFAULT_NETWORK_PREFERENCE = TYPE_WIFI;)
這裏,只是說優先wifi連接模式,不能設置哪個AP/SSID優先
 
(AP/SSID優先也能做,另有N種實現方法,都不難)
 
已經保存了優先網絡,如何enforcePreference? todo
 
當WiFi連上,已經存在的連接(e.g. mobile GPRS,3G,etc)會自動斷開 (因爲同時不能存在兩個active連接)
當離開wifi熱點,信號不足時,wifi斷開,如仍有數據連接請求,會嘗試使用其它連接,如mobile。
這些邏輯保存在ConnectivityService.java中handleConnected handleDisconnect等方法中,未深入研究
 
 
network config, radio attribute等配置文件在 /frameworks/base/core/res/res/values/config.xml
(也可針對device定製config.xml,保存在device目錄中) 
通過讀這個文件初始化ConnectivityService的 mNetConfigs mRadioAttributes,見下面xml comments
 
    107     <!-- An Array of "[Connection name],[ConnectivityManager.TYPE_xxxx],
    108          [associated radio-type],[priority],[restoral-timer(ms)],[dependencyMet]  -->
    109     <!-- the 5th element "resore-time" indicates the number of milliseconds to delay
    110          before automatically restore the default connection.  Set -1 if the connection
    111          does not require auto-restore. -->
    112     <!-- the 6th element indicates boot-time dependency-met value. -->
    113     <string-array translatable="false" name="networkAttributes">
    114         <item>"wifi,1,1,1,-1,true"</item>
    115         <item>"mobile,0,0,0,-1,true"</item>
    116         <item>"mobile_mms,2,0,2,60000,true"</item>
    117         <item>"mobile_supl,3,0,2,60000,true"</item>
    118         <item>"mobile_hipri,5,0,3,60000,true"</item>
    119         <item>"mobile_fota,10,0,2,60000,true"</item>
    120         <item>"mobile_ims,11,0,2,60000,true"</item>
    121         <item>"mobile_cbs,12,0,2,60000,true"</item>
    122         <item>"wifi_p2p,13,1,0,-1,true"</item>
    123     </string-array>
    ...
    138     <string-array translatable="false" name="radioAttributes">
    139         <item>"1,1"</item>
    140         <item>"0,1"</item>
    141     </string-array>
另外,ConnectivityService與DNS,***,Tether,Proxy相關的代碼,均未深入研究
ConnectivityManager提供了抽象的連接管理功能,與具體連接物理層無關,
例如wifi的鏈接管理,需要看 wifi manager/ wifi service , which I did not investigate yet. 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章