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


// 获取当前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();}}保存网络// 保存网络.public static void saveNetworkByConfig(WifiManager manager, WifiConfiguration config) {if (manager == null) {return;}try {Method save = manager.getClass().getDeclaredMethod("save", WifiConfiguration.class, Class.forName("android.net.wifi.WifiManager$ActionListener"));if (save != null) {save.setAccessible(true);save.invoke(manager, config, null);}} catch (Exception e) {e.printStackTrace();}}添加网络// 添加网络.public static int addNetwork(WifiManager manager, WifiConfiguration config) {if (manager != null) {manager.addNetwork(config);}}忘记网络// 忘记网络.public static void forgetNetwork(WifiManager manager, int networkId) {if (manager == null) {return;}try {Method forget = manager.getClass().getDeclaredMethod("forget", int.class, Class.forName("android.net.wifi.WifiManager$ActionListener"));if (forget != null) {forget.setAccessible(true);forget.invoke(manager, networkId, null);}} catch (Exception e) {e.printStackTrace();}}禁用网络// 禁用网络.public static void disableNetwork(WifiManager manager, int netId) {if (manager == null) {return;}try {Method disable = manager.getClass().getDeclaredMethod("disable", int.class, Class.forName("android.net.wifi.WifiManager$ActionListener"));if (disable != null) {disable.setAccessible(true);disable.invoke(manager, networkId, null);}} catch (Exception e) {e.printStackTrace();}}断开连接// 断开连接.public static boolean disconnectNetwork(WifiManager manager) {return manager != null && manager.disconnect();}短暂禁用网络// 禁用短暂网络.public static void disableEphemeralNetwork(WifiManager manager, String SSID) {if (manager == null || TextUtils.isEmpty(SSID))return;try {Method disableEphemeralNetwork = manager.getClass().getDeclaredMethod("disableEphemeralNetwork", String.class);if (disableEphemeralNetwork != null) {disableEphemeralNetwork.setAccessible(true);disableEphemeralNetwork.invoke(manager, SSID);}} catch (Exception e) {e.printStackTrace();}}

经验总结扩展阅读