Android设备信息IP地址等获取

本文介绍了一种在连接WiFi或网线的情况下获取设备IP地址、MAC地址、子网掩码及默认网关的方法,并提供了具体的Java代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  • 项目中用到设备的IP,MAC等信息,要求在连接WiFi或网线情况下均可查找,经过网上查找资料后,整理总结如下,经验证可行

1、获取IP地址

    public static String getWifiIP() {
        WifiManager wifi = (WifiManager) MyApplication.getContext().getSystemService(Context.WIFI_SERVICE);
        WifiInfo info = wifi.getConnectionInfo();
//        return info.getIpAddress() + "";
        return FormatString(info.getIpAddress()).toString();

    }
     public static String getIP() {
        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()) {                      
                            return inetAddress.getHostAddress().toString();
                    }
                }
            }
        } catch (SocketException ex) {
        }
        return null;
    }

2、获取MAC

    /**
     * 使用shell命令来获取mac地址的方式
     *
     * @return
     */
    public static String getMAC() {
        String macSerial = null;
        String str = "";
        try {
            Process pp = Runtime.getRuntime().exec("cat /sys/class/net/wlan0/address ");
            InputStreamReader ir = new InputStreamReader(pp.getInputStream());
            LineNumberReader input = new LineNumberReader(ir);

            for (; null != str; ) {
                str = input.readLine();
                if (str != null) {
                    macSerial = str.trim();// 去空格
                    break;
                }
            }
        } catch (IOException ex) {
            // 赋予默认值
            ex.printStackTrace();
        }    
            return macSerial;
    }

3、获取子网掩码

 public static String getNetmask() {
        WifiManager wifi = (WifiManager) MyApplication.getContext().getSystemService(Context.WIFI_SERVICE);
        DhcpInfo d = wifi.getDhcpInfo();   
        return FormatString(d.netmask).toString();
    }

4、获取默认网关

  public static String getGateway() {
        WifiManager wifi = (WifiManager) MyApplication.getContext().getSystemService(Context.WIFI_SERVICE);
        DhcpInfo d = wifi.getDhcpInfo();     
            return FormatString(d.gateway).toString();
    }

5、其他

    /**
     * 将地址码转换成字符串类型
     * @param value
     * @return
     */
    public static String FormatString(int value) {
        String strValue = "";
        byte[] ary = intToByteArray(value);
        for (int i = ary.length - 1; i >= 0; i--) {
            strValue += (ary[i] & 0xFF);
            if (i > 0) {
                strValue += ".";
            }
        }
        return strValue;
    }

    public static byte[] intToByteArray(int value) {
        byte[] b = new byte[4];
        for (int i = 0; i < 4; i++) {
            int offset = (b.length - 1 - i) * 8;
            b[i] = (byte) ((value >>> offset) & 0xFF);
        }
        return b;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值