今天简单的来聊一下安卓开发中的Wifi,一些常用的基础,主要分为两部分:
1:WiFi的信息
2:WiFi的搜索和连接
现在app大多都需要从网络上获得数据。所以访问网络是在所难免。但是在访问网络之前,我们应该先做一下网络的状态判断。其实在访问网络之前我们要做一些状态判断,对应一些状态判断来做处理,并不是直接使用Http访问网络即可。
很多人在开发就经常把网络这块直接跳过,直接访问网络,一旦断网,各种体验效果不好,不是说app没法用,只是体验效果差。还有,就是我们可能为用户考虑,因为现在一般连网是wifi和手机流量,都知道后者收费是比较高的。假如我们的app加载的图片或者有大的数据下载操作,可是用户的本意是要是在流量下的话就不要操作这些很费流量的的操作,这样就必须要我们做一些连网状态的判断。网络是否连接良好,连接的wifi还是流量,断网或者网络改变了的时候怎么做,这都是一些细节,但是要注意处理。
WIFI开发的三个类和功能描述:
类名 | 功能 |
---|---|
WifiManager | wifi统一管理类,进行各种wifi操作 |
WifiInfo | 描述当前连接的wifi热点信息 |
WifiConfiguration | wifi网络配置信息 |
WiFI的信息
获取连接WIFI的信息
WifiManager mWifiManager=(WifiManager) this.getSystemService(Context.WIFI_SERVICE); WifiInfo mWifiInfo=mWifiManager.getConnectionInfo();
getBSSID() 获取BSSID,在手机WIFI中,就是MAC地址
getSSID() 获取SSID
getDetailedStateOf() 获取客户端的连通性
getHiddenSSID() 获得SSID是否被隐藏
getIpAddress() 获取IP地址
getLinkSpeed() 获得连接的速度
getMacAddress() 获得Mac地址
getRssi() 获得802.11n网络的信号
getSupplicanState() 返回具体客户端状态的信息
如图(简单的测试):
WiFi的搜索和连接
搜索WiFi用ListView列表显示WiFi列表,点击输入密码连接WiFi,
初始连接的的WiFi
之后(过程可以看到状态栏的WiFI改变)
重要的代码
// 定义WifiManager对象 private WifiManager mWifiManager; // 定义WifiInfo对象 private WifiInfo mWifiInfo; // 扫描出的网络连接列表 private List<ScanResult> mWifiList; // 网络连接列表 private List<WifiConfiguration> mWifiConfiguration; // 定义一个WifiLock WifiManager.WifiLock mWifiLock; // 构造器 public WifiAdmin(Context context) { // 取得WifiManager对象 mWifiManager = (WifiManager) context .getSystemService(Context.WIFI_SERVICE); // 取得WifiInfo对象 mWifiInfo = mWifiManager.getConnectionInfo(); } // 打开WIFI public void openWifi(Context context) { if (!mWifiManager.isWifiEnabled()) { mWifiManager.setWifiEnabled(true); }else if (mWifiManager.getWifiState() == 2) { Toast.makeText(context,"亲,Wifi正在开启,不用再开了", Toast.LENGTH_SHORT).show(); }else{ Toast.makeText(context,"亲,Wifi已经开启,不用再开了", Toast.LENGTH_SHORT).show(); } } // 关闭WIFI public void closeWifi(Context context) { if (mWifiManager.isWifiEnabled()) { mWifiManager.setWifiEnabled(false); }else if(mWifiManager.getWifiState() == 1){ Toast.makeText(context,"亲,Wifi已经关闭,不用再关了", Toast.LENGTH_SHORT).show(); }else if (mWifiManager.getWifiState() == 0) { Toast.makeText(context,"亲,Wifi正在关闭,不用再关了", Toast.LENGTH_SHORT).show(); }else{ Toast.makeText(context,"请重新关闭", Toast.LENGTH_SHORT).show(); } } // 检查当前WIFI状态 public void checkState(Context context) { if (mWifiManager.getWifiState() == 0) { Toast.makeText(context,"Wifi正在关闭", Toast.LENGTH_SHORT).show(); } else if (mWifiManager.getWifiState() == 1) { Toast.makeText(context,"Wifi已经关闭", Toast.LENGTH_SHORT).show(); } else if (mWifiManager.getWifiState() == 2) { Toast.makeText(context,"Wifi正在开启", Toast.LENGTH_SHORT).show(); } else if (mWifiManager.getWifiState() == 3) { Toast.makeText(context,"Wifi已经开启", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(context,"没有获取到WiFi状态", Toast.LENGTH_SHORT).show(); } } // 锁定WifiLock public void acquireWifiLock() { mWifiLock.acquire(); } // 解锁WifiLock public void releaseWifiLock() { // 判断时候锁定 if (mWifiLock.isHeld()) { mWifiLock.acquire(); } } // 创建一个WifiLock public void creatWifiLock() { mWifiLock = mWifiManager.createWifiLock("Test"); } // 得到配置好的网络 public List<WifiConfiguration> getConfiguration() { return mWifiConfiguration; } // 指定配置好的网络进行连接 public void connectConfiguration(int index) { // 索引大于配置好的网络索引返回 if (index > mWifiConfiguration.size()) { return; } // 连接配置好的指定ID的网络 mWifiManager.enableNetwork(mWifiConfiguration.get(index).networkId, true); } public void startScan(Context context) { /*mWifiManager.startScan(); // 得到扫描结果 mWifiList = mWifiManager.getScanResults(); // 得到配置好的网络连接 mWifiConfiguration = mWifiManager.getConfiguredNetworks(); if (mWifiList == null) { if(mWifiManager.getWifiState()==3){ Toast.makeText(context,"当前区域没有无线网络", Toast.LENGTH_SHORT).show(); }else if(mWifiManager.getWifiState()==2){ Toast.makeText(context,"WiFi正在开启,请稍后重新点击扫描", Toast.LENGTH_SHORT).show(); }else{ Toast.makeText(context,"WiFi没有开启,无法扫描", Toast.LENGTH_SHORT).show(); } }*/ mWifiManager.startScan(); //得到扫描结果 List<ScanResult> results = mWifiManager.getScanResults(); // 得到配置好的网络连接 mWifiConfiguration = mWifiManager.getConfiguredNetworks(); if (results == null) { if(mWifiManager.getWifiState()==3){ Toast.makeText(context,"当前区域没有无线网络",Toast.LENGTH_SHORT).show(); }else if(mWifiManager.getWifiState()==2){ Toast.makeText(context,"wifi正在开启,请稍后扫描", Toast.LENGTH_SHORT).show(); }else{Toast.makeText(context,"WiFi没有开启", Toast.LENGTH_SHORT).show(); } } else { mWifiList = new ArrayList(); for(ScanResult result : results){ if (result.SSID == null || result.SSID.length() == 0 || result.capabilities.contains("[IBSS]")) { continue; } boolean found = false; for(ScanResult item:mWifiList){ if(item.SSID.equals(result.SSID)&&item.capabilities.equals(result.capabilities)){ found = true;break; } } if(!found){ mWifiList.add(result); } } } } // 得到网络列表 public List<ScanResult> getWifiList() { return mWifiList; } // 查看扫描结果 public StringBuilder lookUpScan() { StringBuilder stringBuilder = new StringBuilder(); for (int i = 0; i < mWifiList.size(); i++) { stringBuilder .append("Index_" + new Integer(i + 1).toString() + ":"); // 将ScanResult信息转换成一个字符串包 // 其中把包括:BSSID、SSID、capabilities、frequency、level stringBuilder.append((mWifiList.get(i)).toString()); stringBuilder.append("/n"); } return stringBuilder; } // 得到MAC地址 public String getMacAddress() { return (mWifiInfo == null) ? "NULL" : mWifiInfo.getMacAddress(); } // 得到接入点的BSSID public String getBSSID() { return (mWifiInfo == null) ? "NULL" : mWifiInfo.getBSSID(); } // 得到IP地址 public int getIPAddress() { return (mWifiInfo == null) ? 0 : mWifiInfo.getIpAddress(); } // 得到连接的ID public int getNetworkId() { return (mWifiInfo == null) ? 0 : mWifiInfo.getNetworkId(); } // 得到WifiInfo的所有信息包 public String getWifiInfo() { return (mWifiInfo == null) ? "NULL" : mWifiInfo.toString(); } // 添加一个网络并连接 public void addNetwork(WifiConfiguration wcg) { int wcgID = mWifiManager.addNetwork(wcg); boolean b = mWifiManager.enableNetwork(wcgID, true); System.out.println("a--" + wcgID); System.out.println("b--" + b); } // 断开指定ID的网络 public void disconnectWifi(int netId) { mWifiManager.disableNetwork(netId); mWifiManager.disconnect(); } public void removeWifi(int netId) { disconnectWifi(netId); mWifiManager.removeNetwork(netId); } //然后是一个实际应用方法,只验证过没有密码的情况: 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 == 1) //WIFICIPHER_NOPASS { config.wepKeys[0] = ""; config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE); config.wepTxKeyIndex = 0; } if(Type == 2) //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 == 3) //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; } private WifiConfiguration IsExsits(String SSID) { List<WifiConfiguration> existingConfigs = mWifiManager.getConfiguredNetworks(); for (WifiConfiguration existingConfig : existingConfigs) { if (existingConfig.SSID.equals("\""+SSID+"\"")) { return existingConfig; } } return null; }
其他参考资料
WIFI系统和系统架构总结 : https://blog.csdn.net/liuhaomatou/article/details/40398753
Android Wifi框架流程分析: https://blog.csdn.net/King1425/article/details/68928558
源码地址: https://github.com/DickyQie/android-wifi
原文地址:https://www.cnblogs.com/zhangqie/p/8867811.html