博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android 常见的工具类
阅读量:5342 次
发布时间:2019-06-15

本文共 6907 字,大约阅读时间需要 23 分钟。

/** * 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
mWifiList; private List
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() != 0); } 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
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
getWifiList() { this.mWifiList = this.mWifiManager.getScanResults(); return this.mWifiList; } public StringBuilder lookUpScan() { StringBuilder stringBuilder = new StringBuilder(); for (int i = 0; i < this.mWifiList.size(); i++) { stringBuilder .append("Index_" + new Integer(i + 1).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 ? 0 : this.mWifiInfo.getIpAddress(); } public int getNetworkId() { return this.mWifiInfo == null ? 0 : 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("\"")[1]; } 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("\"")[1]; } 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(0); } else if (type == WifiCipherType.WIFICIPHER_WPA) { config.preSharedKey = ("\"" + Password + "\""); config.hiddenSSID = true; config.allowedAuthAlgorithms.set(0); config.allowedGroupCiphers.set(2); config.allowedKeyManagement.set(1); config.allowedPairwiseCiphers.set(1); config.allowedGroupCiphers.set(3); config.allowedPairwiseCiphers.set(2); config.status = 2; } else if (type == WifiCipherType.WIFICIPHER_WEP) { config.hiddenSSID = true; config.wepKeys[0] = ("\"" + Password + "\""); config.allowedAuthAlgorithms.set(1); config.allowedGroupCiphers.set(3); config.allowedGroupCiphers.set(2); config.allowedGroupCiphers.set(0); config.allowedGroupCiphers.set(1); config.allowedKeyManagement.set(0); config.wepTxKeyIndex = 0; } return config; } public static enum WifiCipherType { WIFICIPHER_WEP, WIFICIPHER_WPA, WIFICIPHER_NOPASS, WIFICIPHER_INVALID; }}
View Code

 

转载于:https://www.cnblogs.com/luoyangcn/p/4973247.html

你可能感兴趣的文章
LeetCode-Strobogrammatic Number
查看>>
luoguP3414 SAC#1 - 组合数
查看>>
五一 DAY 4
查看>>
(转)接口测试用例设计(详细干货)
查看>>
【译】SSH隧道:本地和远程端口转发
查看>>
win8.1安装Python提示缺失api-ms-win-crt-runtime-l1-1-0.dll问题
查看>>
图片点击轮播(三)-----2017-04-05
查看>>
直播技术细节3
查看>>
《分布式服务架构:原理、设计于实战》总结
查看>>
java中new一个对象和对象=null有什么区别
查看>>
字母和数字键的键码值(keyCode)
查看>>
IE8调用window.open导出EXCEL文件题目
查看>>
Spring mvc初学
查看>>
有意思的代码片段
查看>>
C8051开发环境
查看>>
VTKMY 3.3 VS 2010 Configuration 配置
查看>>
01_1_准备ibatis环境
查看>>
windows中修改catalina.sh上传到linux执行报错This file is needed to run this program解决
查看>>
JavaScript中的BOM和DOM
查看>>
360浏览器兼容模式 不能$.post (不是a 连接 onclick的问题!!)
查看>>