wlan mac address network interface IPv6 IPv4

Network interface acquisition:

Enumeration<NetworkInterface> interfaces =  NetworkInterface.getNetworkInterfaces();
                while(interfaces.hasMoreElements()){
                    String name = interfaces.nextElement().getName();
                    NetworkInterface inter = null;
                    StringBuffer buffer = new StringBuffer();
                    if (name != null) {
                        Log.e(TAG, "interface:  " + name);
                        inter = NetworkInterface.getByName(name);
                    }
                    if(inter != null){
                        byte[] addr = inter.getHardwareAddress();
                        if(addr != null){
                            for (byte b : addr) {
                                buffer.append(String.format("%02X:", b));
                            }
                            if (buffer.length() > 0) {
                                buffer.deleteCharAt(buffer.length() - 1);
                            }
                            Log.e(TAG, name + ":  " +buffer.toString());
                        }

                    }

                }

The following log may be printed:

interface: eth0
eth0:2E:54:E7:A7:D5:83
interface: sit0
interface: lo
interface: wlan0
wlan0: B0:F1:EC:0B:85:26

That is, there are four interfaces named eth0, sit0, lo, and wlan0. Only two of the interfaces have mac addresses.

getHardwareAddress():

/**
     * Returns the hardware address of the interface, if it has one, or null otherwise.
     *
     * @throws SocketException if an I/O error occurs.
     * @since 1.6
     */
    public byte[] getHardwareAddress() throws SocketException {
        try {
            // Parse colon-separated bytes with a trailing newline: "aa:bb:cc:dd:ee:ff\n".
            String s = IoUtils.readFileAsString("/sys/class/net/" + name + "/address");
            byte[] result = new byte[s.length()/3];
            for (int i = 0; i < result.length; ++i) {
                result[i] = (byte) Integer.parseInt(s.substring(3*i, 3*i + 2), 16);
            }
            // We only want to return non-zero hardware addresses.
            for (int i = 0; i < result.length; ++i) {
                if (result[i] != 0) {
                    return result;
                }
            }
            return null;
        } catch (Exception ex) {
            throw rethrowAsSocketException(ex);
        }
    }
Look at the code is to read "/sys/class/net/" + name + "/address"

In addition to the mac address, the network interface (NetworkInterface) also has an important attribute, which is IP, which is only available after networking. Use the following method to read IPv6

private static String[] readIfInet6Lines() throws SocketException {
        try {
            return IoUtils.readFileAsString("/proc/net/if_inet6").split("\n");
        } catch (IOException ioe) {
            throw rethrowAsSocketException (ioe);
        }
    }

Just go to "/proc/net/if_inet6" to read, the content is as follows:

000000000000000000000000000000001 01 80 10 80 lo

fe80000000000000020822fffe6ce3fb 07 40 20 80    wlan0


And when reading IPv4 use NetworkInterface#collectIpv4Address()

Reading IPv6 stones uses NetworkInterface#collectIpv6Addresses(), which calls NetworkInterface#readIfInet6Lines()

Using the tool to write the Mac address is the same as writing the wlan address (or wifi address). The tool to write the Mac address refers to writing the address of the Ethernet interface, not the mac address of the wireless LAN interface. The general name starts with eth is the Ethernet interface (Ethernet), the wireless LAN interface name generally starts with wlan.

Interface names can be defined using the following properties:

wifi.interface=wlan0


After writing the wifi address, it generally needs to restart or restore the factory settings to take effect. It is copied from a certain location to "/sys/class/net/" + name + "/address" when recovery mode is started. Sometimes after the tool is written, it can be read, but the code cannot be read, because there is no "/sys/class/net/" + name + "/address" at all, that is to say, it is copied to "/sys" at startup An error occurred during /class/net/" + name + "/address.


Because mobile devices have various IDs, it is easier to mix them up. Let's talk about the SN number and IMEI number:

The SN code is the abbreviation of Serial Number, sometimes called SerialNo, which is the product serial number. The product serial number is a concept introduced to verify the "legal identity of the product". It is used to protect the legitimate rights and interests of users and enjoy legal services. ; A set of genuine products only corresponds to a set of product serial numbers.

IMEI is TAC + FAC + SNR + SP. IMEI (International Mobile Equipment Identity) is the abbreviation of International Mobile Equipment Identity, the International Mobile Equipment Identification Code, which is an "electronic serial number" composed of 15 digits. only. Each mobile phone will be given a globally unique set of numbers after assembly, and this number will be recorded by the manufacturer from production to delivery.


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326089794&siteId=291194637