這些路徑下的文件是不分平臺及Android大版本的
創(chuàng)新互聯(lián)主營普陀網(wǎng)站建設的網(wǎng)絡公司,主營網(wǎng)站建設方案,重慶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/對應大版本/wifi 中
packages/../framework/src/com/../framework/wifi/LocalWifiSetting.java上層通過LocalWifiSetting.getInstance(mContext)獲取單例對象,構造函數(shù)中注冊wifi相關的廣播代碼如下:
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關閉; 而在實際的過程中, WIFI 從開啟到關閉, 或是從關閉到開啟, 需要經(jīng)歷三個狀態(tài), 以開啟WIFI為例, 其要經(jīng)過的狀態(tài)分別為: 已關閉, 開啟中, 已開啟. 關閉WIFI則相反, 分為為: 已開啟, 關閉中, 關閉. 接收到這個廣播后, 你可以從INTENT中取出當前WIFI硬件的變化狀態(tài), 可以使用 INT 值來區(qū)別; 這個KEY是: EXTRA_WIFI_STATE, 可能得到的值為:0, 1, 2, 3, 4; 當然除了這種獲取方式, 也可以通過WIFIMANAGER對象GETWIFISTATE() 獲取這個值. 也可以從 INTENT 中取出另外一個值, 表示之前WIFI模塊的狀態(tài),對應的KEY, 就是: EXTRA_PREVIOUS_WIFI_STATE;
2、WifiManager.SCAN_RESULTS_AVAILABLE_ACTION: 掃描到一個熱點, 并且此熱點達到可用狀態(tài) 會觸發(fā)此廣播; 此時, 你可以通過 wifiManager.getScanResult() 來取出當前所掃描到的 ScanResult; 同時, 你可以從intent中取出一個boolean值; 如果此值為true, 代表著掃描熱點已完全成功; 為false, 代表此次掃描不成功, ScanResult 距離上次掃描并未得到更新;
3、WifiManager.NETWORK_IDS_CHANGED_ACTION: 在網(wǎng)絡配置, 保存, 添加, 連接, 斷開, 忘記的操作過后, 均會對 WIFI 熱點配置形成影響, 在shell下, 如果有root權限, 可以在執(zhí)行上述動作前后, 分別瀏覽 /data/misc/wifi/wpa_supplicant.conf 應該是有本質(zhì)的變化, 此時會收到此廣播.
4、WifiManager.SUPPLICANT_STATE_CHANGED_ACTION: 建立連接的熱點正在發(fā)生變化. 象征變化的相關類為: SupplicantState, 你可以在接收到此廣播時, 觀察到已經(jīng)建立連接的熱點的整個連接過程, 包含可能會出現(xiàn)連接錯誤的錯誤碼.
5、WifiManager.CONFIGURED_NETWORKS_CHANGED_ACTION: 官方的注釋是這么說的, 廣播已配置的網(wǎng)絡發(fā)生變化, 可由添加, 修改, 刪除網(wǎng)絡的觸發(fā). 當從 intent 中取出key值為 EXTRA_MULTIPLE_NETWORKS_CHANGED, 其值為 true 時, 那么字段 EXTRA_WIFI_CONFIGURATION 中取出來的配置已經(jīng)過時, 不是最新配置了。
6、WifiManager.LINK_CONFIGURATION_CHANGED_ACTION: WIFI 連接配置發(fā)生改變的廣播. 此時, 網(wǎng)路連接功能封裝 LinkProperties 和 NetworkCapabilities 可能發(fā)生變化.
7、WifiManager.NETWORK_STATE_CHANGED_ACTION: WIFI 連接狀態(tài)發(fā)生改變的廣播. 可以從 intent 中取得 NetworkInfo, 此時 NetworkInfo 中提供了連接的新狀態(tài), 如果連接成功, 可以獲取當前連接網(wǎng)絡的 BSSID, 和 WifiInfo.
8、WifiManager.RSSI_CHANGED_ACTION: WIFI 熱點信號強度發(fā)生變化的廣播. 可以獲取當前變化熱點的最新的信號強度.
接收到廣播之后就會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);
}
}
我司電視設置,wifi狀態(tài)開機都是打開的,如果開機為wifi網(wǎng)絡連接的話,就會接受到收到WIFI_STATE_CHANGED_ACTION,CONFIGURED_NETWORKS_CHANGED_ACTION,NETWORK_STATE_CHANGED_ACTION,SUPPLICANT_STATE_CHANGED_ACTION,這個狀態(tài)是從開機到wifi認證的過程,接著我們分別看下我們的代碼流程。
WIFI_STATE_CHANGED_ACTION里面實現(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();
}
這里其實并沒有做什么,只是清空全局變量的一些記錄。然后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 tmpList;
if (mWifiAccessPoints == null) {
tmpList = new ArrayList();
} else {
tmpList = new ArrayList(mWifiAccessPoints);
}
dispatchWifiAccessPointsChanged(tmpList);
}
到這個時候,wifiManger的狀態(tài)就是WIFI_STATE_ENABLED。(如果想要了解開機如何自動連接上次wifi及判斷之前有沒有連接過wifi。請自己閱讀源碼,在此不做交流)接著就是我們比較重要的constructWifiAccessPoints()這個函數(shù):
private List constructWifiAccessPoints() {
Log.v(TAG, "constructWifiAccessPoints");
ArrayList accessPoints = new ArrayList();
/** 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 apMap = new Multimap();
final List 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 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;
}
這個函數(shù)的作用主要是用來獲取WIfiAccessPoint,這個類主要用來存儲wifi的信息。constructWifiAccessPoints這個函數(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:正在進行身份驗證...
OBTAINING_IPADDR:正在獲取Ip地址 CONNECTED:已連接 SUSPENDED:已暫停 DISCONNECTING:正在斷開連接...
DISCONNECTED:已斷開 FAILED:失敗 BLOCKED:已阻止 VERIFYING_POOR_LINK:暫時關閉(網(wǎng)絡狀況不佳)
CAPTIVE_PORTAL_CHECK:判斷是否需要瀏覽器二次登錄,主要有以上幾種狀態(tài)。當我們收到SUPPLICANT_STATE_CHANGED_ACTION的廣播時,在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認證失敗后的處理了。這部分主要是updateWifiAccessPoints(),下面看下packages/../framework/src/com/../framework/wifi/WifiAccessPoint.java,這里主要用于記錄每個wifi的詳細信息,在每次構建的時候狀態(tài)更新的時候都會去重新構造每個WifiAccessPoint,然后fresh。
WifiAccessPoint(Context context, WifiConfiguration config) {
loadConfig(config);
refresh();
}
WifiAccessPoint(Context context, ScanResult result) {
loadResult(result);
refresh();
}
在refresh中主要是更新錯誤原因:
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;
}
}
認證連接的就直接更新wifi狀態(tài)保存就行。那是如何通知上層wifi狀態(tài)發(fā)生改變的呢?這個主要是在packages/../framework/src/com/../framework/wifi/LocalWifiSetting.java中,這里寫了個接口,上層通過實現(xiàn)這個接口處理事件。我們在需要的地方觸發(fā)回調(diào)。
public interface WifiStateChangeListerner {
void onWifiAccessPointsChanage(List accessPoints);
void onWifiStateChanage(int state);
}
private final Collection mCallbacks = new ArrayList();
//上層調(diào)用這里注冊回調(diào)
public void registerCallback(WifiStateChangeListerner callback) {
synchronized (mCallbacks) {
mCallbacks.add(callback);
}
}
public void unregisterCallback(WifiStateChangeListerner callback) {
synchronized (mCallbacks) {
mCallbacks.remove(callback);
}
}
//當wifi的狀態(tài)發(fā)生改變的時候適配層觸發(fā)此函數(shù),回調(diào)上層實現(xiàn)
private void dispatchWifiStateChanged(int state) {
synchronized (mCallbacks) {
for (WifiStateChangeListerner callback : mCallbacks) {
callback.onWifiStateChanage(state);
}
}
}
//當Wifi信息出現(xiàn)變化的時候適配層觸發(fā)此函數(shù),回調(diào)上層實現(xiàn)
private void dispatchWifiAccessPointsChanged(List 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,但這個里面基本上是抽象的方法與類,具體的實現(xiàn)如下:這里因為當時4.4的時候以太網(wǎng)放到mstar下,到5.0的時候發(fā)現(xiàn)會隨大版本的變化而發(fā)生改變,這部分的實現(xiàn)是放到了packages/../framework/android/對應的大版本/ehernet/下,可以按照對應的大版本進行分析,我們先以android4.4的來分析。
所以可以有兩種實現(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的具體實現(xiàn),編譯的時候會android大版本優(yōu)先,如果android大版本下沒有存在,就根據(jù)配置,走相應的板卡或者芯片廠商下邊。
類似與wifi的流程有點類似,上層通過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);
}
其實ethernet的實現(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相關信息的。
isEthernetAvailable()---------------是否支持以太網(wǎng)
getSavedEthConfig()---------------獲取保存的以太網(wǎng),分靜態(tài)IP及非靜態(tài),構建相應的LocalEthernetDevInfo
updateEthDevInfo()----------------更新以太網(wǎng)相關信息(上層應該未使用)
getDhcpInfo()