android 移动数据流量打开导致获取wifi热点IP错误
来源:互联网 发布:别墅网络布线图 编辑:程序博客网 时间:2024/04/29 05:00
都知道获取本地IP的方法如下:
private String getLocalIpAddress() { try { for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) { NetworkInterface intf = en.nextElement(); for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) { InetAddress inetAddress = enumIpAddr.nextElement(); if (!inetAddress.isLoopbackAddress() && !inetAddress.isLinkLocalAddress()) { DebugUtils.debug(TAG, "getLocalIpAddress(): " + inetAddress.getHostAddress().toString()); return inetAddress.getHostAddress().toString(); } } } } catch (SocketException ex) { System.out.println("WifiPreference IpAddress" + ex.toString()); getipFlag = false; } return null; }
以上方法在获取热点IP的时候会出现问题,我们在创建热点之前打开移动流量,这时获取的IP就不是热点的Ip而是移动数据的IP,
这种情况下那么就必须做IP的过滤处理:
1:获取热点Ip的名称
public static String getApName(Context context) { try { ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); Method method = connectivityManager.getClass().getMethod("getTetheredIfaces"); String[] names = (String[]) method.invoke(connectivityManager); DebugUtils.debug(TAG, "names.length: " + names.length); if(names.length > 0){ DebugUtils.debug(TAG, "names[0]: " + names[0]); return names[0]; } } catch (Exception e) { e.printStackTrace(); } return ""; }
2:通过热点名称过滤IP
public static String getNetworkIpAddress(String name) { try { Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); while (interfaces.hasMoreElements()) { NetworkInterface networkInterface = (NetworkInterface) interfaces.nextElement(); Enumeration<InetAddress> enumeration = networkInterface.getInetAddresses(); while (enumeration.hasMoreElements()) { InetAddress inetAddress = (InetAddress) enumeration.nextElement(); DebugUtils.debug(TAG, "getLocalIp networkInterface.getDisplayName():" +networkInterface.getDisplayName()); if (!inetAddress.isLoopbackAddress() && inetAddress instanceof Inet4Address && TextUtils.equals(name, networkInterface.getDisplayName())){ return inetAddress.getHostAddress().toString(); } } } } catch (Exception e) { e.printStackTrace(); } return ""; }
这种方式得到的IP就是我们创建的热点的Ip了
附带:获取WLAN信息,创建的热点的名称密码以及加密方式
public static void getWifiSSIDAndKey(Context context) { WifiManager mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); Method method = null; try { method = mWifiManager.getClass().getMethod("getWifiApConfiguration"); } catch (Exception e) { e.printStackTrace(); } try { WifiConfiguration apConfig = (WifiConfiguration) method.invoke(mWifiManager); WifiBean wifiInfo = new WifiBean(); wifiInfo.SSID = apConfig.SSID; wifiInfo.password = apConfig.preSharedKey; wifiInfo.security = WifiSecurity.getSecurity(apConfig); Config.wifiInfo = wifiInfo; DebugUtils.printInfo("", "wifiInfo.SSID: " + wifiInfo.SSID ); DebugUtils.printInfo("", "wifiInfo.password: " + wifiInfo.password ); DebugUtils.printInfo("", "wifiInfo.加密方式: " + WifiSecurity.getSecurity(apConfig)); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } }
WifiSecurity 类:
public class WifiSecurity { public static final int SECURITY_NONE = 0; public static final int SECURITY_WEP = 1; public static final int SECURITY_PSK = 2; public static final int SECURITY_EAP = 3; public static final int WPA2_PSK = 4; public static int getSecurity(WifiConfiguration config) { if (config.allowedKeyManagement.get(KeyMgmt.WPA_PSK)) { return SECURITY_PSK; } if (config.allowedKeyManagement.get(KeyMgmt.WPA_EAP) || config.allowedKeyManagement.get(KeyMgmt.IEEE8021X)) { return SECURITY_EAP; } if (config.allowedKeyManagement.get(4)) { //这里只能固话,KeyMgmt方法有WPA2_PSK,但是不提供 return WPA2_PSK; } return (config.wepKeys[0] != null) ? SECURITY_WEP : SECURITY_NONE; } }
附带:已经测试过,自动连接创建的wifi热点类,包含加密方式WPA2_PSK和WPA_PSK
demo地址:http://download.csdn.net/detail/zs20082012/9742740
1 0
- android 移动数据流量打开导致获取wifi热点IP错误
- Android中用对话框打开wifi和数据流量。
- android获取周围WIFI热点
- android SDIO error导致wifi无法打开或者连接热点异常的问题
- Android网络相关(WiFi的开关,WiFi热点的开关,获取手机IP地址等)
- 打开android手机的wifi热点
- android 代码设置、打开wifi热点及热点的连接
- android 代码设置、打开wifi热点及热点的连接
- android 代码设置、打开wifi热点及热点的连接
- android 代码设置、打开wifi热点及热点的连接
- android 代码设置、打开wifi热点及热点的连接
- android 代码设置、打开wifi热点及热点的连接
- android 代码设置、打开wifi热点及热点的连接
- android 代码设置、打开wifi热点及热点的连接
- android 代码设置、打开wifi热点及热点的连接
- android 代码设置、打开wifi热点及热点的连接
- android 代码设置、打开wifi热点及热点的连接
- android 代码设置、打开wifi热点及热点的连接
- 斯坦福机器学习公开课随笔5
- OSGI企业应用开发(十三)OSGI Web应用开发(二)
- 设计模式的原则
- GreenDao 3.2.0 的基本使用
- 僵尸进程孤儿进程知识点
- android 移动数据流量打开导致获取wifi热点IP错误
- 深入浅出大数据–课本学习 two
- 盒子模型和div布局
- 分类决策树原理及实现(二)
- Python日常-字典排序
- Java Virtual Machine 1
- Tarjan求解有向图强连通分量的线性时间的算法
- PAT甲级1044
- 自定义ListView和SwipeRefreshLayout实现上拉加载和下拉刷新