最近在项目中需要做到考勤打卡的功能,这样就需要我们设置考勤打卡WiFi,但是WiFi的名称是可以更改的,所以就考虑的根据WiFi的MAC地址去判断当前连接的WiFi是否是我们所设置的考勤打卡的WiFi了,所以首先需要建立一个实体类将获取的周边WiFi保存到我们的本地
WifiBean.java
public class WifiBean implements Comparable<WifiBean> {
private String wifiName;
private String level;
private String state; //已连接 正在连接 未连接 三种状态
private String capabilities;//加密方式
private String macName;
@Override
public String toString() {
return "WifiBean{" +
"wifiName='" + wifiName + '\'' +
", level='" + level + '\'' +
", state='" + state + '\'' +
", capabilities='" + capabilities + '\'' +
", macName='" + macName + '\'' +
'}';
}
public String getMacName() {
return macName;
}
public void setMacName(String macName) {
this.macName = macName;
}
public String getCapabilities() {
return capabilities;
}
public void setCapabilities(String capabilities) {
this.capabilities = capabilities;
}
public String getWifiName() {
return wifiName;
}
public void setWifiName(String wifiName) {
this.wifiName = wifiName;
}
public String getLevel() {
return level;
}
public void setLevel(String level) {
this.level = level;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
@Override
public int compareTo(WifiBean o) {
int level1 = Integer.parseInt(this.getLevel());
int level2 = Integer.parseInt(o.getLevel());
return level1 - level2;
}
}
接下来就是要在onCreate中去判断WiFi的开启状态进行的操作
if (WifiSupport.isOpenWifi(SetCheckWorkActivity.this)) {
sortScaResult();
} else {
showToast("WIFI处于关闭状态或权限获取失败");
}
/**
* 获取wifi列表然后将bean转成自己定义的WifiBean
*/
public void sortScaResult() {
List<ScanResult> scanResults = WifiSupport.noSameName(WifiSupport.getWifiScanResult(this));
infoList.clear();
if (!StringUtils.isNullOrEmpty(scanResults)) {
for (int i = 0; i < scanResults.size(); i++) {
WifiBean wifiBean = new WifiBean();
wifiBean.setWifiName(scanResults.get(i).SSID);
wifiBean.setState(Constants.AppContants.WIFI_STATE_UNCONNECT); //只要获取都假设设置成未连接,真正的状态都通过广播来确定
wifiBean.setCapabilities(scanResults.get(i).capabilities);
wifiBean.setLevel(WifiSupport.getLevel(scanResults.get(i).level) + "");
wifiBean.setMacName(scanResults.get(i).BSSID);
infoList.add(wifiBean);
//排序
Collections.sort(infoList);
}
}
}
这样的话我们所需要的功能就已经实现了
虽然这样我们所需要的功能已经实现,但是在用户体验上最好还是能够实时监听用户的无线网状态
private int connectType = 0;//1:连接成功? 2 正在连接
private WifiBroadcastReceiver wifiReceiver;
//监听wifi状态
public class WifiBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (WifiManager.WIFI_STATE_CHANGED_ACTION.equals(intent.getAction())) {
int state = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, 0);
switch (state) {
/**
* WIFI_STATE_DISABLED WLAN已经关闭
* WIFI_STATE_DISABLING WLAN正在关闭
* WIFI_STATE_ENABLED WLAN已经打开
* WIFI_STATE_ENABLING WLAN正在打开
* WIFI_STATE_UNKNOWN 未知
*/
case WifiManager.WIFI_STATE_DISABLED: {
Log.d(TAG, "已经关闭");
showToast("WIFI处于关闭状态");
break;
}
case WifiManager.WIFI_STATE_DISABLING: {
Log.e(TAG, "正在关闭");
break;
}
case WifiManager.WIFI_STATE_ENABLED: {
Log.e(TAG, "已经打开");
sortScaResult();
break;
}
case WifiManager.WIFI_STATE_ENABLING: {
Log.e(TAG, "正在打开");
break;
}
case WifiManager.WIFI_STATE_UNKNOWN: {
Log.e(TAG, "未知状态");
break;
}
}
} else if (WifiManager.NETWORK_STATE_CHANGED_ACTION.equals(intent.getAction())) {
NetworkInfo info = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
Log.d(TAG, "--NetworkInfo--" + info.toString());
if (NetworkInfo.State.DISCONNECTED == info.getState()) {//wifi没连接上
Log.e(TAG, "wifi没连接上");
for (int i = 0; i < infoList.size(); i++) {//没连接上将 所有的连接状态都置为“未连接”
infoList.get(i).setState(Constants.AppContants.WIFI_STATE_UNCONNECT);
}
adapter.notifyDataSetChanged();
} else if (NetworkInfo.State.CONNECTED == info.getState()) {//wifi连接上了
Log.e(TAG, "wifi连接上了");
WifiInfo connectedWifiInfo = WifiSupport.getConnectedWifiInfo(SetCheckWorkActivity.this);
//连接成功 跳转界面 传递ip地址
connectType = 1;
wifiListSet(connectedWifiInfo.getSSID(), connectType);
} else if (NetworkInfo.State.CONNECTING == info.getState()) {//正在连接
Log.e(TAG, "wifi正在连接");
WifiInfo connectedWifiInfo = WifiSupport.getConnectedWifiInfo(SetCheckWorkActivity.this);
connectType = 2;
wifiListSet(connectedWifiInfo.getSSID(), connectType);
}
} else if (WifiManager.SCAN_RESULTS_AVAILABLE_ACTION.equals(intent.getAction())) {
Log.e(TAG, "网络列表变化了");
wifiListChange();
}
}
}
/**
* //网络状态发生改变 调用此方法!
*/
public void wifiListChange() {
sortScaResult();
WifiInfo connectedWifiInfo = WifiSupport.getConnectedWifiInfo(this);
if (connectedWifiInfo != null) {
wifiListSet(connectedWifiInfo.getSSID(), connectType);
}
}
/**
* 将"已连接"或者"正在连接"的wifi热点放置在第一个位置
*
* @param wifiName
* @param type
*/
public void wifiListSet(String wifiName, int type) {
int index = -1;
WifiBean wifiInfo = new WifiBean();
if (StringUtils.isNullOrEmpty(infoList)) {
return;
}
for (int i = 0; i < infoList.size(); i++) {
infoList.get(i).setState(Constants.AppContants.WIFI_STATE_UNCONNECT);
}
Collections.sort(infoList);//根据信号强度排序
for (int i = 0; i < infoList.size(); i++) {
WifiBean wifiBean = infoList.get(i);
if (index == -1 && ("\"" + wifiBean.getWifiName() + "\"").equals(wifiName)) {
index = i;
wifiInfo.setLevel(wifiBean.getLevel());
wifiInfo.setWifiName(wifiBean.getWifiName());
wifiInfo.setCapabilities(wifiBean.getCapabilities());
wifiInfo.setMacName(wifiBean.getMacName());
if (type == 1) {
wifiInfo.setState(Constants.AppContants.WIFI_STATE_CONNECT);
} else {
wifiInfo.setState(Constants.AppContants.WIFI_STATE_ON_CONNECTING);
}
}
}
if (index != -1) {
infoList.remove(index);
infoList.add(0, wifiInfo);
adapter.notifyDataSetChanged();
}
}
@Override
protected void onResume() {
super.onResume();
//注册广播
wifiReceiver = new WifiBroadcastReceiver();
IntentFilter filter = new IntentFilter();
filter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);//监听wifi是开关变化的状态
filter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);//监听wifi连接状态广播,是否连接了一个有效路由
filter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);//监听wifi列表变化(开启一个热点或者关闭一个热点)
this.registerReceiver(wifiReceiver, filter);
}
@Override
protected void onPause() {
super.onPause();
this.unregisterReceiver(wifiReceiver);
}
然后在考勤的时候我们首先要获取当前的网络时间滚动,所以需要在onCreate中添加
//时间刷新
new TimeThread().start();
public class TimeThread extends Thread {
@Override
public void run() {
do {
try {
Thread.sleep(1000);
Message msg = new Message();
msg.what = 1;
mHandler.sendMessage(msg);
} catch (InterruptedException e) {
e.printStackTrace();
}
} while (true);
}
}
//考勤打卡获取网络同步时间
@SuppressLint("HandlerLeak")
private Handler mHandler = new Handler() {
@SuppressLint("SetTextI18n")
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case 1:
long sysTime = System.currentTimeMillis();
CharSequence sysTimeStr = DateFormat.format("HH:mm:ss", sysTime);
checkWork.setText(sysTimeStr + "\t" + "打卡");
}
break;
}
}
};
其次我们需要获取当前连接的WiFi名称和MAC地址以判断是否在考勤打卡的范围之内
//获取当前连接的wifi信息
@SuppressLint("WifiManagerLeak")
WifiManager manager = (WifiManager) getSystemService(WIFI_SERVICE);
WifiInfo wifiInfo = manager.getConnectionInfo();
nowWifiName = wifiInfo.getSSID();
if (!StringUtils.isEmpty(nowWifiName)) {
nowWifiName = nowWifiName.substring(1, nowWifiName.length() - 1);
}
Log.e("wifi信息", "wifi名称:" +nowWifiName + "-------MAC:" + wifiInfo.getBSSID());