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);}
 

更多相关文章

  1. android 获取局域网内其他手机的ip
  2. Android(安卓)获取无线蓝牙MAC信息代码
  3. Android连接网络
  4. Android工具类
  5. 初学Android,使用自定义的XML资源(十八)
  6. Android通过原生方式获取经纬度与城市信息的方法
  7. Android(安卓)6.0权限处理问题
  8. 怎样用 C/C++ 开发一个原生程序运行在 Android(安卓)下而不用 AD
  9. 导入的ANDROID 项目没有ANDROID的JAR包

随机推荐

  1. 第一个Android简单程序-拼板
  2. get installed apps info on android and
  3. Android提示框
  4. 在四大主件以外的类中 怎么使用Context
  5. Android(安卓)PackageManager 卸载包的方
  6. 自学android 坑2
  7. Android在子线程中更新UI(二)
  8. 源码剖析: Notification的发送
  9. Android(安卓)WebView的简单使用
  10. android PreferenceActivity使用