Android設備信息IP地址等獲取

  • 項目中用到設備的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;
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章