class WifiAdmin {
private WifiManager mWifiManager;
private WifiInfo mWifiInfo;
private List<ScanResult> mWifiList;
private List<WifiConfiguration> mWifiConfigurations;
WifiLock mWifiLock;
public WifiAdmin(Context context) {
mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
mWifiInfo = mWifiManager.getConnectionInfo();
}
public void openWifi() {
if (!mWifiManager.isWifiEnabled()) {
mWifiManager.setWifiEnabled(true);
}
}
public void closeWifi() {
if (!mWifiManager.isWifiEnabled()) {
mWifiManager.setWifiEnabled(false);
}
}
public int checkState() {
return mWifiManager.getWifiState();
}
public void acquireWifiLock(){
mWifiLock.acquire();
}
public void releaseWifiLock() {
if(mWifiLock.isHeld()) {
mWifiLock.acquire();
}
}
public void createWifiLock(){
mWifiLock=mWifiManager.createWifiLock("test");
}
public List<WifiConfiguration> getConfiguration(){
return mWifiConfigurations;
}
public void connetionConfiguration(int index){
if(index>mWifiConfigurations.size()){
return ;
}
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();
}
public int getNetWordId(){
return (mWifiInfo==null)?0:mWifiInfo.getNetworkId();
}
public String getWifiInfo(){
return (mWifiInfo==null)?"NULL":mWifiInfo.toString();
}
public void addNetWork(WifiConfiguration configuration){
int wcgId=mWifiManager.addNetwork(configuration);
mWifiManager.enableNetwork(wcgId, true);
}
public void disConnectionWifi(int netId){
mWifiManager.disableNetwork(netId);
mWifiManager.disconnect();
}
}
<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>