android 移动数据流量打开导致获取wifi热点IP错误

都知道获取本地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

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值