// 获取当前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();}}
经验总结扩展阅读
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- 随身wifi怎么用的要自己买卡么(随身wifi靠谱吗)
- 随身WiFi是什么意思,怎么用的(随身wifi有什么意义)
- 随身WiFi怎么用(随身wifi怎么交费)
- 随身WiFI是怎么用的(随身wifi激活的步骤)
- 移动随身wifi怎么用(移动wifi多少钱一个月)
- 随身无线网怎么用(随身wifi是不是智商税)
- android安卓手机:[4]如何怎么打开.apk文件
- 29 《吐血整理》高级系列教程-吃透Fiddler抓包教程-Fiddler如何抓取Android7.0以上的Https包-终篇
- 通过netty把百度地图API获取的地理位置从Android端发送到Java服务器端
- Android自动化测试工具调研