本文出自:ttp://rayln.iteye.com/blog/1229665,谢谢作者的辛苦笔记!
package com.wifi;
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.WifiManager;
import android.util.Log;
public class NetChoice {
// String wifiSSID="FORTUNETONE";
Context context;
WifiManager wifiManager;
// WifiReceiver wifiReceiver;
public NetChoice(Context context) {
this.context = context;
wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
// wifiReceiver=new WifiReceiver();
// context.registerReceiver(wifiReceiver, new
// IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
}
/**
* 判断当前网络连接状态 0、无连接 1、3G 2、WIFI
*
* @return
* @author 2011-08-16
*/
public int checkNetworkInfo() {
int netType = 0;
ConnectivityManager conMan = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
State mobile = conMan.getNetworkInfo(ConnectivityManager.TYPE_MOBILE)
.getState();
State wifi = conMan.getNetworkInfo(ConnectivityManager.TYPE_WIFI)
.getState();
if (mobile == State.CONNECTED || mobile == State.CONNECTING) {
netType = 1;
}
if (wifi == State.CONNECTED || wifi == State.CONNECTING) {
netType = 2;
}
Log.d("carWifi", wifi + "---wifi--state--mobile--" + mobile
+ "---type--" + netType);
return netType;
}
/**
* WIFI勾选 开关
*
* @author 2011-08-16
*/
public void openWifi() {
boolean b = false;
if (!wifiManager.isWifiEnabled()) {
b = wifiManager.setWifiEnabled(true);
Log.d("carWifi", "---------open wifi --" + b);
} else {
Log.d("carWifi", "-----wifi was open!!--");
}
}
public void closeWifi() {
if (wifiManager.isWifiEnabled()) {
wifiManager.setWifiEnabled(false);
}
}
/**
* 添加WIFI信息
*
* @param wifiList
* 搜索到的wifi集合
* @param wifiSSID
* wifi名称
* @param pwd
* wifi密码
* @return wifiId
* @author 2011-08-16
*/
public int addWifiConfig(List<ScanResult> wifiList, String wifiSSID,
String pwd) {
int wifiId = -1;
if (wifiList != null) {
for (int i = 0; i < wifiList.size(); i++) {
ScanResult sr = wifiList.get(i);
if (sr.SSID.equals(wifiSSID)) {
WifiConfiguration wc = new WifiConfiguration();
wc.SSID = "\"" + wifiSSID + "\"";
wc.preSharedKey = "\"" + pwd + "\"";
wifiId = wifiManager.addNetwork(wc);
if (wifiId != -1) {
return wifiId;
}
}
}
}
return wifiId;
}
/**
* 连接wifi
*
* @param wifiId
* @return
* @author 2011-08-16
*/
public boolean connectWifi(int wifiId) {
for (int i = 0; i < wifiManager.getConfiguredNetworks().size(); i++) {
WifiConfiguration wc = wifiManager.getConfiguredNetworks().get(i);
if (wc.networkId == wifiId) {
return wifiManager.enableNetwork(wc.networkId, true);
}
}
return false;
}
public List<WifiConfiguration> getConnectionWifi(){
return wifiManager.getConfiguredNetworks();
}
public boolean addWifiConnection(String wifiSSID,String pwd){
boolean isConnection = false;
WifiConfiguration wc = new WifiConfiguration();
wc.SSID = "\"" + wifiSSID + "\"";
wc.preSharedKey = "\"" + pwd + "\"";
int wifiId = wifiManager.addNetwork(wc);
System.out.println("wifiId:"+wifiId);
if (wifiId != -1) {
isConnection = wifiManager.enableNetwork(wifiId, true);
}
return isConnection;
}
/**
* 扫描wifi热点
*
* @return wifiList
* @author 2011-08-16
*/
public List<ScanResult> scanWifi() {
// wifiManager.startScan();
return wifiManager.getScanResults();
}
// class WifiReceiver extends BroadcastReceiver{
//
// @Override
// public void onReceive(Context context, Intent intent)
// {
// // TODO Auto-generated method stub
// wifiList=wifiManager.getScanResults();
// }
//
// }
}