这是一个无线网络的工具类包含一些无线网络信息的设置,判断,状态等详情请看注释,后续会补上的wifi静态IP设置,信息获取等,代码下载地址https://download.csdn.net/download/qq_35345103/12845590
// 取得WifiManager对象
mWifiManager = (WifiManager) context
.getSystemService(Context.WIFI_SERVICE);
// 取得WifiInfo对象
mWifiInfo = mWifiManager.getConnectionInfo();
// 打开WIFI
public void setWifistate(Boolean state) {
//判断是热点是否打开如果打开就关闭
if(mWifiManager.getWifiApState() == WifiManager.WIFI_AP_STATE_ENABLED || mWifiManager.getWifiApState() == WifiManager.WIFI_AP_STATE_ENABLING){
mWifiManager.setWifiApEnabled(mWifiManager.getWifiApConfiguration(),false);
}
mWifiManager.setWifiEnabled(state);
}
//判断wifi是否连接
public Boolean iswifienabled(){
return mWifiManager.isWifiEnabled();
}
// 检查当前WIFI状态
public Boolean getwifiState() {
if (mWifiManager.getWifiState() == 0) {
return false;
} else if (mWifiManager.getWifiState() == 1) {
return false;
} else if (mWifiManager.getWifiState() == 2) {
return true;
} else if (mWifiManager.getWifiState() == 3) {
return true;
} else {
return false;
}
}
// 得到保存过的网络信息
public List<WifiConfiguration> getConfiguration() {
return mWifiManager.getConfiguredNetworks();
}
//获取当前连接的wifi信息
public WifiConfiguration getconnectconfig(){
List<WifiConfiguration> wifiConfigList = mWifiManager.getConfiguredNetworks();
for (WifiConfiguration wifiConfiguration : wifiConfigList) {
//配置过的SSID
String configSSid = wifiConfiguration.SSID;
configSSid = utils.getreplace(configSSid);
//当前连接SSID
String currentSSid = getSSiD();
currentSSid = utils.getreplace(currentSSid);
//比较networkId,防止配置网络保存相同的SSID
if (configSSid.equals(currentSSid) && mWifiInfo.getNetworkId() == wifiConfiguration.networkId) {
return wifiConfiguration;
}
}
return null;
}
//判断是否为保存的wifi
public WifiConfiguration getconnectdown(ScanResult ScanResult){
List<WifiConfiguration> wifiConfigList = mWifiManager.getConfiguredNetworks();
for (WifiConfiguration wifiConfiguration : wifiConfigList) {
//配置过的SSID
String configSSid = wifiConfiguration.SSID;
configSSid = utils.getreplace(configSSid);
//当前连接SSID
String currentSSid = utils.getreplace(ScanResult.SSID);
Logutll.d("保存的id"+configSSid +currentSSid);
//比较networkId,防止配置网络保存相同的SSID
if (configSSid.equals(currentSSid)) {
Logutll.d("匹配");
return wifiConfiguration;
}
}
return null;
}
//得到未连接的网络列表
public List<ScanResult> getwifilistconfig(){
List<ScanResult> wifiConfigList = mWifiManager.getScanResults();
for (int i = 0; i < wifiConfigList.size(); i++) {
//配置过的SSID
String configSSid = wifiConfigList.get(i).SSID;
configSSid = utils.getreplace(configSSid);
//当前连接SSID
String currentSSid = getSSiD();
currentSSid = utils.getreplace(currentSSid);
//比较networkId,防止配置网络保存相同的SSID
if (configSSid.equals(currentSSid) && mWifiInfo.getBSSID().equals(wifiConfigList.get(i).BSSID)) {
wifiConfigList.remove(i);
}
}
return wifiConfigList;
}
/**
* 忘记某一个wifi密码
*
* @param targetSsid
*/
public void removeWifiBySsid(String targetSsid) {
Logutll.d("remossids"+targetSsid);
List<WifiConfiguration> wifiConfigs = mWifiManager.getConfiguredNetworks();
for (WifiConfiguration wifiConfig : wifiConfigs) {
String ssid = utils.getreplace(wifiConfig.SSID);
Logutll.d("remossids"+ssid+targetSsid);
if (ssid.equals(targetSsid)) {
Logutll.d("remossid"+ssid);
removeWifi(wifiConfig.networkId);
}
}
}
// 得到扫描结果
public Boolean startScan() {
return mWifiManager.startScan();
}
//判断是否静态
public Boolean getwificonfigstatic(WifiConfiguration wifiConfiguration){
return wifiConfiguration.getIpAssignment() == IpConfiguration.IpAssignment.STATIC;
}
//获取连接wifi的config
public WifiConfiguration getWifiConfiguration(int networkId) {
List<WifiConfiguration> configuredNetworks = mWifiManager.getConfiguredNetworks();
if (configuredNetworks != null) {
for (WifiConfiguration configuredNetwork : configuredNetworks) {
if (configuredNetwork.networkId == networkId) {
return configuredNetwork;
}
}
}
return null;
}
// 得到网络列表
public List<ScanResult> getWifiList() {
return mWifiManager.getScanResults();
}
// 得到MAC地址
public String getMacAddress() {
return (mWifiInfo == null) ? "NULL" : mWifiInfo.getMacAddress();
}
// 得到接入点的BSSID
public String getBSSID() {
return (mWifiInfo == null) ? null : mWifiInfo.getBSSID();
}
// 得到IP地址
public String getIPAddress() {
return utils.getfrmentIPinfo((mWifiInfo == null) ? 0 : mWifiInfo.getIpAddress());
}
// 得到连接的ID
public int getNetworkId() {
return (mWifiInfo == null) ? 0 : mWifiInfo.getNetworkId();
}
// 得到WifiInfo的所有信息包
public String getWifiInfo() {
mWifiInfo = mWifiManager.getConnectionInfo();
return (mWifiInfo == null) ? null : mWifiInfo.toString();
}
//获取wifi名
public String getSSiD() {
mWifiInfo = mWifiManager.getConnectionInfo();
return utils.getreplace((mWifiInfo == null) ? null : mWifiInfo.getSSID());
}
//获取信号强度
public int getRssiD() {
return (mWifiInfo == null) ? -100 : mWifiInfo.getRssi();
}
// 添加一个网络并连接
public void addNetwork(WifiConfiguration wcg) {
mWifiManager.disconnect();
int wcgID = mWifiManager.addNetwork(wcg);
boolean b = mWifiManager.enableNetwork(wcgID, true);
Logutll.d("wifiid"+wcgID + b);
}
// 断开指定ID的网络
public void disconnectWifi(int netId) {
mWifiManager.disableNetwork(netId);
mWifiManager.disconnect();
}
public void removeWifi(int netId) {
disconnectWifi(netId);
mWifiManager.removeNetwork(netId);
mWifiManager.saveConfiguration();
}
public WifiConfiguration CreateWifiInfo(String SSID, String Password, int Type)
{
WifiConfiguration config = new WifiConfiguration();
config.allowedAuthAlgorithms.clear();
config.allowedGroupCiphers.clear();
config.allowedKeyManagement.clear();
config.allowedPairwiseCiphers.clear();
config.allowedProtocols.clear();
config.SSID = "\"" + SSID + "\"";
WifiConfiguration tempConfig = this.IsExsits(SSID);
if(tempConfig != null) {
mWifiManager.removeNetwork(tempConfig.networkId);
}
if(Type == 0) //WIFICIPHER_NOPASS
{
config.wepKeys[0] = "";
config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
config.wepTxKeyIndex = 0;
}
if(Type == 1) //WIFICIPHER_WEP
{
config.hiddenSSID = true;
config.wepKeys[0]= "\""+Password+"\"";
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;
}
if(Type == 2) //WIFICIPHER_WPA
{
config.preSharedKey = "\""+Password+"\"";
config.hiddenSSID = true;
config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
//config.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
config.status = WifiConfiguration.Status.ENABLED;
}
return config;
}