1. /**
  2. * Wifi 管理类
  3. *
  4. * @author Administrator
  5. * 使用方法
  6. * WifiManagerUtils wifiManager = new WifiManagerUtils(context);
  7. if (wifiManager.isConnection())
  8. {
  9. Intent intent = new Intent(context, AseoZdpUpdateService.class);
  10. context.startService(intent);
  11. }
  12. *
  13. */
  14. public class WifiManagerUtils {
  15. private WifiManager mWifiManager;
  16. private WifiInfo mWifiInfo;
  17. private List<ScanResult> mWifiList;
  18. private List<WifiConfiguration> mWifiConfiguration;
  19.  
  20. public WifiManagerUtils(Context context) {
  21. this.mWifiManager = ((WifiManager) context.getSystemService("wifi"));
  22.  
  23. this.mWifiInfo = this.mWifiManager.getConnectionInfo();
  24. }
  25.  
  26. public boolean isConnection() {
  27. this.mWifiInfo = this.mWifiManager.getConnectionInfo();
  28.  
  29. return (this.mWifiManager.isWifiEnabled()) && (this.mWifiInfo != null)
  30. && (this.mWifiInfo.getBSSID() != null)
  31. && (this.mWifiInfo.getIpAddress() != );
  32. }
  33.  
  34. public boolean isConnection(String ssid) {
  35. this.mWifiInfo = this.mWifiManager.getConnectionInfo();
  36. if ((!this.mWifiManager.isWifiEnabled()) || (this.mWifiInfo == null))
  37. return false;
  38. String string = this.mWifiInfo.getSSID();
  39.  
  40. return string.contains(ssid);
  41. }
  42.  
  43. public int checkState() {
  44. return this.mWifiManager.getWifiState();
  45. }
  46.  
  47. public List<WifiConfiguration> getConfiguration() {
  48. return this.mWifiConfiguration;
  49. }
  50.  
  51. public void connectConfiguration(int index) {
  52. if (index > this.mWifiConfiguration.size()) {
  53. return;
  54. }
  55.  
  56. this.mWifiManager
  57. .enableNetwork(
  58. ((WifiConfiguration) this.mWifiConfiguration.get(index)).networkId,
  59. true);
  60. }
  61.  
  62. public void startScan() {
  63. this.mWifiManager.startScan();
  64.  
  65. this.mWifiConfiguration = this.mWifiManager.getConfiguredNetworks();
  66. }
  67.  
  68. public List<ScanResult> getWifiList() {
  69. this.mWifiList = this.mWifiManager.getScanResults();
  70. return this.mWifiList;
  71. }
  72.  
  73. public StringBuilder lookUpScan() {
  74. StringBuilder stringBuilder = new StringBuilder();
  75. for (int i = ; i < this.mWifiList.size(); i++) {
  76. stringBuilder
  77. .append("Index_" + new Integer(i + ).toString() + ":");
  78.  
  79. stringBuilder.append(((ScanResult) this.mWifiList.get(i))
  80. .toString());
  81. stringBuilder.append("/n");
  82. }
  83. return stringBuilder;
  84. }
  85.  
  86. public String getMacAddress() {
  87. return this.mWifiInfo == null ? "NULL" : this.mWifiInfo.getMacAddress();
  88. }
  89.  
  90. public String getBSSID() {
  91. return this.mWifiInfo == null ? "NULL" : this.mWifiInfo.getBSSID();
  92. }
  93.  
  94. public String getSSID() {
  95. return this.mWifiInfo == null ? "NULL" : this.mWifiInfo.getSSID();
  96. }
  97.  
  98. public int getIPAddress() {
  99. return this.mWifiInfo == null ? : this.mWifiInfo.getIpAddress();
  100. }
  101.  
  102. public int getNetworkId() {
  103. return this.mWifiInfo == null ? : this.mWifiInfo.getNetworkId();
  104. }
  105.  
  106. public String getWifiInfo() {
  107. return this.mWifiInfo == null ? "NULL" : this.mWifiInfo.toString();
  108. }
  109.  
  110. public void disconnectWifi(int netId) {
  111. this.mWifiManager.disableNetwork(netId);
  112. this.mWifiManager.disconnect();
  113. }
  114.  
  115. public String Connect(String SSID, String Password, WifiCipherType type) {
  116. if (!this.mWifiManager.isWifiEnabled()) {
  117. return null;
  118. }
  119.  
  120. this.mWifiInfo = this.mWifiManager.getConnectionInfo();
  121. String ssidString = getSSID();
  122. if ((ssidString != null) && (ssidString.contains("\""))) {
  123. ssidString = ssidString.split("\"")[];
  124. }
  125.  
  126. WifiConfiguration oldConfig = IsExsits(ssidString);
  127. if (oldConfig != null) {
  128. disconnectWifi(oldConfig.networkId);
  129. }
  130.  
  131. WifiConfiguration tempConfig = CreateWifiInfo(SSID, Password, type);
  132. int tempID = this.mWifiManager.addNetwork(tempConfig);
  133. boolean ret = this.mWifiManager.enableNetwork(tempID, true);
  134. this.mWifiManager.reconnect();
  135.  
  136. return ssidString;
  137. }
  138.  
  139. public boolean RemoveWifiConfig(String ssid) {
  140. WifiConfiguration Config = IsExsits(ssid);
  141. if (Config == null) {
  142. return false;
  143. }
  144. this.mWifiManager.disableNetwork(Config.networkId);
  145. this.mWifiManager.removeNetwork(Config.networkId);
  146. return true;
  147. }
  148.  
  149. public boolean Connect(String ssid) {
  150. if ((ssid != null) && (ssid.contains("\""))) {
  151. ssid = ssid.split("\"")[];
  152. }
  153. WifiConfiguration tempConfig = IsExsits(ssid);
  154. if (tempConfig != null) {
  155. boolean bRet = this.mWifiManager.enableNetwork(
  156. tempConfig.networkId, true);
  157. this.mWifiManager.reconnect();
  158. }
  159. return true;
  160. }
  161.  
  162. public WifiConfiguration IsExsits(String SSID) {
  163. List existingConfigs = this.mWifiManager.getConfiguredNetworks();
  164. for (WifiConfiguration existingConfig : existingConfigs) {
  165. if (existingConfig.SSID.equals("\"" + SSID + "\"")) {
  166. return existingConfig;
  167. }
  168. }
  169. return null;
  170. }
  171.  
  172. private WifiConfiguration CreateWifiInfo(String SSID, String Password,
  173. WifiCipherType type) {
  174. WifiConfiguration config = new WifiConfiguration();
  175. config.allowedAuthAlgorithms.clear();
  176. config.allowedGroupCiphers.clear();
  177. config.allowedKeyManagement.clear();
  178. config.allowedPairwiseCiphers.clear();
  179. config.allowedProtocols.clear();
  180. config.SSID = ("\"" + SSID + "\"");
  181.  
  182. if (type == WifiCipherType.WIFICIPHER_NOPASS) {
  183. config.hiddenSSID = true;
  184. config.allowedKeyManagement.set();
  185. } else if (type == WifiCipherType.WIFICIPHER_WPA) {
  186. config.preSharedKey = ("\"" + Password + "\"");
  187. config.hiddenSSID = true;
  188. config.allowedAuthAlgorithms.set();
  189. config.allowedGroupCiphers.set();
  190. config.allowedKeyManagement.set();
  191. config.allowedPairwiseCiphers.set();
  192.  
  193. config.allowedGroupCiphers.set();
  194. config.allowedPairwiseCiphers.set();
  195. config.status = ;
  196. } else if (type == WifiCipherType.WIFICIPHER_WEP) {
  197. config.hiddenSSID = true;
  198. config.wepKeys[] = ("\"" + Password + "\"");
  199. config.allowedAuthAlgorithms.set();
  200. config.allowedGroupCiphers.set();
  201. config.allowedGroupCiphers.set();
  202. config.allowedGroupCiphers.set();
  203. config.allowedGroupCiphers.set();
  204. config.allowedKeyManagement.set();
  205. config.wepTxKeyIndex = ;
  206. }
  207. return config;
  208. }
  209.  
  210. public static enum WifiCipherType {
  211. WIFICIPHER_WEP, WIFICIPHER_WPA, WIFICIPHER_NOPASS, WIFICIPHER_INVALID;
  212. }
  213. }

