Android掌控WiFi不完全指南( 二 )

开启、关闭WIFI// 开启/关闭 WIFI.public static boolean setWifiEnabled(WifiManager manager, boolean enabled) {return manager != null && manager.setWifiEnabled(enabled);}扫描周围的WiFi// 开始扫描 WIFI.public static void startScanWifi(WifiManager manager) {if (manager != null) {manager.startScan();}}获取扫描结果// 获取扫描 WIFI 的热点:public static List<ScanResult> getScanResult(WifiManager manager) {return manager == null ? null : manager.getScanResult();}获取历史WiFi配置信息// 获取已经保存过的/配置好的 WIFI 热点.public static List<WifiConfiguration> getConfiguredNetworks(WifiManager manager) {return manager == null ? null : manager.WifiConfiguration();}获取对应scanResult的配置信息List<WifiConfiguration> configs = wifiManager.getMatchingWifiConfig(scanResult);// 可以打印一下看具体的情况:if (configs == null || configs.isEmpty()) return;for (WifiConfiguration config : configs) {Log.v(TAG, "config = " + config);}获取WIFI MAC地址public String getWifiBSSID() {return mWifiInfo.getBSSID();}获取本机MAC地址Android M版本之后,通过wifiInfo.getMacAddress()获取的MAC地址是一个固定的假地址,值为02:00:00:00:00:00,在这里通过getMacAddress函数获取真实MAC
// 获取本机MAC地址// Android M版本之后,通过wifiInfo.getMacAddress()获取的MAC地址是一个固定的假地址,值为02:00:00:00:00:00public String getSelfMac(){String mac=mWifiInfo==null?"null":mWifiInfo.getMacAddress();if(TextUtils.equals(mac, "02:00:00:00:00:00")) {String temp = getMacAddress();if (!TextUtils.isEmpty(temp)) {mac = temp;}}return mac;}private static String getMacAddress(){String macAddress = "";try {Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();while (interfaces.hasMoreElements()) {NetworkInterface iF = interfaces.nextElement();byte[] addr = iF.getHardwareAddress();if (addr == null || addr.length == 0) {continue;}StringBuilder buf = new StringBuilder();for (byte b : addr) {buf.append(String.format("%02X:", b));}if (buf.length() > 0) {buf.deleteCharAt(buf.length() - 1);}String mac = buf.toString();//WifiMonitorLogger.i("mac", "interfaceName="+iF.getName()+", mac="+mac);if(TextUtils.equals(iF.getName(), "wlan0")){return mac;}}} catch (SocketException e) {e.printStackTrace();return macAddress;}return macAddress;}获取WIFI的网络速度和速度单位// 获取当前连接wifi的速度public int getConnWifiSpeed(){return mWifiInfo.getLinkSpeed();}// 获取当前连接wifi的速度单位public String getConnWifiSpeedUnit(){return WifiInfo.LINK_SPEED_UNITS;}获取当前连接WIFI的信号强度// 获取当前连接wifi的信号强度public int getConnWifiLevel(){return mWifiManager.calculateSignalLevel(mWifiInfo.getRssi(),5);}获取当前连接的WIFI的加密方式本来我以为wifiinfo里面应该会有解决方案 , 但是搜索了一下之后发现 如何在不扫描所有wifi网络的情况下获取当前wifi连接的加密类型?看来还是需要遍历scanresults , 但是很显然SSID容易重复 , 所以用WIFI BSSID来唯一确定
// 获取当前WIFI连接的加密方式// capabilities的格式是 [认证标准+秘钥管理+加密方案]public String getConnCap(){String currentBSSID=mWifiInfo.getBSSID();for(ScanResult result:scanResultList){//WifiMonitorLogger.i(currentBSSID+":"+result.BSSID);if(currentBSSID.equals(result.BSSID)){return result.capabilities;}}return "null";}另外返回的capabilities格式一般为[认证标准+秘钥管理+加密方案] , 所以看到的时候不用太慌张可以通过以下方式来判定加密
static final int SECURITY_NONE = 0;static final int SECURITY_WEP = 1;static final int SECURITY_PSK = 2;static final int SECURITY_EAP = 3;private int getType(ScanResult result) {if (result == null) {return SECURITY_NONE;}String capbility = result.capabilities;if (capbility == null || capbility.isEmpty()) {return SECURITY_NONE;}// 如果包含WAP-PSK的话,则为WAP加密方式if (capbility.contains("WPA-PSK") || capbility.contains("WPA2-PSK")) {return SECURITY_PSK;} else if (capbility.contains("WPA2-EAP")) {return SECURITY_EAP;} else if (capbility.contains("WEP")) {return SECURITY_WEP;} else if (capbility.contains("ESS")) {// 如果是ESS则没有密码return SECURITY_NONE;}return SECURITY_NONE;}JAVA代码连接WiFiAndroid提供了两种方式连接WiFi:

  • 通过配置连接
  • 通过networkId连接
封装后的函数如下
// 使用 WifiConfiguration 连接.public static void connectByConfig(WifiManager manager, WifiConfiguration config) {if (manager == null) {return;}try {Method connect = manager.getClass().getDeclaredMethod("connect", WifiConfiguration.class, Class.forName("android.net.wifi.WifiManager$ActionListener"));if (connect != null) {connect.setAccessible(true);connect.invoke(manager, config, null);}} catch (Exception e) {e.printStackTrace();}}// 使用 networkId 连接.public static void connectByNetworkId(WifiManager manager, int networkId) {if (manager == null) {return;}try {Method connect = manager.getClass().getDeclaredMethod("connect", int.class, Class.forName("android.net.wifi.WifiManager$ActionListener"));if (connect != null) {connect.setAccessible(true);connect.invoke(manager, networkId, null);}} catch (Exception e) {e.printStackTrace();}}

推荐阅读