Android WiFi模組學習

來源:互聯網
上載者:User

標籤:

轉自:http://www.th7.cn/Program/Android/201303/130087.shtml

wifi模組簡介

android本身已經提供了包來處理Android環境下對Wifi的操作,位置是android.net.wifi下面。調用wifi操作需要在mainfest中添加對應許可權。

主要功能類

官方提供的大致可以分為四個主要的類:WifiManager,ScanResult,WifiConfiguration,WifiInfo,此外還有WifiLock,MulticastLock等

 

WifiManager

提供wifi管理的各種主要API,主要包含wifi的掃描,建立串連,配置等

http://developer.android.com/reference/android/net/wifi/WifiManager.html

 

ScanResult

描述開啟掃描後的Wifi熱點的資訊。包含SSID、Capabilities、frequency、level(訊號強度)等。

http://developer.android.com/reference/android/net/wifi/ScanResult.html

 

WifiConfiguration

描述wifi的串連資訊,包含SSID、SSID隱藏、Password等的設定。

http://developer.android.com/reference/android/net/wifi/WifiConfiguration.html

 

WifiInfo

描述已建立串連後的wifi資訊。包含ip、mac地址、連線速度等資訊。區別與ScanResult的概念。

http://developer.android.com/reference/android/net/wifi/WifiInfo.html

 

常用功能代碼擷取WifiManager
WifiManager wm = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);

設定wifi功能開啟

bRet = wifiManager.isWifiEnabled();//判斷是否開啟

bRet = wifiManager.setWifiEnabled(true);//設定開啟關閉

注意,調用setWifiEnabled後,系統進行wifi模組的開啟需要一定時間,此時通過wifiManager.getWifiState()擷取的狀態來判斷是否完成。

 

WifiManager.WIFI_STATE_DISABLED : WIFI網卡不可用(1)

WifiManager.WIFI_STATE_DISABLING : WIFI網卡正在關閉(0)

WifiManager.WIFI_STATE_ENABLED : WIFI網卡可用(3)

WifiManager.WIFI_STATE_ENABLING : WIFI網正在開啟(2) (WIFI啟動需要一段時間)

WifiManager.WIFI_STATE_UNKNOWN  : 未知網卡狀態

 

while (wifiManager.getWifiState() == WifiManager.WIFI_STATE_ENABLING) {                            
try {
Thread.currentThread();
Thread.sleep(100);
} catch (InterruptedException ie) {
}
}

 

掃描附近存取點AP
List<ScanResult> list = null;if ( wifiManager.startScan())list = wifiManager.getScanResults();

 

擷取AP的訊號強度、頻率等
scanResult.level  //訊號強度scanResult.frequency  //頻率

 

擷取手機中儲存過配置的串連資訊
List<WifiConfiguration> existingConfigs = wifiManager.getConfiguredNetworks();//擷取儲存的配置資訊

 

判斷某ssid的AP是否有儲存過配置資訊
for (WifiConfiguration existingConfig : existingConfigs) {         
    if (existingConfig.SSID.equals("/"" + SSID + "/"")) {
    return existingConfig;
  }
}

 

關閉Wifi串連
wifiManager.disableNetwork(existingConfig.networkId);

 

移除Wifi串連配置
wifiManager.removeNetwork(networkId);

注意與上面的disableNetwork的區別。Disable只是單純的中斷連線,儲存的ssid和密碼並不清除。

Remove則是從手機中移除(忘記)該串連配置,用來清除儲存的密碼

在網路正常串連的狀態,也可直接調用remove來代替disable中斷網路,但會清除密碼資訊。

 

建立串連Configuration
public WifiConfiguration CreateWifiConfig(String SSID, String Password,   WifiCipherType Type) {                  
WifiConfiguration config = new WifiConfiguration();
config.allowedAuthAlgorithms.clear();
config.allowedGroupCiphers.clear();
config.allowedKeyManagement.clear();
config.allowedPairwiseCiphers.clear();
config.allowedProtocols.clear();
config.SSID = "/"" + SSID + "/"";

//無密碼
if (Type == WifiCipherType.WIFICIPHER_NOPASS) {
config.wepKeys[0] = "";
config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
config.wepTxKeyIndex = 0;
}

//WEP加密
if (Type == WifiCipherType.WIFICIPHER_WEP) {
config.preSharedKey = "/"" + Password + "/"";
config.hiddenSSID = true;
config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.SHARED);
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);
config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
config.wepTxKeyIndex = 0;
} //WPA加密
if (Type == WifiCipherType.WIFICIPHER_WPA) {
    config.preSharedKey = "/"" + Password + "/"";
    config.hiddenSSID = true;
    // config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
    // config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
    // config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
    // config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
    // config.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
    // config.status = WifiConfiguration.Status.ENABLED;
} else {
    return null;
}
    return config;
}

添加、開啟一個wifi
public boolean addNewWifi(WifiConfiguration newConfig){                   
int netID = wifiManager.addNetwork(newConfig);//添加
boolean bRet = wifiManager.enableNetwork(netID, false);//啟動
return bRet;
} 
擷取目前的WifiInfo(IP、mac等)
WifiInfo wInfo = wifiManager.getConnectionInfo();//擷取當前串連的info 
wInfo.getBSSID();// 擷取BSSID
wInfo.getHiddenSSID();// 獲得SSID 是否被隱藏
wInfo.getIpAddress();// 擷取IP 位址
wInfo.getLinkSpeed();// 獲得串連的速度
wInfo.getMacAddress();// 獲得Mac 地址
wInfo.getRssi();// 獲得802.11n 網路的訊號
wInfo.getSSID();// 獲得SSID
SupplicantState suppState = wInfo.getSupplicantState();// 返回具體用戶端狀態的資訊
wInfo.getDetailedStateOf(suppState);// 擷取用戶端的連通性

 

相關許可權
修改網路狀態的許可權
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" ></uses-permission>
修改WIFI狀態的許可權<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" ></uses-permission>
訪問網路許可權<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" ></uses-permission>
訪問WIFI許可權<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" ></uses-permission>

Android WiFi模組學習

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.