Android 常见的工具类的更多相关文章

  1. Android 软件管理工具类Utils

    Android 软件管理工具类Utils /** * Created by uilubo on 2015/9/30. * 工具类 */ public class Utils { public stat ...

  2. (转载)实例详解Android快速开发工具类总结

    实例详解Android快速开发工具类总结 作者:LiJinlun 字体:[增加 减小] 类型:转载 时间:2016-01-24我要评论 这篇文章主要介绍了实例详解Android快速开发工具类总结的相关 ...

  3. 一个使用命令行编译Android项目的工具类

    一个使用命令行编译Android项目的工具类 简单介绍 编译apk项目须要使用的几个工具,基本都在sdk中,它们各自是(Windows系统): 1.aapt.exe 资源打包工具 2.android. ...

  4. Android开发常用工具类

    来源于http://www.open-open.com/lib/view/open1416535785398.html 主要介绍总结的Android开发中常用的工具类,大部分同样适用于Java. 目前 ...

  5. Android常用的工具类

    主要介绍总结的Android开发中常用的工具类,大部分同样适用于Java.目前包括HttpUtils.DownloadManagerPro.ShellUtils.PackageUtils. Prefe ...

  6. Android常用的工具类(转)

    主要介绍总结的Android开发中常用的工具类,大部分同样适用于Java.目前包括HttpUtils.DownloadManagerPro.ShellUtils.PackageUtils.Prefer ...

  7. 2013最新Android常用的工具类整理

    主要介绍总结的Android开发中常用的工具类,大部分同样适用于Java. 目前包括HttpUtils.DownloadManagerPro.ShellUtils.PackageUtils. Pref ...

  8. 最全Android开发常用工具类

    主要介绍总结的Android开发中常用的工具类,大部分同样适用于Java. 目前包括  HttpUtils.DownloadManagerPro.Safe.ijiami.ShellUtils.Pack ...

  9. android下载简单工具类

    功能是实现下载文件,图片或MP3等,为了简单起见使用单线程,此代码为MarsAndroid教程的复制品,放在此处,留着参考. 首先是一个得到字节流随后保存到内存卡上的工具类: package com. ...

随机推荐

  1. bzoj3137: [Baltic2013]tracks

    炸一看好像很神仙的样子,其实就是个sb题 万年不见的1A 但是我们可以反过来想,先选一个起点到终点的联通块,然后这联通块后面相当于就能够走了,继续找联通块 然后就能发现直接相邻的脚步相同的边权为0,否 ...

  2. 各种DP总结

    一.数位DP 1.含有或不含某个数“xx”: HDU3555 Bomb HDU2089 不要62 2.满足某些条件,如能整除某个数,或者数位上保持某种特性: HDU3652 B-number Code ...

  3. LightOJ - 1422 Halloween Costumes —— 区间DP

    题目链接:https://vjudge.net/problem/LightOJ-1422 1422 - Halloween Costumes    PDF (English) Statistics F ...

  4. 多态、抽象类、接口、区别(java基础知识九)

    1.多态的概述以及代码体现 * A:多态概述 * 事物存在的多种形态 * B:多态前提 * a:要有继承关系. * 一个类是父类,一个类是子类 * b:要有方法重写. * c:要有父类引用指向子类对象 ...

  5. LA-3716(sort的神用)

    题意: 给出两条长度均为n的NDA链A和B,找出一段最长的字串[l,r]使得该区域的突变位置不超过p%; 思路: sum[i]表示[1,i]中不相同的个数,可得表达式(sum[i]-sum[j])/( ...

  6. 搜索好题UVA1601

    题目 分析:如果以当前3个小写字母的位置为状态,则问题转化为图上的最短路问题.但是如果每次都判断小写字母的下一步是否合法,那就是说每次移动都需要判断5^3,肯定会超时.可以把所有可以移动的格子找出来建 ...

  7. 怎么解决Failed to load the JNI shared library

    怎么解决Failed to load the JNIshared library   解决Failed to load the JNIshared library唯一的方法就是重新安装eclipse, ...

  8. jQuery中排除指定元素,同时选择剩下的所有元素

    场景:某页面用了js延时加载技术处理所有图片,以改善用户体验,但是有几个图片不想延时加载,要求把它们单独挑出来. 研究了一下jQuery的API文档,搞掂了,jQuery真的很方便,贴在这里备份: 1 ...

  9. bzoj 4260 REBXOR —— Trie树

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4260 用 Trie 树可以找出前缀异或的最大值和后缀异或的最大值,拼起来即可: 注意要先加入 ...

  10. 关于cout输出精度问题

    #include <iostream.h> #include <iomanip.h> void main(void) { cout.setf(ios::fixed); cout ...