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