/**
* Wifi 管理类
*
* @author Administrator
* 使用方法
* WifiManagerUtils wifiManager = new WifiManagerUtils(context);
if (wifiManager.isConnection())
{
Intent intent = new Intent(context, AseoZdpUpdateService.class);
context.startService(intent);
}
*
*/
public class WifiManagerUtils {
private WifiManager mWifiManager;
private WifiInfo mWifiInfo;
private List<ScanResult> mWifiList;
private List<WifiConfiguration> mWifiConfiguration; public WifiManagerUtils(Context context) {
this.mWifiManager = ((WifiManager) context.getSystemService("wifi")); this.mWifiInfo = this.mWifiManager.getConnectionInfo();
} public boolean isConnection() {
this.mWifiInfo = this.mWifiManager.getConnectionInfo(); return (this.mWifiManager.isWifiEnabled()) && (this.mWifiInfo != null)
&& (this.mWifiInfo.getBSSID() != null)
&& (this.mWifiInfo.getIpAddress() != );
} public boolean isConnection(String ssid) {
this.mWifiInfo = this.mWifiManager.getConnectionInfo();
if ((!this.mWifiManager.isWifiEnabled()) || (this.mWifiInfo == null))
return false;
String string = this.mWifiInfo.getSSID(); return string.contains(ssid);
} public int checkState() {
return this.mWifiManager.getWifiState();
} public List<WifiConfiguration> getConfiguration() {
return this.mWifiConfiguration;
} public void connectConfiguration(int index) {
if (index > this.mWifiConfiguration.size()) {
return;
} this.mWifiManager
.enableNetwork(
((WifiConfiguration) this.mWifiConfiguration.get(index)).networkId,
true);
} public void startScan() {
this.mWifiManager.startScan(); this.mWifiConfiguration = this.mWifiManager.getConfiguredNetworks();
} public List<ScanResult> getWifiList() {
this.mWifiList = this.mWifiManager.getScanResults();
return this.mWifiList;
} public StringBuilder lookUpScan() {
StringBuilder stringBuilder = new StringBuilder();
for (int i = ; i < this.mWifiList.size(); i++) {
stringBuilder
.append("Index_" + new Integer(i + ).toString() + ":"); stringBuilder.append(((ScanResult) this.mWifiList.get(i))
.toString());
stringBuilder.append("/n");
}
return stringBuilder;
} public String getMacAddress() {
return this.mWifiInfo == null ? "NULL" : this.mWifiInfo.getMacAddress();
} public String getBSSID() {
return this.mWifiInfo == null ? "NULL" : this.mWifiInfo.getBSSID();
} public String getSSID() {
return this.mWifiInfo == null ? "NULL" : this.mWifiInfo.getSSID();
} public int getIPAddress() {
return this.mWifiInfo == null ? : this.mWifiInfo.getIpAddress();
} public int getNetworkId() {
return this.mWifiInfo == null ? : this.mWifiInfo.getNetworkId();
} public String getWifiInfo() {
return this.mWifiInfo == null ? "NULL" : this.mWifiInfo.toString();
} public void disconnectWifi(int netId) {
this.mWifiManager.disableNetwork(netId);
this.mWifiManager.disconnect();
} public String Connect(String SSID, String Password, WifiCipherType type) {
if (!this.mWifiManager.isWifiEnabled()) {
return null;
} this.mWifiInfo = this.mWifiManager.getConnectionInfo();
String ssidString = getSSID();
if ((ssidString != null) && (ssidString.contains("\""))) {
ssidString = ssidString.split("\"")[];
} WifiConfiguration oldConfig = IsExsits(ssidString);
if (oldConfig != null) {
disconnectWifi(oldConfig.networkId);
} WifiConfiguration tempConfig = CreateWifiInfo(SSID, Password, type);
int tempID = this.mWifiManager.addNetwork(tempConfig);
boolean ret = this.mWifiManager.enableNetwork(tempID, true);
this.mWifiManager.reconnect(); return ssidString;
} public boolean RemoveWifiConfig(String ssid) {
WifiConfiguration Config = IsExsits(ssid);
if (Config == null) {
return false;
}
this.mWifiManager.disableNetwork(Config.networkId);
this.mWifiManager.removeNetwork(Config.networkId);
return true;
} public boolean Connect(String ssid) {
if ((ssid != null) && (ssid.contains("\""))) {
ssid = ssid.split("\"")[];
}
WifiConfiguration tempConfig = IsExsits(ssid);
if (tempConfig != null) {
boolean bRet = this.mWifiManager.enableNetwork(
tempConfig.networkId, true);
this.mWifiManager.reconnect();
}
return true;
} public WifiConfiguration IsExsits(String SSID) {
List existingConfigs = this.mWifiManager.getConfiguredNetworks();
for (WifiConfiguration existingConfig : existingConfigs) {
if (existingConfig.SSID.equals("\"" + SSID + "\"")) {
return existingConfig;
}
}
return null;
} private WifiConfiguration CreateWifiInfo(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.hiddenSSID = true;
config.allowedKeyManagement.set();
} else if (type == WifiCipherType.WIFICIPHER_WPA) {
config.preSharedKey = ("\"" + Password + "\"");
config.hiddenSSID = true;
config.allowedAuthAlgorithms.set();
config.allowedGroupCiphers.set();
config.allowedKeyManagement.set();
config.allowedPairwiseCiphers.set(); config.allowedGroupCiphers.set();
config.allowedPairwiseCiphers.set();
config.status = ;
} else if (type == WifiCipherType.WIFICIPHER_WEP) {
config.hiddenSSID = true;
config.wepKeys[] = ("\"" + Password + "\"");
config.allowedAuthAlgorithms.set();
config.allowedGroupCiphers.set();
config.allowedGroupCiphers.set();
config.allowedGroupCiphers.set();
config.allowedGroupCiphers.set();
config.allowedKeyManagement.set();
config.wepTxKeyIndex = ;
}
return config;
} public static enum WifiCipherType {
WIFICIPHER_WEP, WIFICIPHER_WPA, WIFICIPHER_NOPASS, WIFICIPHER_INVALID;
}
}

最新文章

  1. SQL SERVER中求上月、本月和下月的第一天和最后一天 DATEADD DATEDIFF
  2. Linux网路编程系列-网络I/O模型
  3. 转:大气炫酷焦点轮播图js特效
  4. Visual studio 非常好的插件
  5. javascript “||”、“&amp;&amp;”的灵活运用
  6. (lleetcode)Merge Sorted Array
  7. java 导入包
  8. IntPtr
  9. jQuery慢慢啃之事件对象(十一)
  10. CodeForces462B
  11. IdentityServer(12)- 使用 ASP.NET Core Identity
  12. WordPress文章中插入qq表情
  13. postman简单教程-环境变量,全局变量的设置及作用
  14. 内网ssh穿透
  15. 中国最强AI超级服务器问世,每秒提供AI计算2000万亿次
  16. 记一次InputStream引起的乱码
  17. Apache系列:Apache的全局配置
  18. vue中导入外面文件(css,js)方式
  19. 学习git 新手。这个写的不错
  20. vue中的插槽slot

热门文章

  1. JavaScript正则表达式API
  2. codeforces 399B. Red and Blue Balls 解题报告
  3. hdu 2680 Choose the best route 解题报告
  4. oracle 删除用户命令和部分表空间操作
  5. Oracle:不同数据库版本导致的Ora-00918问题
  6. jsp项目上传到服务器
  7. [Selenium] close alert window
  8. ndoejs后台查询数据库返回的值-进行解析
  9. 为什么linux有足够的内存还进行swap?
  10. NC文件的处理【netcdf】