åèå¦ä¸å
容ï¼å¯ä»¥å®ç°æ²¡æå¯ç çwifièªå¨è¿æ¥ï¼
å®ç°èªå¨è¿æ¥å°æå®çWiFiçç¹ï¼æ们éè¦çæAndroidéçwifimanagerç±»ï¼ä¸é¢æ¯ç»åç½ç»èµæåæ¬äººå®è·µç»åºçæ¹æ³ï¼ä¾åèï¼
1. èªå®ä¹ä¸ä¸ªwifiAdminæä½ç±»ï¼
[java] view plain copy
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 boolean addNetwork(WifiConfiguration wcg) {
int wcgID = mWifiManager.addNetwork(wcg);
boolean b = mWifiManager.enableNetwork(wcgID, true);
System.out.println("a--" + wcgID);
System.out.println("b--" + b);
return 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å å¯
2.è°ç¨æ¶æ¯è¿æ ·çï¼
[java] view plain copy
WifiAdmin wifiAdmin = new WifiAdmin(wifiTest.this); //wifiTest.this is a context for sample;
wifiAdmin.openWifi();
WifiConfiguration wcg=wifiAdmin.CreateWifiInfo(wifiName, wifiPass,3); //wifiName is the SSID; wifiPass is the password;
wifiAdmin.addNetwork(wcg);
3.ç¨ççå»ï¼ä¾¿ä¼è¿æ¥ä¸wifiäº
温馨提示:内容为网友见解,仅供参考