package com.example.wifitest;
import java.util.List;import android.content.Context;import android.net.wifi.ScanResult;import android.net.wifi.WifiConfiguration;import android.net.wifi.WifiInfo;import android.net.wifi.WifiManager;import android.net.wifi.WifiManager.WifiLock;public class WifiAdmin { // 定義WifiManager對象 private WifiManager mWifiManager; // 定義WifiInfo對象 private WifiInfo mWifiInfo; // 掃描出的網路連接列表 private List<ScanResult> mWifiList; // 網路連接列表 private List<WifiConfiguration> mWifiConfiguration; // 定義一個WifiLock WifiLock mWifiLock; // 構造器 public WifiAdmin(Context context) { // 取得WifiManager對象 mWifiManager = (WifiManager) context .getSystemService(Context.WIFI_SERVICE); // 取得WifiInfo對象 mWifiInfo = mWifiManager.getConnectionInfo(); } // 開啟WIFI public void openWifi() { if (!mWifiManager.isWifiEnabled()) { mWifiManager.setWifiEnabled(true); } } // 關閉WIFI public void closeWifi() { if (mWifiManager.isWifiEnabled()) { mWifiManager.setWifiEnabled(false); } } // 檢查當前WIFI狀態 public int checkState() { return mWifiManager.getWifiState(); } // 鎖定WifiLock public void acquireWifiLock() { mWifiLock.acquire(); } // 解鎖WifiLock public void releaseWifiLock() { // 判斷時候鎖定 if (mWifiLock.isHeld()) { mWifiLock.acquire(); } } // 建立一個WifiLock public void creatWifiLock() { mWifiLock = mWifiManager.createWifiLock("Test"); } // 得到配置好的網路 public List<WifiConfiguration> getConfiguration() { return mWifiConfiguration; } // 指定配置好的網路進行串連 public void connectConfiguration(int index) { // 索引大於配置好的網路索引返回 if (index > mWifiConfiguration.size()) { return; } // 串連配置好的指定ID的網路 mWifiManager.enableNetwork(mWifiConfiguration.get(index).networkId, true); } public void startScan() { mWifiManager.startScan(); // 得到掃描結果 mWifiList = mWifiManager.getScanResults(); // 得到配置好的網路連接 mWifiConfiguration = mWifiManager.getConfiguredNetworks(); } // 得到網路列表 public List<ScanResult> getWifiList() { return mWifiList; } // 查看掃描結果 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; } // 得到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(); } // 添加一個網路並串連 public void addNetwork(WifiConfiguration wcg) { int wcgID = mWifiManager.addNetwork(wcg); boolean b = mWifiManager.enableNetwork(wcgID, true); System.out.println("a--" + wcgID); System.out.println("b--" + b); } // 斷開指定ID的網路 public void disconnectWifi(int netId) { mWifiManager.disableNetwork(netId); mWifiManager.disconnect(); } // 然後是一個實際應用方法,只驗證過沒有密碼的情況: public WifiConfiguration CreateWifiInfo(String SSID, String Password, int Type) { WifiConfiguration config = new WifiConfiguration(); config.allowedAuthAlgorithms.clear(); config.allowedGroupCiphers.clear(); config.allowedKeyManagement.clear(); config.allowedPairwiseCiphers.clear(); config.allowedProtocols.clear(); config.SSID = "\"" + SSID + "\""; WifiConfiguration tempConfig = this.IsExsits(SSID); if (tempConfig != null) { mWifiManager.removeNetwork(tempConfig.networkId); } if (Type == 1) // WIFICIPHER_NOPASS { config.wepKeys[0] = ""; config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE); config.wepTxKeyIndex = 0; } if (Type == 2) // WIFICIPHER_WEP { config.hiddenSSID = true; config.wepKeys[0] = "\"" + Password + "\""; 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; } if (Type == 3) // 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.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP); config.allowedPairwiseCiphers .set(WifiConfiguration.PairwiseCipher.CCMP); config.status = WifiConfiguration.Status.ENABLED; } return config; } private WifiConfiguration IsExsits(String SSID) { List<WifiConfiguration> existingConfigs = mWifiManager .getConfiguredNetworks(); for (WifiConfiguration existingConfig : existingConfigs) { if (existingConfig.SSID.equals("\"" + SSID + "\"")) { return existingConfig; } } return null; }}
}
//分為三種情況:1沒有密碼2用wep加密3用wpa加密
改動主要集中在CreateWifiInfo這個方法中,並且添加了一個私人方法:
(1)將與方法的第三個參數有關的變數都改成int型,或者使用原作者的枚舉型(存在bug需要改正),但枚舉會在後續的開發中遇到些困難;
(2)在if(type == 3)中注釋掉“config.allowedProtocols.set(WifiConfiguration.Protocol.WPA);”,並添加“
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP); config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);“這兩句,否則當wifi熱點需要輸入密碼時,無法加入網路。
(3)在代碼末尾添加了方法IsExsits,原因在於如果按照網上介紹的方法成功加入指定的wifi後,都會在終端的wifi列表中新添加一個以該指定ssid的網路,所以每運行一次程式,列表中就會多一個相同名字的ssid。而該方法就是檢查wifi列表中是否有以輸入參數為名的wifi熱點,如果存在,則在CreateWifiInfo方法開始配置wifi網路之前將其移除,以避免ssid的重複:
WifiConfiguration tempConfig = this.IsExsits(SSID);
if(tempConfig != null) {
mWifiManager.removeNetwork(tempConfig.networkId);
}
以上便是wifi工具類的建立,之後就可以在其他部分執行個體化這個類,調用其中的方法完成加入指定ssid的wifi熱點,還是先上代碼吧,建立一個名為Test_wifiActivity.java的檔案(同上,沒有包含包聲明和import語句):
public class Test_wifiActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); WifiAdmin wifiAdmin = new WifiAdmin(this); wifiAdmin.openWifi(); wifiAdmin.addNetwork(wifiAdmin.CreateWifiInfo("XXX", "XXX", 3)); }}
很簡單,如果是接入wifi,大體上只涉及到openWifi(開啟wifi)、CreateWifiInfo(配置wifi網路資訊)和addNetwork(添加配置好的網路並串連),對CreateWifiInfo進行簡單的說明:第一參數是SSID的名稱;第二個參數是指定SSID網路的密碼,當不要求輸入密碼是置空(”“);第三個參數是熱點類型:1-無密碼 / 2-WEP密碼驗證(未測試)/ 3-WAP或WAP2 PSK密碼驗證。
最後就是在Manifest中添加相應的許可權了:
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" ></uses-permission> <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" ></uses-permission> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" ></uses-permission> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" ></uses-permission>