Android获取WiFi ip相关信息
16lz
2022-01-21
private void getip(){ WifiManager wifiMgr = (WifiManager) getSystemService(Context.WIFI_SERVICE); DhcpInfo dhcpInfo = wifiMgr.getDhcpInfo(); WifiInfo wifiInfo = wifiMgr.getConnectionInfo(); if (wifiInfo != null) { Log.d(TAG,"wifi名称:"+wifiInfo.getSSID().replace("\"", ""));//获取到的wifi名称带双引号,用replace去掉 Log.d(TAG,"wifi信号强度:"+ wifiInfo.getRssi()); } if (dhcpInfo != null) { Log.d(TAG,"ip:"+intToIp(dhcpInfo.ipAddress)); Log.d(TAG,"dns:"+intToIp(dhcpInfo.dns1)); Log.d(TAG,"网关:"+intToIp(dhcpInfo.gateway));//就是路由? } Log.d(TAG,"子网掩码:"+getNetMask());//直接用dhcpInfo.netmask获取到的是0.0.0.0,貌似是api的bug}private String getNetMask() { try { Enumeration eni = NetworkInterface .getNetworkInterfaces(); while (eni.hasMoreElements()) { NetworkInterface networkCard = eni.nextElement(); List ncAddrList = networkCard .getInterfaceAddresses(); Iterator ncAddrIterator = ncAddrList.iterator(); while (ncAddrIterator.hasNext()) { InterfaceAddress networkCardAddress = ncAddrIterator.next(); InetAddress address = networkCardAddress.getAddress(); if (!address.isLoopbackAddress()) { String hostAddress = address.getHostAddress(); Log.d(TAG,"address = " + hostAddress); if (hostAddress.indexOf(":") > 0) { // case : ipv6 continue; } else { // case : ipv4 String maskAddress = calcMaskByPrefixLength(networkCardAddress.getNetworkPrefixLength()); Log.d(TAG,"subnetmask = " + maskAddress); return maskAddress; } } } } } catch (Exception e) { e.printStackTrace(); return "N/A"; } return "N/A";}private String intToIp(int paramInt) { return (paramInt & 0xFF) + "." + (0xFF & paramInt >> 8) + "." + (0xFF & paramInt >> 16) + "." + (0xFF & paramInt >> 24);}
更多相关文章
- android 获取局域网内其他手机的ip
- Android(安卓)获取无线蓝牙MAC信息代码
- Android连接网络
- Android工具类
- 初学Android,使用自定义的XML资源(十八)
- Android通过原生方式获取经纬度与城市信息的方法
- Android(安卓)6.0权限处理问题
- 怎样用 C/C++ 开发一个原生程序运行在 Android(安卓)下而不用 AD
- 导入的ANDROID 项目没有ANDROID的JAR包