生活随笔
收集整理的這篇文章主要介紹了
Android 4.4 Kitkat 使能有线网络 Ethernet
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
背景
Android kitkat 默認已經支持 Ethernet 有線網絡,只要稍微配置,便可以直接使用,測試結果,網絡瀏覽器和下載都沒有沒有問題,而且系統可以做到與 wifi 共存,互相不影響功能,這里簡單介紹如何使能 Ethernet,并簡要分析其代碼和流程。
Linux 配置部分
Linux 需要能夠支持有線網絡,生成 eth 網絡設備節點。
Android 配置
overlay
主要是 overlay 里面添加 Ethernet 網絡類型支持: ?frameworks/base/core/res/res/values/config.xml?
<string-array?translatable="false"?name="radioAttributes">?? ????<item>"1,1"</item>?? ????<item>"7,1"</item>?? ????<item>"9,1"</item>?? </string-array>?? 其中 9 對應 Ethernet 的網絡類型,其定義在?
ConnectivityManager.java 中
? ? ? ? ?? public?static?final?int?TYPE_ETHERNET????=?9;?? init.<board>.rc
init 里面需要添加 dhcp 和 ip renew 服務
#?DHCPCD?? #?#?eth0?? service?dhcpcd_eth0?/system/bin/dhcpcd?-ABKL?? ????class?main?? ????disabled?? ????oneshot?? ?? #?IP?Renew?? #?#?eth0?? service?iprenew_eth0?/system/bin/dhcpcd?-n?? ????class?main?? ????disabled?? ????oneshot?? 流程分析
ConnectivityService
ConnectivityService 的構造函數里面將會讀取?radioAttributes 里面的網絡配置
public?ConnectivityService(Context?context,?INetworkManagementService?netManager,?? ????????INetworkStatsService?statsService,?INetworkPolicyManager?policyManager,?? ????????NetworkFactory?netFactory)?{?? ????if?(DBG)?log("ConnectivityService?starting?up");?? ?? String[]?raStrings?=?context.getResources().getStringArray(?? ????????com.android.internal.R.array.radioAttributes);?? for?(String?raString?:?raStrings)?{?? ????RadioAttributes?r?=?new?RadioAttributes(raString);?? ????if?(VDBG)?log("raString="?+?raString?+?"?r="?+?r);?? ????if?(r.mType?>?ConnectivityManager.MAX_RADIO_TYPE)?{?? ????????loge("Error?in?radioAttributes?-?ignoring?attempt?to?define?type?"?+?r.mType);?? ????????continue;?? ????}?? ????if?(mRadioAttributes[r.mType]?!=?null)?{?? ????????loge("Error?in?radioAttributes?-?ignoring?attempt?to?redefine?type?"?+?? ????????????????r.mType);?? ????????continue;?? ????}?? ????mRadioAttributes[r.mType]?=?r;?? }?? 根據網絡配置數據,將會創建?EthernetDataTracker , 并開始監聽?startMonitoring
?? for?(int?targetNetworkType?:?mPriorityList)?{?? ????final?NetworkConfig?config?=?mNetConfigs[targetNetworkType];?? ????final?NetworkStateTracker?tracker;?? ????try?{?? ????????tracker?=?netFactory.createTracker(targetNetworkType,?config);?? ????????mNetTrackers[targetNetworkType]?=?tracker;?? ????}?catch?(IllegalArgumentException?e)?{?? ????????Slog.e(TAG,?"Problem?creating?"?+?getNetworkTypeName(targetNetworkType)?? ????????????????+?"?tracker:?"?+?e);?? ????????continue;?? ????}?? ?? ????tracker.startMonitoring(context,?mTrackerHandler);?? ????if?(config.isDefault())?{?? ????????tracker.reconnect();?? ????}?? }?? EthernetDataTracker
EthernetDataTracker 將會尋找第一個以 eth 開頭的有線網絡設備,打開并開始做 dhcp
<!--?Regex?of?wired?ethernet?ifaces?-->?? <string?translatable="false"?name="config_ethernet_iface_regex">eth\\d</string>?? sIfaceMatch?=?context.getResources().getString(?? ??????????????????com.android.internal.R.string.config_ethernet_iface_regex);?? try?{?? ????final?String[]?ifaces?=?mNMService.listInterfaces();?? ????for?(String?iface?:?ifaces)?{?? ????????if?(iface.matches(sIfaceMatch))?{?? ????????????mNMService.setInterfaceUp(iface);?? ????????????InterfaceConfiguration?config?=?mNMService.getInterfaceConfig(iface);?? ?? ????????????if?(getEthernetCarrierState(iface)?==?1)?{?? ????????????????mIface?=?iface;?? ????????????????mLinkUp?=?true;?? ????????????????mNetworkInfo.setIsAvailable(true);?? ?? ????????????????if?(config?!=?null?&&?mHwAddr?==?null)?{?? ????????????????????mHwAddr?=?config.getHardwareAddress();?? ?? ????????????????????if?(mHwAddr?!=?null)?{?? ????????????????????????mNetworkInfo.setExtraInfo(mHwAddr);?? ????????????????????}?? ????????????????}?? ????????????}?? ?? ?????????????? ????????????NetworkUtils.stopDhcp(iface);?? ????????}?? ????}?? ?? ????reconnect();?? }?catch?(RemoteException?e)?{?? ????Log.e(TAG,?"Could?not?get?list?of?interfaces?"?+?e);?? }??
DHCP 成功后,通知NetworkManagementService ?網路已經連接,這個時候上層應用便可以開始執行網絡操作。
mNetworkInfo.setDetailedState(DetailedState.CONNECTED,?null,?mHwAddr);?? Message?msg?=?mCsHandler.obtainMessage(EVENT_STATE_CHANGED,?mNetworkInfo);?? msg.sendToTarget();?? 如果有多個網口呢,這個?EthernetDataTracker 顯然不能滿足要求,必須對其進行擴展。
總結
以上是生活随笔為你收集整理的Android 4.4 Kitkat 使能有线网络 Ethernet的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。