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
原创粉丝点击