Android提供了android.net.wifi包供我們操作WiFi。首先介紹一下幾個類:
WifiManager:提供了管理WiFI連接的大部分API
ScanResult:已經檢測出的接入點(包括接入點的地址、名稱、身份認證、頻率、信號強度)
WifiConfiguration:WiFi連接的網絡配置(包括安全配置等)
WifiInfo:WiFI無線連接的描述(包括接入點、網絡連接狀態、隱藏的接入點、IP地址、連接速度、MAC地址、網絡ID、信號強度等等)
WifiManager.WifiLock:允許應用程序一直使用WiFI無線網絡,通常情況下當用戶在一段時間內沒有任何操作的時候,WiFi網絡會自動關閉。我們使用WifiLock來鎖定WiFi網絡,使其一直保持連接,直到這個鎖被釋放。多個應用程序可能有多個鎖,當多有的應用程序的鎖都被釋放的時候,WiFi才被關閉。
下面我們通過代碼理解WiFI的使用:
通過以下代碼獲得WifiManager對象(Wifi是系統的一項服務)
[java]
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);打開WiFI[java] view plaincopyprint?public void openWifi() {
if (!mWifiManager.isWifiEnabled()) {
mWifiManager.setWifiEnabled(true);
}
}
public void openWifi() {
if (!mWifiManager.isWifiEnabled()) {
mWifiManager.setWifiEnabled(true);
}
}關閉WiFi[java] view plaincopyprint?public void closeWifi() {
if (!mWifiManager.isWifiEnabled()) {
mWifiManager.setWifiEnabled(false);
}
}
public void closeWifi() {
if (!mWifiManager.isWifiEnabled()) {
mWifiManager.setWifiEnabled(false);
}
}創建一個WifiLock[java] view plaincopyprint?public void creatWifiLock() {
mWifiLock = mWifiManager.createWifiLock("Test");
}
public void creatWifiLock() {
mWifiLock = mWifiManager.createWifiLock("Test");
}鎖定WifiLock[java] view plaincopyprint?public void acquireWifiLock() {
mWifiLock.acquire();
}
public void acquireWifiLock() {
mWifiLock.acquire();
}釋放WifiLock[java] view plaincopyprint?public void releaseWifiLock() {
if (mWifiLock.isHeld()) {
mWifiLock.release();
}
}
public void releaseWifiLock() {
if (mWifiLock.isHeld()) {
mWifiLock.release();
}
}添加一個WiFi並連接[java] view plaincopyprint?public void addNetworkAndConn(WifiConfiguration wcg) {
int netId = mWifiManager.addNetwork(wcg);
mWifiManager.enableNetwork(netId, true);
}
public void addNetworkAndConn(WifiConfiguration wcg) {
int netId = mWifiManager.addNetwork(wcg);
mWifiManager.enableNetwork(netId, true);
}斷開一個指定ID的WiFi[java] view plaincopyprint?public void disconnectWifi(int netId) {
mWifiManager.disableNetwork(netId);
mWifiManager.disconnect();
}
public void disconnectWifi(int netId) {
mWifiManager.disableNetwork(netId);
mWifiManager.disconnect();
}連接到一個已經配置好了的WiFi[html] view plaincopyprint?public void connectConfiguration(int index) {
// 索引大於配置好的網絡索引返回
if (index > mWifiConfiguration.size()) {
return;
}
mWifiManager.enableNetwork(mWifiConfiguration.get(index).networkId,
true);
}
public void connectConfiguration(int index) {
// 索引大於配置好的網絡索引返回
if (index > mWifiConfiguration.size()) {
return;
}
mWifiManager.enableNetwork(mWifiConfiguration.get(index).networkId,
true);
}掃描可接入的WiFi
[java]
public void startScan() {
mWifiManager.startScan();
// 得到掃描結果
List<ScanResult> wifiList = mWifiManager.getScanResults();
// 得到配置好的網絡連接
List<WifiConfiguration> wifiConfigList = mWifiManager.getConfiguredNetworks();
}
public void startScan() {
mWifiManager.startScan();
// 得到掃描結果
List<ScanResult> wifiList = mWifiManager.getScanResults();
// 得到配置好的網絡連接
List<WifiConfiguration> wifiConfigList = mWifiManager.getConfiguredNetworks();
}查看掃描結果
[java]
public StringBuilder lookUpScan() {
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < mWifiList.size(); i++) {
stringBuilder
.append("Index_" + new Integer(i + 1).toString() + ":");
// 將ScanResult信息轉換成一個字符串包
// 其中把包括:BSSID、SSID、capabilities、frequency、level
stringBuilder.append((mWifiList.get(i)).toString());
stringBuilder.append("\n");
}
return stringBuilder;
}
public StringBuilder lookUpScan() {
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < mWifiList.size(); i++) {
stringBuilder
.append("Index_" + new Integer(i + 1).toString() + ":");
// 將ScanResult信息轉換成一個字符串包
// 其中把包括:BSSID、SSID、capabilities、frequency、level
stringBuilder.append((mWifiList.get(i)).toString());
stringBuilder.append("\n");
}
return stringBuilder;
}得到WiFi的一些信息[java] view plaincopyprint?WifiInfo mifiInfo = mWifiManager.getConnectionInfo();
// 得到MAC地址 www.2cto.com
public String GetMacAddress() {
return (mWifiInfo == null) ? "NULL" : mWifiInfo.getMacAddress();
}
// 得到接入點的BSSID
public String GetBSSID() {
return (mWifiInfo == null) ? "NULL" : mWifiInfo.getBSSID();
}
// 得到IP地址
public int GetIPAddress() {
return (mWifiInfo == null) ? 0 : mWifiInfo.getIpAddress();
}
// 得到連接的ID
public int GetNetworkId() {
return (mWifiInfo == null) ? 0 : mWifiInfo.getNetworkId();
}
// 得到WifiInfo的所有信息包
public String GetWifiInfo() {
return (mWifiInfo == null) ? "NULL" : mWifiInfo.toString();
}
WifiInfo mifiInfo = mWifiManager.getConnectionInfo();
// 得到MAC地址
public String GetMacAddress() {
return (mWifiInfo == null) ? "NULL" : mWifiInfo.getMacAddress();
}
// 得到接入點的BSSID
public String GetBSSID() {
return (mWifiInfo == null) ? "NULL" : mWifiInfo.getBSSID();
}
// 得到IP地址
public int GetIPAddress() {
return (mWifiInfo == null) ? 0 : mWifiInfo.getIpAddress();
}
// 得到連接的ID
public int GetNetworkId() {
return (mWifiInfo == null) ? 0 : mWifiInfo.getNetworkId();
}
// 得到WifiInfo的所有信息包
public String GetWifiInfo() {
return (mWifiInfo == null) ? "NULL" : mWifiInfo.toString();
}最後,使用WiFi必須添加的權限
[html]
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_CHECKIN_PROPERTIES"></uses-permission>
<uses-permission android:name="android.permission.WAKE_LOCK"></uses-permission>
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.MODIFY_PHONE_STATE"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_CHECKIN_PROPERTIES"></uses-permission>
<uses-permission android:name="android.permission.WAKE_LOCK"></uses-permission>
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.MODIFY_PHONE_STATE"></uses-permission>