/**
* 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;
}
}

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. codeforces 454B. Little Pony and Sort by Shift 解题报告

    题目链接:http://codeforces.com/problemset/problem/454/B 题目意思:给出一个序列你 a1, a2, ..., an. 问每次操作只能通过将最后一个数拿出来 ...

  2. Oracle:通过dbv查看数据文件是否有坏块

    我们备份的数据文件,可以通过oacle自带的dbv工具来查看是否是好的. 下面实验如下: 环境:oracle10.2.0.1 1.检查数据文件是否有坏块 [oracle@app orcl]$ dbv ...

  3. Fleck websocket官方事例

    Fleck websocket官方事例 server: using Fleck;using System;using System.Collections.Generic;using System.L ...

  4. hdu-2680 Choose the best route(最短路)

    题目链接: Choose the best route Time Limit: 2000/1000 MS (Java/Others)     Memory Limit: 32768/32768 K ( ...

  5. [Java] 继承,隐藏,覆盖,重载,多态,抽象类,接口

    1.子类 class SonClass extends ABC{...} 在子类定义后,子类中就可以直接隐式包含父类的成员变量和方法,而不用再写,这就是使用继承的优点. 子类包含父类的成员,不是子类和 ...

  6. SDOI2016 Round1 题解

    BZOJ4513 储能表 数位DP,f[i][2][2][2]表示前i位,是否卡n的上界,是否卡m的上界,是否卡k的下界,枚举每一维的下一位直接转移. #include<cstdio> # ...

  7. JS 之正则表达式

    字符 含意 \ 做为转意,即通常在"\"后面的字符不按原来意义解释,如/b/匹配字符"b",当b前面加了反斜杆后/\b/,转意为匹配一个单词的边界. -或- 对 ...

  8. 数据连接池使用方法 JADI

    1.编写context.xml 文件复制到 WebRoot/META_INF/context.xml <?xml version='1.0' encoding='utf-8'?> < ...

  9. 土法搞docker系列之自制docker的graph driver vdisk

    写在最前 偶然整理,翻出来14年刚开始学docker的时候的好多资料.当时docker刚刚进入国内,还有很多的问题.当时我们的思考方式很简单,docker确实是个好的工具,虽然还不成熟.但是不能因为短 ...

  10. Codeforces - Gym102028 - 2018 Jiaozuo Regional Contest

    http://codeforces.com/gym/102028 A. Xu Xiake in Henan Province 看起来像水题.乱搞一下,还真是. #include<bits/std ...