<span style="font-size:24px;">wifi管理帮助类,可直接用,有网上搜的一些方法,也有自己添加的一些方法,其中需要添加的权限:</span>
<span style="font-size:24px;"><span style="color: rgb(69, 69, 69); font-family: arial, 宋体, sans-serif, tahoma, 'Microsoft YaHei'; font-size: 14px; line-height: 24px;"></span></span><pre style="line-height: 26.600000381469727px; font-family: 'Courier New', Courier, monospace;"><uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"></uses-permission>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
<span style="font-size:24px;">
</span>
import java.util.Iterator;
import java.util.List;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo.State;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.net.wifi.WifiManager.WifiLock;
import android.text.TextUtils;
public class WifiUtil {
private Context context;
//定义一个WifiManager对象
private WifiManager mWifiManager;
//定义一个WifiInfo对象
private WifiInfo mWifiInfo;
//扫描出的网络连接列表
private List<ScanResult> mWifiList;
//网络连接列表
private List<WifiConfiguration> mWifiConfigurations;
WifiLock mWifiLock;
public WifiUtil(Context context){
//取得WifiManager对象
mWifiManager=(WifiManager) context.getSystemService(Context.WIFI_SERVICE);
//取得WifiInfo对象
mWifiInfo=mWifiManager.getConnectionInfo();
this.context=context;
}
//打开wifi
public boolean openWifi(){
boolean isOk=false;
if(!mWifiManager.isWifiEnabled()){
isOk= mWifiManager.setWifiEnabled(true);
}
return isOk;
}
//关闭wifi
public boolean closeWifi(){
boolean isOk=false;
if(mWifiManager.isWifiEnabled()){
isOk=mWifiManager.setWifiEnabled(false);
}
return isOk;
}
// 检查当前wifi状态
public int checkState() {
return mWifiManager.getWifiState();
}
//锁定wifiLock
public void acquireWifiLock(){
mWifiLock.acquire();
}
//解锁wifiLock
public void releaseWifiLock(){
//判断是否锁定
if(mWifiLock.isHeld()){
mWifiLock.acquire();
}
}
//创建一个wifiLock
public void createWifiLock(){
mWifiLock=mWifiManager.createWifiLock("test");
}
//得到配置好的网络
public List<WifiConfiguration> getConfiguration(){
return mWifiConfigurations;
}
//指定配置好的网络进行连接
public void connetionConfiguration(int index){
if(index>mWifiConfigurations.size()){
return ;
}
//连接配置好指定ID的网络
mWifiManager.enableNetwork(mWifiConfigurations.get(index).networkId, true);
}
public void startScan(){
mWifiManager.startScan();
//得到扫描结果
mWifiList=mWifiManager.getScanResults();
//得到配置好的网络连接
mWifiConfigurations=mWifiManager.getConfiguredNetworks();
}
//得到网络列表
public List<ScanResult> getWifiList(){
return mWifiList;
}
//查看扫描结果
public StringBuffer lookUpScan(){
StringBuffer sb=new StringBuffer();
for(int i=0;i<mWifiList.size();i++){
sb.append("Index_" + new Integer(i + 1).toString() + ":");
// 将ScanResult信息转换成一个字符串包
// 其中把包括:BSSID、SSID、capabilities、frequency、level
sb.append((mWifiList.get(i)).toString()).append("\n");
}
return sb;
}
public String getMacAddress(){
return (mWifiInfo==null)?"NULL":mWifiInfo.getMacAddress();
}
public String getBSSID(){
return (mWifiInfo==null)?"NULL":mWifiInfo.getBSSID();
}
public int getIpAddress(){
return (mWifiInfo==null)?0:mWifiInfo.getIpAddress();
}
//得到连接的ID
public int getNetWordId(){
return (mWifiInfo==null)?0:mWifiInfo.getNetworkId();
}
//得到wifiInfo的所有信息
public String getWifiInfo(){
return (mWifiInfo==null)?"NULL":mWifiInfo.toString();
}
//添加一个网络并连接
public boolean addNetWork(WifiConfiguration configuration){
int wcgId=mWifiManager.addNetwork(configuration);
mWifiManager.saveConfiguration();
return mWifiManager.enableNetwork(wcgId, true);
}
//断开指定ID的网络
public void disConnectionWifi(int netId){
mWifiManager.disableNetwork(netId);
mWifiManager.disconnect();
}
// 判断wifi连接状态
public boolean isWifiAvailable() {
ConnectivityManager conMan = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
State wifi = conMan.getNetworkInfo(ConnectivityManager.TYPE_WIFI)
.getState();
if (State.CONNECTED == wifi) {
return true;
} else {
return false;
}
}
/**
* 创建一个wifi信息
* @param ssid 名称
* @param passawrd 密码
* @param paramInt 有3个参数,1是无密码,2是简单密码,3是wap加密
* @param type 是"ap"还是"wifi"
* @return
*/
public WifiConfiguration createWifiInfo(String ssid, String passawrd,int paramInt, String type) {
//配置网络信息类
WifiConfiguration localWifiConfiguration1 = new WifiConfiguration();
//设置配置网络属性
localWifiConfiguration1.allowedAuthAlgorithms.clear();
localWifiConfiguration1.allowedGroupCiphers.clear();
localWifiConfiguration1.allowedKeyManagement.clear();
localWifiConfiguration1.allowedPairwiseCiphers.clear();
localWifiConfiguration1.allowedProtocols.clear();
if(type.equals("wt")) { //wifi连接
localWifiConfiguration1.SSID = ("\"" + ssid + "\"");
WifiConfiguration localWifiConfiguration2 = isExsits(ssid);
if(localWifiConfiguration2 != null) {
mWifiManager.removeNetwork(localWifiConfiguration2.networkId); //从列表中删除指定的网络配置网络
}
if(paramInt == 1) { //没有密码
localWifiConfiguration1.wepKeys[0] = "";
localWifiConfiguration1.allowedKeyManagement.set(0);
localWifiConfiguration1.wepTxKeyIndex = 0;
} else if(paramInt == 2) { //简单密码
localWifiConfiguration1.hiddenSSID = true;
localWifiConfiguration1.wepKeys[0] = ("\"" + passawrd + "\"");
} else { //wap加密
localWifiConfiguration1.preSharedKey = ("\"" + passawrd + "\"");
localWifiConfiguration1.hiddenSSID = true;
localWifiConfiguration1.allowedAuthAlgorithms.set(0);
localWifiConfiguration1.allowedGroupCiphers.set(2);
localWifiConfiguration1.allowedKeyManagement.set(1);
localWifiConfiguration1.allowedPairwiseCiphers.set(1);
localWifiConfiguration1.allowedGroupCiphers.set(3);
localWifiConfiguration1.allowedPairwiseCiphers.set(2);
}
}else {//"ap" wifi热点
localWifiConfiguration1.SSID = ssid;
localWifiConfiguration1.allowedAuthAlgorithms.set(1);
localWifiConfiguration1.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
localWifiConfiguration1.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
localWifiConfiguration1.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
localWifiConfiguration1.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);
localWifiConfiguration1.allowedKeyManagement.set(0);
localWifiConfiguration1.wepTxKeyIndex = 0;
if (paramInt == 1) { //没有密码
localWifiConfiguration1.wepKeys[0] = "";
localWifiConfiguration1.allowedKeyManagement.set(0);
localWifiConfiguration1.wepTxKeyIndex = 0;
} else if (paramInt == 2) { //简单密码
localWifiConfiguration1.hiddenSSID = true;//网络上不广播ssid
localWifiConfiguration1.wepKeys[0] = passawrd;
} else if (paramInt == 3) {//wap加密
localWifiConfiguration1.preSharedKey = passawrd;
localWifiConfiguration1.allowedAuthAlgorithms.set(0);
localWifiConfiguration1.allowedProtocols.set(1);
localWifiConfiguration1.allowedProtocols.set(0);
localWifiConfiguration1.allowedKeyManagement.set(1);
localWifiConfiguration1.allowedPairwiseCiphers.set(2);
localWifiConfiguration1.allowedPairwiseCiphers.set(1);
}
}
return localWifiConfiguration1;
}
/**
* 创建网络配置
* @param ssid
* @param passwd
* @param type
* @return
*/
public WifiConfiguration createWifiConfiguration(String ssid, String passwd, String 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.equals("NOPASS")) {
config.wepKeys[0] = "";
config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
config.wepTxKeyIndex = 0;
} else if (type.equals("WEP")) {
config.preSharedKey = "\"" + passwd + "\"";
config.hiddenSSID = true;
config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.SHARED);
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);
config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
config.wepTxKeyIndex = 0;
} else if (type.equals("WPA")) {
config.preSharedKey = "\"" + passwd + "\"";
config.hiddenSSID = true;
config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_EAP);
config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
// WPA
config.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
// WPA2
config.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
config.status = WifiConfiguration.Status.ENABLED;
} else {
return null;
}
return config;
}
/**
* 是否存在网络信息
* @param str 热点名称
* @return
*/
private WifiConfiguration isExsits(String str) {
Iterator localIterator = this.mWifiManager.getConfiguredNetworks().iterator();
WifiConfiguration localWifiConfiguration;
do {
if(!localIterator.hasNext()) return null;
localWifiConfiguration = (WifiConfiguration) localIterator.next();
}while(!localWifiConfiguration.SSID.equals("\"" + str + "\""));
return localWifiConfiguration;
}
// 计算信号强度
public String calSignalLevel(int numLevels) {
String s="";
if (numLevels<=0 && numLevels>=-50) {
s="强";
} else if (numLevels<-50 && numLevels>=-70) {
s="一般";
} else {
s="弱";
}
return s;
}
//网络安全性
public String getCipherType(String capabilities ){
String cipherType="";
if (!TextUtils.isEmpty(capabilities)) {
if (capabilities.contains("WPA") || capabilities.contains("wpa")) {
cipherType="WPA";
} else if (capabilities.contains("WEP") || capabilities.contains("wep")) {
cipherType="WEP";
} else {
cipherType="NOPASS";
}
}
return cipherType;
}
}