Android 获取物理MAC地址和序列号

获取物理MAC地址:

  public String getMacAddress() {
        List interfaces = null;
        try {
            interfaces = Collections.list(NetworkInterface.getNetworkInterfaces());
            for (NetworkInterface networkInterface : interfaces) {
                if (networkInterface != null && TextUtils.isEmpty(networkInterface.getName()) == false) {
                    if ("wlan0".equalsIgnoreCase(networkInterface.getName())) {
                        byte[] macBytes = networkInterface.getHardwareAddress();
                        if (macBytes != null && macBytes.length > 0) {
                            StringBuilder str = new StringBuilder();
                            for (byte b : macBytes) {
                                str.append(String.format("%02X:", b));
                            }
                            if (str.length() > 0) {
                                str.deleteCharAt(str.length() - 1);
                            }
                            return str.toString();
                        }
                    }
                }
            }
        } catch (SocketException e) {
            e.printStackTrace();
        }
        return "unknown";
    }

获取序列号:

   /**
     * 第一种方式
     * @return
     */
    public static String getDeviceSN(){
        String serial = null;
        try {
            Class c =Class.forName("android.os.SystemProperties");
            Method get =c.getMethod("get", String.class);
            serial = (String)get.invoke(c, "ro.serialno");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return serial;
    }

    /**
     * 第二种方式
     * @return
     */
    public static String getDeviceSN1(){
        String serialNumber = android.os.Build.SERIAL;
        return serialNumber;
    }

 

你可能感兴趣的:(Android 获取物理MAC地址和序列号)