這些路徑下的文件是不分平臺及Android大版本的
創(chuàng)新互聯(lián)主營普陀網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營網(wǎng)站建設(shè)方案,重慶App定制開發(fā),普陀h5微信小程序定制開發(fā)搭建,普陀網(wǎng)站營銷推廣歡迎普陀等地區(qū)企業(yè)咨詢
packages/../framework/src/com/../framework/wifi/LocalWifiSetting.java
packages/../framework/src/com/../framework/wifi/WifiAccessPoint.java
packages/../framework/src/com/../framework/wifi/WifiDevInfo.java
由于android 大版本不同,差異化的東西放到了framework/android/對應(yīng)大版本/wifi 中
packages/../framework/src/com/../framework/wifi/LocalWifiSetting.java上層通過LocalWifiSetting.getInstance(mContext)獲取單例對象,構(gòu)造函數(shù)中注冊wifi相關(guān)的廣播代碼如下:
public static LocalWifiSetting getInstance(Context context) {
return (LocalWifiSetting) Context.getInstance(context).getSystemService(Context.WIFI_SETTING_SERVICE);
}
public LocalWifiSetting (Context context) {
mWifiManager =(WifiManager)context.getSystemService(Context.WIFI_SERVICE);
mContext=context;
mScanner = new Scanner();
mFilter = new IntentFilter();
mFilter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
mFilter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
mFilter.addAction(WifiManager.NETWORK_IDS_CHANGED_ACTION);
mFilter.addAction(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION);
mFilter.addAction(WifiManager.CONFIGURED_NETWORKS_CHANGED_ACTION);
mFilter.addAction(WifiManager.LINK_CONFIGURATION_CHANGED_ACTION);
mFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
mFilter.addAction(WifiManager.RSSI_CHANGED_ACTION);
mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
handleEvent(context, intent);
}
};
mContext.registerReceiver(mReceiver, mFilter);
}
1、WIFI_STATE_CHANGED_ACTION:WIFI模塊硬件狀態(tài)改變的廣播,看到的直觀表征有, WIFI開啟, WIFI關(guān)閉; 而在實(shí)際的過程中, WIFI 從開啟到關(guān)閉, 或是從關(guān)閉到開啟, 需要經(jīng)歷三個(gè)狀態(tài), 以開啟WIFI為例, 其要經(jīng)過的狀態(tài)分別為: 已關(guān)閉, 開啟中, 已開啟. 關(guān)閉WIFI則相反, 分為為: 已開啟, 關(guān)閉中, 關(guān)閉. 接收到這個(gè)廣播后, 你可以從INTENT中取出當(dāng)前WIFI硬件的變化狀態(tài), 可以使用 INT 值來區(qū)別; 這個(gè)KEY是: EXTRA_WIFI_STATE, 可能得到的值為:0, 1, 2, 3, 4; 當(dāng)然除了這種獲取方式, 也可以通過WIFIMANAGER對象GETWIFISTATE() 獲取這個(gè)值. 也可以從 INTENT 中取出另外一個(gè)值, 表示之前WIFI模塊的狀態(tài),對應(yīng)的KEY, 就是: EXTRA_PREVIOUS_WIFI_STATE;
2、WifiManager.SCAN_RESULTS_AVAILABLE_ACTION: 掃描到一個(gè)熱點(diǎn), 并且此熱點(diǎn)達(dá)到可用狀態(tài) 會觸發(fā)此廣播; 此時(shí), 你可以通過 wifiManager.getScanResult() 來取出當(dāng)前所掃描到的 ScanResult; 同時(shí), 你可以從intent中取出一個(gè)boolean值; 如果此值為true, 代表著掃描熱點(diǎn)已完全成功; 為false, 代表此次掃描不成功, ScanResult 距離上次掃描并未得到更新;
3、WifiManager.NETWORK_IDS_CHANGED_ACTION: 在網(wǎng)絡(luò)配置, 保存, 添加, 連接, 斷開, 忘記的操作過后, 均會對 WIFI 熱點(diǎn)配置形成影響, 在shell下, 如果有root權(quán)限, 可以在執(zhí)行上述動作前后, 分別瀏覽 /data/misc/wifi/wpa_supplicant.conf 應(yīng)該是有本質(zhì)的變化, 此時(shí)會收到此廣播.
4、WifiManager.SUPPLICANT_STATE_CHANGED_ACTION: 建立連接的熱點(diǎn)正在發(fā)生變化. 象征變化的相關(guān)類為: SupplicantState, 你可以在接收到此廣播時(shí), 觀察到已經(jīng)建立連接的熱點(diǎn)的整個(gè)連接過程, 包含可能會出現(xiàn)連接錯(cuò)誤的錯(cuò)誤碼.
5、WifiManager.CONFIGURED_NETWORKS_CHANGED_ACTION: 官方的注釋是這么說的, 廣播已配置的網(wǎng)絡(luò)發(fā)生變化, 可由添加, 修改, 刪除網(wǎng)絡(luò)的觸發(fā). 當(dāng)從 intent 中取出key值為 EXTRA_MULTIPLE_NETWORKS_CHANGED, 其值為 true 時(shí), 那么字段 EXTRA_WIFI_CONFIGURATION 中取出來的配置已經(jīng)過時(shí), 不是最新配置了。
6、WifiManager.LINK_CONFIGURATION_CHANGED_ACTION: WIFI 連接配置發(fā)生改變的廣播. 此時(shí), 網(wǎng)路連接功能封裝 LinkProperties 和 NetworkCapabilities 可能發(fā)生變化.
7、WifiManager.NETWORK_STATE_CHANGED_ACTION: WIFI 連接狀態(tài)發(fā)生改變的廣播. 可以從 intent 中取得 NetworkInfo, 此時(shí) NetworkInfo 中提供了連接的新狀態(tài), 如果連接成功, 可以獲取當(dāng)前連接網(wǎng)絡(luò)的 BSSID, 和 WifiInfo.
8、WifiManager.RSSI_CHANGED_ACTION: WIFI 熱點(diǎn)信號強(qiáng)度發(fā)生變化的廣播. 可以獲取當(dāng)前變化熱點(diǎn)的最新的信號強(qiáng)度.
接收到廣播之后就會handleEvent:
private void handleEvent(Context context, Intent intent) {
String action = intent.getAction();
if (WifiManager.WIFI_STATE_CHANGED_ACTION.equals(action)) {
updateWifiState(intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE,
WifiManager.WIFI_STATE_UNKNOWN));
}else if (WifiManager.SCAN_RESULTS_AVAILABLE_ACTION.equals(action)) {
updateWifiAccessPoints();
} else if (WifiManager.SCAN_RESULTS_AVAILABLE_ACTION.equals(action) ||
WifiManager.CONFIGURED_NETWORKS_CHANGED_ACTION.equals(action) ||
WifiManager.LINK_CONFIGURATION_CHANGED_ACTION.equals(action)) {
updateWifiAccessPoints();
} else if (WifiManager.SUPPLICANT_STATE_CHANGED_ACTION.equals(action)) {
SupplicantState state = (SupplicantState) intent.getParcelableExtra(
WifiManager.EXTRA_NEW_STATE);
if (!mConnected.get() && SupplicantState.isHandshakeState(state)) {
updateConnectionState(WifiInfo.getDetailedStateOf(state));
} else {
updateConnectionState(null);
}
int authState = intent.getIntExtra(WifiManager.EXTRA_SUPPLICANT_ERROR, -1);
if ( authState == WifiManager.ERROR_AUTHENTICATING ) {
updateWifiAccessPoints();
}
DetailedState detailedState = WifiInfo.getDetailedStateOf(state);
Log.i(TAG, "action: " + action + "state:" + detailedState + "authState:" + authState);
} else if (WifiManager.NETWORK_STATE_CHANGED_ACTION.equals(action)) {
NetworkInfo info = (NetworkInfo) intent.getParcelableExtra(
WifiManager.EXTRA_NETWORK_INFO);
mConnected.set(info.isConnected());
updateWifiAccessPoints();
updateConnectionState(info.getDetailedState());
} else if (WifiManager.RSSI_CHANGED_ACTION.equals(action)) {
updateConnectionState(null);
}
}
我司電視設(shè)置,wifi狀態(tài)開機(jī)都是打開的,如果開機(jī)為wifi網(wǎng)絡(luò)連接的話,就會接受到收到WIFI_STATE_CHANGED_ACTION,CONFIGURED_NETWORKS_CHANGED_ACTION,NETWORK_STATE_CHANGED_ACTION,SUPPLICANT_STATE_CHANGED_ACTION,這個(gè)狀態(tài)是從開機(jī)到wifi認(rèn)證的過程,接著我們分別看下我們的代碼流程。
WIFI_STATE_CHANGED_ACTION里面實(shí)現(xiàn)updateWifiState
private void updateWifiState(int state) {
dispatchWifiStateChanged(state);
switch (state) {
case WifiManager.WIFI_STATE_ENABLED:
//mScanner.resume();
return;
case WifiManager.WIFI_STATE_ENABLING:
break;
case WifiManager.WIFI_STATE_DISABLED:
break;
}
mLastWifiInfo = null;
mLastState = null;
mScanner.pause();
}
這里其實(shí)并沒有做什么,只是清空全局變量的一些記錄。然后CONFIGURED_NETWORKS_CHANGED_ACTION:
public void updateWifiAccessPoints() {
final int wifiState = mWifiManager.getWifiState();
switch (wifiState) {
case WifiManager.WIFI_STATE_ENABLED:
mWifiAccessPoints = constructWifiAccessPoints();
break;
case WifiManager.WIFI_STATE_ENABLING:
break;
case WifiManager.WIFI_STATE_DISABLING:
break;
case WifiManager.WIFI_STATE_DISABLED:
break;
}
dispatchWifiStateChanged(wifiState);
/*
* mWifiAccessPoints is null when wifi is disabled,which will throw NullPointerException
*/
List<WifiAccessPoint> tmpList;
if (mWifiAccessPoints == null) {
tmpList = new ArrayList<WifiAccessPoint>();
} else {
tmpList = new ArrayList<WifiAccessPoint>(mWifiAccessPoints);
}
dispatchWifiAccessPointsChanged(tmpList);
}
到這個(gè)時(shí)候,wifiManger的狀態(tài)就是WIFI_STATE_ENABLED。(如果想要了解開機(jī)如何自動連接上次wifi及判斷之前有沒有連接過wifi。請自己閱讀源碼,在此不做交流)接著就是我們比較重要的constructWifiAccessPoints()這個(gè)函數(shù):
private List<WifiAccessPoint> constructWifiAccessPoints() {
Log.v(TAG, "constructWifiAccessPoints");
ArrayList<WifiAccessPoint> accessPoints = new ArrayList<WifiAccessPoint>();
/** Lookup table to more quickly update WifiAccessPoints by only considering objects with the
* correct SSID. Maps SSID -> List of WifiAccessPoints with the given SSID.
* 獲取保存過的wifi信息
*
*/
Multimap<String, WifiAccessPoint> apMap = new Multimap<String, WifiAccessPoint>();
final List<WifiConfiguration> configs = mWifiManager.getConfiguredNetworks();
if (configs != null) {
Log.v(TAG, "configs size = " + configs.size());
for (WifiConfiguration config : configs) {
WifiAccessPoint accessPoint = new WifiAccessPoint(mContext, config);
if (mLastWifiInfo != null && mLastState != null)
accessPoint.update(mLastWifiInfo, mLastState);
accessPoints.add(accessPoint);
apMap.put(accessPoint.ssid, accessPoint);
}
}
//獲取掃描到但未保存過的wifi
final List<ScanResult> results = mWifiManager.getScanResults();
if (results != null) {
for (ScanResult result : results) {
// Ignore hidden and ad-hoc networks.
if (result.SSID == null || result.SSID.length() == 0 ||
result.capabilities.contains("[IBSS]")) {
continue;
}
boolean found = false;
for (WifiAccessPoint accessPoint : apMap.getAll(result.SSID)) {
if (accessPoint.update(result))
found = true;
}
if (!found) {
WifiAccessPoint accessPoint = new WifiAccessPoint(mContext, result);
if (mLastWifiInfo != null && mLastState != null)
accessPoint.update(mLastWifiInfo, mLastState);
accessPoints.add(accessPoint);
apMap.put(accessPoint.ssid, accessPoint);
}
}
}
// Pre-sort accessPoints to speed preference insertion
Collections.sort(accessPoints);
return accessPoints;
}
這個(gè)函數(shù)的作用主要是用來獲取WIfiAccessPoint,這個(gè)類主要用來存儲wifi的信息。constructWifiAccessPoints這個(gè)函數(shù)首先通過mWifiManager.getConfiguredNetworks之前有鏈接過的wifi信息,保存到ApMap,然后再保存掃描到的wifi。接著就是NETWORK_STATE_CHANGED_ACTION。
private void updateConnectionState(DetailedState state) {
/* sticky broadcasts can call this when wifi is disabled */
if (!mWifiManager.isWifiEnabled()) {
mScanner.pause();
return;
}
if (state == DetailedState.OBTAINING_IPADDR) {
mScanner.pause();
} else {
//mScanner.resume();
}
mLastWifiInfo = mWifiManager.getConnectionInfo();
Log.d(TAG, "updateConnectionState " + state);
if (state != null) {
mLastState = state;
}
if (mWifiAccessPoints != null) {
for (WifiAccessPoint accessPoint : mWifiAccessPoints) {
accessPoint.update(mLastWifiInfo, mLastState);
}
}
}
這里主要更新wifi的連接狀態(tài),IDLE:空閑 SCANNING:正在掃描 CONNECTING:連接中 AUTHENTICATING:正在進(jìn)行身份驗(yàn)證...
OBTAINING_IPADDR:正在獲取Ip地址 CONNECTED:已連接 SUSPENDED:已暫停 DISCONNECTING:正在斷開連接...
DISCONNECTED:已斷開 FAILED:失敗 BLOCKED:已阻止 VERIFYING_POOR_LINK:暫時(shí)關(guān)閉(網(wǎng)絡(luò)狀況不佳)
CAPTIVE_PORTAL_CHECK:判斷是否需要瀏覽器二次登錄,主要有以上幾種狀態(tài)。當(dāng)我們收到SUPPLICANT_STATE_CHANGED_ACTION的廣播時(shí),在handleEvent中的處理如下:
SupplicantState state = (SupplicantState) intent.getParcelableExtra(
WifiManager.EXTRA_NEW_STATE);
if (!mConnected.get() && SupplicantState.isHandshakeState(state)) {
updateConnectionState(WifiInfo.getDetailedStateOf(state));
} else {
// During a connect, we may have the supplicant
// state change affect the detailed network state.
// Make sure a lost connection is updated as well.
updateConnectionState(null);
}
int authState = intent.getIntExtra(WifiManager.EXTRA_SUPPLICANT_ERROR, -1);
if ( authState == WifiManager.ERROR_AUTHENTICATING ) {
updateWifiAccessPoints();
}
DetailedState detailedState = WifiInfo.getDetailedStateOf(state);
Log.i(TAG, "action: " + action + "state:" + detailedState + "authState:" + authState);
這里主要處理wifi認(rèn)證失敗后的處理了。這部分主要是updateWifiAccessPoints(),下面看下packages/../framework/src/com/../framework/wifi/WifiAccessPoint.java,這里主要用于記錄每個(gè)wifi的詳細(xì)信息,在每次構(gòu)建的時(shí)候狀態(tài)更新的時(shí)候都會去重新構(gòu)造每個(gè)WifiAccessPoint,然后fresh。
WifiAccessPoint(Context context, WifiConfiguration config) {
loadConfig(config);
refresh();
}
WifiAccessPoint(Context context, ScanResult result) {
loadResult(result);
refresh();
}
在refresh中主要是更新錯(cuò)誤原因:
private void refresh() {
if (getState() != null) { // This is the active connection
mDisableReason = -1;
} else if (getRssiLevel() == Integer.MAX_VALUE) { // Wifi out of range
mDisableReason = -1;
} else if (mConfig != null && mConfig.status == WifiConfiguration.Status.DISABLED) {
switch (mConfig.disableReason) {
case WifiConfiguration.DISABLED_AUTH_FAILURE:
mDisableReason = WifiConfiguration.DISABLED_AUTH_FAILURE;
break;
case WifiConfiguration.DISABLED_DHCP_FAILURE:
case WifiConfiguration.DISABLED_DNS_FAILURE:
case WifiConfiguration.DISABLED_UNKNOWN_REASON:
mDisableReason = mConfig.disableReason;
break;
}
} else { // In range, not disabled.
mDisableReason = -1;
}
}
認(rèn)證連接的就直接更新wifi狀態(tài)保存就行。那是如何通知上層wifi狀態(tài)發(fā)生改變的呢?這個(gè)主要是在packages/../framework/src/com/../framework/wifi/LocalWifiSetting.java中,這里寫了個(gè)接口,上層通過實(shí)現(xiàn)這個(gè)接口處理事件。我們在需要的地方觸發(fā)回調(diào)。
public interface WifiStateChangeListerner {
void onWifiAccessPointsChanage(List<WifiAccessPoint> accessPoints);
void onWifiStateChanage(int state);
}
private final Collection<WifiStateChangeListerner> mCallbacks = new ArrayList<WifiStateChangeListerner>();
//上層調(diào)用這里注冊回調(diào)
public void registerCallback(WifiStateChangeListerner callback) {
synchronized (mCallbacks) {
mCallbacks.add(callback);
}
}
public void unregisterCallback(WifiStateChangeListerner callback) {
synchronized (mCallbacks) {
mCallbacks.remove(callback);
}
}
//當(dāng)wifi的狀態(tài)發(fā)生改變的時(shí)候適配層觸發(fā)此函數(shù),回調(diào)上層實(shí)現(xiàn)
private void dispatchWifiStateChanged(int state) {
synchronized (mCallbacks) {
for (WifiStateChangeListerner callback : mCallbacks) {
callback.onWifiStateChanage(state);
}
}
}
//當(dāng)Wifi信息出現(xiàn)變化的時(shí)候適配層觸發(fā)此函數(shù),回調(diào)上層實(shí)現(xiàn)
private void dispatchWifiAccessPointsChanged(List<WifiAccessPoint> accessPoints) {
synchronized (mCallbacks) {
for (WifiStateChangeListerner callback : mCallbacks) {
callback.onWifiAccessPointsChanage(accessPoints);
}
}
}
到這里適配層wifi比較中要的邏輯基本結(jié)束,其他函數(shù)看函數(shù)名稱基本能夠了解大致含義:基本上是在packages/../framework/src/com/../framework/wifi/LocalWifiSetting.java中的查找、連接、獲取wifi信息、刪除保存wifi等等
###ethernet 模塊文檔
接著介紹一下以太網(wǎng)的流程。文件主要路徑:
packages/../framework/src/com/../framework/ethernet/LocalEthCallback.java
packages/../framework/src/com/../framework/ethernet/LocalEthernetDevInfo.java
packages/../framework/src/com/../framework/ethernet/LocalEthManager.java
類似于wifi,主要的類是LocalEthManager,但這個(gè)里面基本上是抽象的方法與類,具體的實(shí)現(xiàn)如下:這里因?yàn)楫?dāng)時(shí)4.4的時(shí)候以太網(wǎng)放到mstar下,到5.0的時(shí)候發(fā)現(xiàn)會隨大版本的變化而發(fā)生改變,這部分的實(shí)現(xiàn)是放到了packages/../framework/android/對應(yīng)的大版本/ehernet/下,可以按照對應(yīng)的大版本進(jìn)行分析,我們先以android4.4的來分析。
所以可以有兩種實(shí)現(xiàn)方式:
./framework/hardware/mstar/tv1/src/ethernet/MstarLocalEthManager.java
./framework/hardware/mtk/tv1/src/ethernet/MtkLocalEthManager.java
./framework/hardware/softwinner/tv1/src/ethernet/SoftwinnerLocalEthManager.java
./framework/hardware/hisilicon/tv1/src/ethernet/HisiLocalEthManager.java
./framework/hardware/amlogic/tv1/src/ethernet/AmlogicLocalEthManager.java
./framework/android/lollipop/src/ethernet/LocalEthManager.java
這些是LocalEthManager.java的具體實(shí)現(xiàn),編譯的時(shí)候會android大版本優(yōu)先,如果android大版本下沒有存在,就根據(jù)配置,走相應(yīng)的板卡或者芯片廠商下邊。
類似與wifi的流程有點(diǎn)類似,上層通過LocalWifiSetting.getInstance(mContext)獲取單例對象:
public LocalEthManager(Context context) {
if (DEBUG)
Log.d(TAG, "LocalEthManager construct");
mEthernetManager = (EthernetManager) context.getSystemService(Context.ETHERNET_SERVICE);
mConnectivityManager = (ConnectivityManager) context.getSystemService(
Context.CONNECTIVITY_SERVICE);
IntentFilter intentfilter = new IntentFilter();
intentfilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
context.registerReceiver(mBroadcastReceiver, intentfilter);
}
其實(shí)ethernet的實(shí)現(xiàn)方式更簡單,主要是監(jiān)聽CONNECTIVITY_ACTION = "android.net.conn.CONNECTIVITY_CHANGE",接收到廣播,觸發(fā)連接狀態(tài)回調(diào),給上層使用
private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
int type = intent.getIntExtra(ConnectivityManager.EXTRA_NETWORK_TYPE, ConnectivityManager.TYPE_NONE);
if (type == ConnectivityManager.TYPE_ETHERNET) {
NetworkInfo networkInfo = mConnectivityManager.getNetworkInfo(ConnectivityManager.TYPE_ETHERNET);
if (DEBUG)
Log.d(TAG, "Eth state change action");
if (networkInfo != null && networkInfo.isConnected()) {
Log.d(TAG,"LocalEthManager.EVENT_ETH_CONNECTED");
dispatchEthEvent(LocalEthManager.EVENT_ETH_CONNECTED);
} else {
Log.d(TAG,"LocalEthManager.EVENT_ETH_DISCONNECTED");
dispatchEthEvent(LocalEthManager.EVENT_ETH_DISCONNECTED);
}
}
}
};
其他的函數(shù)都是給上層獲取ethernet相關(guān)信息的。
isEthernetAvailable()---------------是否支持以太網(wǎng)
getSavedEthConfig()---------------獲取保存的以太網(wǎng),分靜態(tài)IP及非靜態(tài),構(gòu)建相應(yīng)的LocalEthernetDevInfo
updateEthDevInfo()----------------更新以太網(wǎng)相關(guān)信息(上層應(yīng)該未使用)
getDhcpInfo()
文章名稱:android上層wifi模塊調(diào)用分析
當(dāng)前URL:http://www.rwnh.cn/article16/igpcgg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供營銷型網(wǎng)站建設(shè)、搜索引擎優(yōu)化、網(wǎng)站內(nèi)鏈、做網(wǎng)站、網(wǎng)站排名、商城網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)