作者:hosion0723 | 来源:互联网 | 2023-08-27 13:11
直接贴代码WifiAdmin.java定义WifiManager对象privateWifiManagermWifiManager;定义WifiInfo对象privateWifiIn
直接贴代码
WifiAdmin.java
private WifiManager mWifiManager; private WifiInfo mWifiInfo; private List mWifiList; private List mWifiConfiguration; WifiLock mWifiLock; private Context mctx;private int TIME_OUT=25;OnConnectWifiStateListener mOnConnectWifiStateListener = null;public interface OnConnectWifiStateListener{/*** 连接成功时调用*/void connectWifiSuccess();/*** 连接wife失败时调用*/void connectWifiFailed();}public WifiAdmin(Context context) { mctx = context;mWifiManager = (WifiManager) context .getSystemService(Context.WIFI_SERVICE); mWifiInfo = mWifiManager.getConnectionInfo(); } public void setOnConnectWifiStateListener(OnConnectWifiStateListener l) {mOnConnectWifiStateListener = l;}public void openWifi() { if (!mWifiManager.isWifiEnabled()) { mWifiManager.setWifiEnabled(true); } } public void closeWifi() { if (mWifiManager.isWifiEnabled()) { mWifiManager.setWifiEnabled(false); } } public boolean checkState() {return mWifiManager.isWifiEnabled();
} public void acquireWifiLock() { mWifiLock.acquire(); } public void releaseWifiLock() { if (mWifiLock.isHeld()) { mWifiLock.acquire(); } } public void creatWifiLock() { mWifiLock = mWifiManager.createWifiLock("Test"); } public List getConfiguration() { return mWifiConfiguration; } public void connectConfiguration(int index) { if (index > mWifiConfiguration.size()) { return; } mWifiManager.enableNetwork(mWifiConfiguration.get(index).networkId, true); } public void startScan() { mWifiManager.startScan(); mWifiList = mWifiManager.getScanResults(); mWifiConfiguration = mWifiManager.getConfiguredNetworks(); } public List getWifiList() { return mWifiList; } public StringBuilder lookUpScan() { StringBuilder stringBuilder = new StringBuilder(); for (int i = 0; i "Index_" + new Integer(i + 1).toString() + ":"); stringBuilder.append((mWifiList.get(i).SSID).toString()); stringBuilder.append("\n"); } return stringBuilder; }public String getMacAddress() { return (mWifiInfo == null) ? "NULL" : mWifiInfo.getMacAddress(); } public String getBSSID() { return (mWifiInfo == null) ? "NULL" : mWifiInfo.getBSSID(); } public int getIPAddress() { return (mWifiInfo == null) ? 0 : mWifiInfo.getIpAddress(); } public int getNetworkId() { return (mWifiInfo == null) ? 0 : mWifiInfo.getNetworkId(); } public String getWifiInfo() { return (mWifiInfo == null) ? "NULL" : mWifiInfo.toString(); }public String getSSID(){return (mWifiInfo==null) ? "NULL" : mWifiInfo.getSSID();}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);} /*** 连接配置好的Wifi*/public boolean connectConfigurationWifi(WifiConfiguration wcg){boolean b = mWifiManager.enableNetwork(wcg.networkId, true);return b;}public void disconnectWifi(int netId) { mWifiManager.disableNetwork(netId); mWifiManager.disconnect(); } 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 + "\"";
if(Type == 1) { config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE); } if(Type == 2) { 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) { 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.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);config.status = WifiConfiguration.Status.ENABLED; }return config; } /*** 判断某个网络是否已保存在配置中* @param ssid* @return*/public WifiConfiguration IsExsits(String SSID) {
for (WifiConfiguration existingConfig : mWifiConfiguration) { if (existingConfig.SSID.equals("\"" + SSID + "\"")||existingConfig.SSID.equals(SSID)) { return existingConfig; } } return null; }/*** 判断某个网络有没有连接* @param ssid* @return*/public boolean checkSSIDState(String ssid){mWifiInfo = mWifiManager.getConnectionInfo(); if(mWifiInfo!=null&&getSSID()!=null){if(getSSID().equals("\"" + ssid + "\"")||getSSID().equals(ssid)){return true;}}return false;}/*** 获取扫描的结果* @return*/public List getScanResultList(){List list=new ArrayList();if(mWifiList!=null&&mWifiList.size()>0){for (int i = 0; i "号-- "+mWifiList.get(i).SSID);} }return list;}/*** 获取已保存配置的网络* @return*/public List getConfigWifiList(){List list=new ArrayList();if(mWifiConfiguration!=null&&mWifiConfiguration.size()>0){for(int i=0;i"号-- "+mWifiConfiguration.get(i).SSID);}}return list;}/*** 判断能不能搜索到指定的网络* @param SSID* @return*/public boolean checkScanResult(String SSID){if(mWifiList!=null&&mWifiList.size()>0){for (int i = 0; i if(mWifiList.get(i).SSID.equals(SSID)||mWifiList.get(i).SSID.equals("\"" + SSID + "\"")){return true;}} }return false;}public void connectWifi(){new connectWifiThr().start();}class connectWifiThr extends Thread {@Overridepublic void run() {int timeCount = 0;if (!checkState()) {openWifi();while (!checkState()) {if (timeCount >= TIME_OUT) {mOnConnectWifiStateListener.connectWifiFailed();return;}try {sleep(1000);} catch (InterruptedException e) {e.printStackTrace();mOnConnectWifiStateListener.connectWifiFailed();return;}timeCount++;}try {sleep(2000);} catch (InterruptedException e) {e.printStackTrace();mOnConnectWifiStateListener.connectWifiFailed();return;}}if (checkSSIDState(MyApplication.wifi_ssid)) {timeCount = 0;while (!(checkState()&& checkSSIDState(MyApplication.wifi_ssid) && NetUtils.IsHaveInternet(mctx))) {if (timeCount >= TIME_OUT) {mOnConnectWifiStateListener.connectWifiFailed();return;}try {sleep(1000);} catch (InterruptedException e) {e.printStackTrace();mOnConnectWifiStateListener.connectWifiFailed();return;}timeCount++;}LogHelper.trace("连接路由花费时间:" + timeCount + "秒");mOnConnectWifiStateListener.connectWifiSuccess();} else {startScan();WifiConfiguration wcg = IsExsits(MyApplication.wifi_ssid);if (wcg != null) {connectConfigurationWifi(wcg);} else {int encryptionType = 1;if (!StringUtil.isEmpty(MyApplication.wifi_password)) {encryptionType = 3;}addNetwork(CreateWifiInfo(MyApplication.wifi_ssid,MyApplication.wifi_password, encryptionType));}timeCount = 0;while (!(checkState()&& checkSSIDState(MyApplication.wifi_ssid) && NetUtils.IsHaveInternet(mctx))) {if (timeCount >= TIME_OUT) {mOnConnectWifiStateListener.connectWifiFailed();return;}try {sleep(1000);} catch (InterruptedException e) {e.printStackTrace();mOnConnectWifiStateListener.connectWifiFailed();return;}timeCount++;}LogHelper.trace("连接路由花费时间:" + timeCount + "秒");mOnConnectWifiStateListener.connectWifiSuccess();}}}