获取WifiManager WifiManager wm = (WifiManager) this.getSystemService(Context.WIFI_SERVICE); 设置wifi功能开启 bRet = wifiManager.isWifiEnabled();//判断是否开启 bRet = wifiManager.setWifiEnabled(true);//设置开启关闭 注意,调用setWifiEnabled后,系统进行wifi模块的开启需要一定时间,此时通过wifiManager.getWifiState()获取的状态来判断是否完成。 WifiManager.WIFI_STATE_DISABLED : WIFI网卡不可用(1) WifiManager.WIFI_STATE_DISABLING : WIFI网卡正在关闭(0) WifiManager.WIFI_STATE_ENABLED : WIFI网卡可用(3) WifiManager.WIFI_STATE_ENABLING : WIFI网正在打开(2) (WIFI启动需要一段时间) WifiManager.WIFI_STATE_UNKNOWN : 未知网卡状态 while (wifiManager.getWifiState() == WifiManager.WIFI_STATE_ENABLING) { try { Thread.currentThread(); Thread.sleep(100); } catch (InterruptedException ie) { } } 扫描附近接入点AP List list = null; if ( wifiManager.startScan()) list = wifiManager.getScanResults(); 获取AP的信号强度、频率等 scanResult.level //信号强度 scanResult.frequency //频率 获取手机中保存过配置的连接信息 List existingConfigs = wifiManager.getConfiguredNetworks();//获取保存的配置信息 判断某ssid的AP是否有保存过配置信息 for (WifiConfiguration existingConfig : existingConfigs) { if (existingConfig.SSID.equals(""" + SSID + """)) { return existingConfig; } } 关闭Wifi连接 wifiManager.disableNetwork(existingConfig.networkId); 移除Wifi连接配置 wifiManager.removeNetwork(networkId); 注意与上面的disableNetwork的区别。Disable只是单纯的断开连接,保存的ssid和密码并不清除。 Remove则是从手机中移除(忘记)该连接配置,用来清除保存的密码 在网络正常连接的状态,也可直接调用remove来代替disable中断网络,但会清除密码信息。 创建连接Configuration public WifiConfiguration CreateWifiConfig(String SSID, String Password, WifiCipherType 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 == WifiCipherType.WIFICIPHER_NOPASS) { config.wepKeys[0] = ""; config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE); config.wepTxKeyIndex = 0; } //WEP加密 if (Type == WifiCipherType.WIFICIPHER_WEP) { config.preSharedKey = """ + Password + """; config.hiddenSSID = true; 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; } //WPA加密 if (Type == WifiCipherType.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.status = WifiConfiguration.Status.ENABLED; } else { return null; } return config; } 添加、开启一个wifi public boolean addNewWifi(WifiConfiguration newConfig){ int netID = wifiManager.addNetwork(newConfig);//添加 boolean bRet = wifiManager.enableNetwork(netID, false);//启动 return bRet; } 获取目前的WifiInfo(IP、mac等) WifiInfo wInfo = wifiManager.getConnectionInfo();//获取当前连接的info wInfo.getBSSID();// 获取BSSID wInfo.getHiddenSSID();// 获得SSID 是否被隐藏 wInfo.getIpAddress();// 获取IP 地址 wInfo.getLinkSpeed();// 获得连接的速度 wInfo.getMacAddress();// 获得Mac 地址 wInfo.getRssi();// 获得802.11n 网络的信号 wInfo.getSSID();// 获得SSID SupplicantState suppState = wInfo.getSupplicantState();// 返回具体客户端状态的信息 wInfo.getDetailedStateOf(suppState);// 获取客户端的连通性 相关权限 修改网络状态的权限 修改WIFI状态的权限 访问网络权限 访问WIFI权限