生活随笔
收集整理的這篇文章主要介紹了
Android 4.4 Kitkat 使能有线网络 Ethernet
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
背景
Android kitkat 默認已經(jīng)支持 Ethernet 有線網(wǎng)絡(luò),只要稍微配置,便可以直接使用,測試結(jié)果,網(wǎng)絡(luò)瀏覽器和下載都沒有沒有問題,而且系統(tǒng)可以做到與 wifi 共存,互相不影響功能,這里簡單介紹如何使能 Ethernet,并簡要分析其代碼和流程。
Linux 配置部分
Linux 需要能夠支持有線網(wǎng)絡(luò),生成 eth 網(wǎng)絡(luò)設(shè)備節(jié)點。
Android 配置
overlay
主要是 overlay 里面添加 Ethernet 網(wǎng)絡(luò)類型支持: ?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 對應(yīng) Ethernet 的網(wǎng)絡(luò)類型,其定義在?
ConnectivityManager.java 中
? ? ? ? ?? public?static?final?int?TYPE_ETHERNET????=?9;?? init.<board>.rc
init 里面需要添加 dhcp 和 ip renew 服務(wù)
#?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 的構(gòu)造函數(shù)里面將會讀取?radioAttributes 里面的網(wǎng)絡(luò)配置
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;?? }?? 根據(jù)網(wǎng)絡(luò)配置數(shù)據(jù),將會創(chuàng)建?EthernetDataTracker , 并開始監(jiān)聽?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 開頭的有線網(wǎng)絡(luò)設(shè)備,打開并開始做 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 ?網(wǎng)路已經(jīng)連接,這個時候上層應(yīng)用便可以開始執(zhí)行網(wǎng)絡(luò)操作。
mNetworkInfo.setDetailedState(DetailedState.CONNECTED,?null,?mHwAddr);?? Message?msg?=?mCsHandler.obtainMessage(EVENT_STATE_CHANGED,?mNetworkInfo);?? msg.sendToTarget();?? 如果有多個網(wǎng)口呢,這個?EthernetDataTracker 顯然不能滿足要求,必須對其進行擴展。
總結(jié)
以上是生活随笔為你收集整理的Android 4.4 Kitkat 使能有线网络 Ethernet的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。