首先是权限,因为WIFI也是可以用于定位的,只是精度没那么高而已,所以6.0之后的权限系统对WIFI的权限进行了限制,除了需要日常的WIFI操作权限之外,还需要定位权限,否则会获取不到WIFI的信息。
<!--完全的网络访问权限-->
<uses-permission android:name="android.permission.INTERNET" />
<!-- 以下是使用wifi访问网络所需的权限 -->
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<!--定位权限-->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS" />
大体步骤,就是:
1、显示列表(已连接、已保存的、已保存但没信号的等等,需要根据不同的状态区别显示并排序)
2、判定是否已经保存,如果保存了直接连接
3、没保存,提示输入密码,没有密码就跳过
4、代码自动将需要连接的WIFI以及密码,写入系统保存中,写了之后再进行连接
其他需要注意:保存了的忘了有可能也需要修改密码,SSID不能用作唯一码,因为WIFI名称是有可能是一样的。
下面是工具类代码:
package com.imxiaoyu.common.utils;
import android.app.Activity;
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.text.TextUtils;
import com.imxiaoyu.common.base.helper.BaseHelper;
import java.util.List;
/**
* WIFI连接工具类
* Created by 庞光渝 on 2017/3/1.
*/
public class WifiUtils extends BaseHelper {
private WifiManager wifiManager;
public WifiUtils(Activity activity) {
super(activity);
wifiManager = (WifiManager) getActivity().getSystemService(Context.WIFI_SERVICE);
doStartScan();
}
/**
* 扫码、刷新WIFI
*/
public void doStartScan() {
wifiManager.startScan();
}
/**
* 是否已经开启了WIFI
* @return
*/
public boolean isWifiOpen(){
if(wifiManager==null){
return false;
}
return wifiManager.getWifiState() == WifiManager.WIFI_STATE_ENABLED;
}
/**
* 开、关 WIFI
*
* @param bln
*/
public void doSwitchWifi(boolean bln) {
if (wifiManager != null) {
wifiManager.setWifiEnabled(bln);
}
}
/**
* 返回WIFI列表,并按信号强度排序
*
* @return
*/
public List<ScanResult> getScanResults() {
return ascWifiLevel(wifiManager.getScanResults());//得到扫描结果
}
/**
* 断开网络
*/
public void disableNetwork(int wifiId){
wifiManager.disableNetwork(wifiId);
}
/**
* 不保存网络
* @param wifiId
*/
public void removeNetwork(int wifiId){
wifiManager.removeNetwork(wifiId);
}
/**
* 不保存网络
* @param SSID
*/
public void removeNetwork(String SSID){
removeNetwork(isConfiguration(SSID));
}
/**
* 返回当前正在连接的WIFI的信息(正在连接,也包括没连接上或者密码错误状态,相当于是一个占位的状态)
* @return
*/
public WifiInfo getWifiInfo(){
return wifiManager.getConnectionInfo();
}
/**
* 得到配置好的(即已经保存了密码的)WIFI的信息
*/
public List<WifiConfiguration> getConfiguration() {
return wifiManager.getConfiguredNetworks();
}
/**
* 判定指定的WIFI已经配置好,然后根据WIFI的地址bssid返回NETId;
*
* @param SSID WifiConfiguration这个列表的SSID是前后带有双引号的,传进来的时候一定要注意
* @return
*/
public int isConfiguration(String SSID) {
List<WifiConfiguration> wifiConfigurations = getConfiguration();
if(wifiConfigurations!=null) {
for (int i = 0; i < wifiConfigurations.size(); i++) {
if (wifiConfigurations.get(i).SSID.equals(SSID)) {//地址相同
return wifiConfigurations.get(i).networkId;
}
}
}
return -1;
}
/**
* 判定指定的WIFI已经配置好,然后根据WIFI的地址bssid返回NETId;
*
* @param BSSID WifiConfiguration这个列表的SSID是前后带有双引号的,传进来的时候一定要注意
* @return
*/
public int isConfigurationBSSID(String BSSID) {
List<WifiConfiguration> wifiConfigurations = getConfiguration();
if(wifiConfigurations!=null) {
for (int i = 0; i < wifiConfigurations.size(); i++) {
if (wifiConfigurations.get(i).BSSID!=null&&wifiConfigurations.get(i).BSSID.equals(BSSID)) {//地址相同
return wifiConfigurations.get(i).networkId;
}
}
}
return -1;
}
/**
* 添加指定WIFI的配置信息,原列表不存在此SSID
*
* @param wifiList
* @param ssid
* @param pwd
* @return
*/
public int addWifiConfig(List<ScanResult> wifiList, String ssid, String pwd) {
int wifiId = -1;
for (int i = 0; i < wifiList.size(); i++) {
ScanResult wifi = wifiList.get(i);
if (wifi.SSID.equals(ssid)) {
WifiConfiguration wifiCong = new WifiConfiguration();
wifiCong.SSID = "\"" + wifi.SSID + "\"";//\"转义字符,代表"
wifiCong.preSharedKey = "\"" + pwd + "\"";//WPA-PSK密码
wifiCong.hiddenSSID = false;
wifiCong.status = WifiConfiguration.Status.ENABLED;
wifiId = wifiManager.addNetwork(wifiCong);//将配置好的特定WIFI密码信息添加,添加完成后默认是不激活状态,成功返回ID,否则为-1
if (wifiId != -1) {
return wifiId;
}
}
}
return wifiId;
}
/**
* 连接指定的WIFI
*
* @param wifiId
* @return
*/
public boolean doConnectWifi(int wifiId) {
List<WifiConfiguration> wifiConfigurations = getConfiguration();
for (int i = 0; i < wifiConfigurations.size(); i++) {
WifiConfiguration wifi = wifiConfigurations.get(i);
if (wifi.networkId == wifiId) {
while (!(wifiManager.enableNetwork(wifiId, true))) {//激活该Id,建立连接
//status:0--已经连接,1--不可连接,2--可以连接
ALog.e("ConnectWifi", String.valueOf(wifiConfigurations.get(wifiId).status));
}
return true;
}
}
return false;
}
/**
* 取消保存
*
* @param networkID
*/
public void removeNetword(int networkID) {
wifiManager.removeNetwork(networkID);
wifiManager.saveConfiguration();
}
/**
* 排序一下信号强度
*
* @param list
* @return
*/
public static List<ScanResult> ascWifiLevel(List<ScanResult> list) {
for (int i = 0; i < list.size(); i++) {
for (int j = 0; j < list.size() - 1; j++) {
if (list.get(j).level < list.get(j + 1).level) {
ScanResult scanResult = list.get(j);
list.set(j, list.get(j + 1));
list.set(j + 1, scanResult);
}
}
}
return list;
}
/**
* 返回一个WIFI的加密方式
*
* @param ssid
* @return
*/
public String getCipherType(String ssid) {
WifiManager wifiManager = (WifiManager) getActivity().getSystemService(Context.WIFI_SERVICE);
List<ScanResult> list = wifiManager.getScanResults();
for (ScanResult scResult : list) {
if (!TextUtils.isEmpty(scResult.SSID) && scResult.SSID.equals(ssid)) {
String capabilities = scResult.capabilities;
if (!TextUtils.isEmpty(capabilities)) {
if (capabilities.contains("WPA") || capabilities.contains("wpa")) {
return "WAP";
} else if (capabilities.contains("WEP") || capabilities.contains("wep")) {
return "WEP";
} else {
return null;
}
}
}
}
return null;
}
}
参考博客:
http://blog.csdn.net/caoliang0921/article/details/8799530
http://www.soso.io/article/100080.html
http://blog.csdn.net/u014774589/article/details/48950727