android下为自己定义wifimanager类

package com.FIDO.wifi;
import java.util.List;
import android.content.Context;
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;

public class wifiManager {
	private WifiManager wifiManager;  //wifi 管理
	private WifiInfo wifiInfo;   //wifi 信息
	private List<ScanResult>scanResultList; //扫描出来的网络列表
	private List<WifiConfiguration>wifiConfigList;//网络配置列表
	private WifiLock wifiLock;    //wifi锁
	
	//获取连接信息
	public wifiManager(Context context){//获取当前wifi服务
		this.wifiManager = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);
		this.wifiInfo = wifiManager.getConnectionInfo();
	}
	//得到连接信息
	public boolean getWifiStatus(){   
		 return wifiManager.isWifiEnabled();
	}
	//打开 / 关闭wifi
	public boolean openWifi(){    
		if(!wifiManager.isWifiEnabled()){
			return wifiManager.setWifiEnabled(true);
		}else{
			return false;
		}
	}
	public boolean closeWifi(){    
		if(!wifiManager.isWifiEnabled()){
			return true;
		}else{
			return wifiManager.setWifiEnabled(false);
		}
	}
	
	// 其实锁定WiFI就是判断wifi是否建立成功,在这里使用的是held,握手的意思acquire 得到
	public void lockWifi(){
	  wifiLock.acquire();
	}
	public void unLockWifi(){
	  if(!wifiLock.isHeld()){
	   wifiLock.release();
	  }
	}
	// 创建wifi锁,考虑到不是每次都会使用Wifi锁,所以干脆自己建立一个方法!需要时调用,建立就OK
	public void createWifiLock(){
	  wifiLock = wifiManager.createWifiLock("hanzhen");
	}
	//扫描网络
	public void startScan(){
	  wifiManager.startScan();
	  scanResultList = wifiManager.getScanResults();   //扫描 返回结果列表
	  wifiConfigList = wifiManager.getConfiguredNetworks(); //扫描 配置列表 
	}
	public List<ScanResult>getWifiList(){
	  return scanResultList;
	}
	public List<WifiConfiguration>getWifiConfigList(){
	  return wifiConfigList;
	}
	//获取扫描列表
	public StringBuilder lookUpscan(){
		StringBuilder scanBuilder = new StringBuilder();
		for(int i=0; i<scanResultList.size(); i++){
			scanBuilder.append("编号:"+(i+1));
			scanBuilder.append(scanResultList.get(i).toString());
			scanBuilder.append("\n");
		}
		return scanBuilder;
	}
	//获取指定信号的强度
	public int getLevel(int NetId){
		return scanResultList.get(NetId).level;
	}
	//获取本机MAC地址
	public String getMac(){
		return (wifiInfo == null) ? "" : wifiInfo.getMacAddress();
	}
	public String getBSSID(){
		return (wifiInfo == null) ? null : wifiInfo.getBSSID();
	}
	public String getSSID(){
		return (wifiInfo == null) ? null : wifiInfo.getSSID();
	}
	//返回当前连接的网络ID
	public int getCurrentNetId(){
		return (wifiInfo == null) ? null : wifiInfo.getNetworkId();
	}
	//返回所有信息
	public String getwifiInfo(){
		return (wifiInfo == null) ? null : wifiInfo.toString();
	}
	//返回IP 地址
	private String intToIp(int i){
		return (i&0xff)+"."+((i>>8)&0xff)+"."+((i>>16)&0xff)+"."+((i>>24)&0xff);
	}
	public String getIp(){
		return (wifiInfo == null) ? null : intToIp(wifiInfo.getIpAddress());
	} 
	//添加一个连接
	public boolean addNetWorkLink(WifiConfiguration config){
		int NetId = wifiManager.addNetwork(config);
		return wifiManager.enableNetwork(NetId, true);
	}
	//禁用一个连接
	public boolean disableNetWorkLink(int NetId){
		wifiManager.disableNetwork(NetId);
		return wifiManager.disconnect();
	}
	//移除一个连接
	public boolean removeNetWorkLink(int NetId){
		return wifiManager.removeNetwork(NetId);
	}
	//不显示SSID
	public void hiddenSSID(int NetId){
		wifiConfigList.get(NetId).hiddenSSID = true;
	}
	//显示SSID
	public void displaySSID(int NetId){
		wifiConfigList.get(NetId).hiddenSSID = false;
	}
}


----------------------------------------------------------

根据个人情况可加入log查验错误。

*******************************************************************************

13-3-29  飞扬小米(记)

你可能感兴趣的:(android下为自己定义wifimanager类)