SoFunction
Updated on 2025-05-09

Android and iOS devices MAC address generation principle and Java implementation detailed explanation

introduction

In wireless network communication, the MAC (Media Access Control) address is the device's unique network identifier, which is usually represented as a 12-bit hexadecimal number (such as 00:1A:2B:3C:4D:5E). It not only plays a key role in network communication, but is also widely used in device filtering, access control and other scenarios. However, there are significant differences in the generation and management of MAC addresses between different operating systems (such as Android and iOS). This article will discuss in depth:

  • Basic structure of MAC address
  • MAC address characteristics for Android and iOS
  • Java code implements the generation of MAC addresses on different platforms
  • Things to note in practical applications

1. MAC address basics

1.1 Composition of MAC address

The MAC address consists of 48 bits (6 bytes), usually represented as a 12-bit hexadecimal number, separated by a colon or hyphen, for example:

00:1A:2B:3C:4D:5E

First 3 bytes (OUI): Assigned by IEEE to the manufacturer (such as 00:1A:2B may represent a certain brand)

The last 3 bytes: allocated by the manufacturer to ensure device uniqueness

1.2 Classification of MAC addresses

type illustrate Example
Unicast Target is a single device 02:1A:2B:3C:4D:5E
Multicast Targeted for multiple devices 03:1A:2B:3C:4D:5E
Broadcast Target for all devices FF:FF:FF:FF:FF:FF

In addition, MAC addresses are divided into:

  • Global Management (U/L=0): Allocated by IEEE, unique to the world
  • Local management (U/L=1): User-defined, only the only one in the LAN needs to be guaranteed to be unique.

2. Differences in MAC address between Android and iOS

2.1 Android's MAC address

Traditional method: Use the fixed MAC address when the device leaves the factory (OUI is registered by the manufacturer).

Randomization strategy (Android 10+):

  • Use random MAC when scanning Wi-Fi (enhanced privacy)
  • Real MAC may be restored when connecting to the network

Features:

  • Local management (02:xx:xx:xx:xx:xx:xx)
  • Unicast address (lowest bit is 0)

2.2 iOS MAC address

iOS 8+ introduces random MAC:

  • Random MAC is used by default when scanning Wi-Fi (preventing tracking)
  • Switch back to the real MAC when connecting to the network

Features:

  • The format is the same as the standard MAC, but the behavior is randomized
  • Usually local management (02:xx:xx:xx:xx:xx:xx:xx)

3. Java implements MAC address generation

3.1 Basic MAC generation method

import ;

public class BasicMacGenerator {
    public static String generateMac() {
        Random rand = new Random();
        byte[] macBytes = new byte[6];
        (macBytes);
        
        // Set as local management + unicast (02:XX:XX:XX:XX:XX:XX)        macBytes[0] = (byte)((macBytes[0] & 0xFE) | 0x02);
        
        // Format as XX:XX:XX:XX:XX:XX:XX        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < ; i++) {
            (("%02X", macBytes[i]));
            if (i <  - 1) (":");
        }
        return ();
    }

    public static void main(String[] args) {
        ("Random MAC: " + generateMac());
    }
}

Output example:

Random MAC: 02:3A:4B:5C:6D:7E

3.2 MAC generation that distinguishes between Android and iOS

public class PlatformMacGenerator {
    private static final Random random = new Random();

    // Android: Local management + unicast (02:XX:XX:XX:XX:XX)    public static String generateAndroidMac() {
        byte[] mac = new byte[6];
        (mac);
        mac[0] = (byte)((mac[0] & 0xFE) | 0x02);
        return formatMac(mac);
    }

    // iOS: The format is the same, but the behavior is randomized    public static String generateIosMac() {
        byte[] mac = new byte[6];
        (mac);
        mac[0] = (byte)((mac[0] & 0xFE) | 0x02);
        return formatMac(mac);
    }

    private static String formatMac(byte[] mac) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < ; i++) {
            (("%02X", mac[i]));
            if (i <  - 1) (":");
        }
        return ();
    }

    public static void main(String[] args) {
        ("Android MAC: " + generateAndroidMac());
        ("iOS MAC: " + generateIosMac());
    }
}

Output example:

Android MAC: 02:1A:2B:3C:4D:5E
iOS MAC: 02:A1:B2:C3:D4:E5

4. Things to note in practical application

Privacy protection:

  • Android 10+ and iOS 8+ use random MAC to scan Wi-Fi by default
  • Real MAC may be exposed only when connected to the network

Unique risk:

  • Randomly generated MACs may conflict (very low probability)
  • The production environment should be checked in conjunction with database

Network policy restrictions:

  • Some enterprise networks may prohibit random MACs
  • Need a whitelist mechanism

5. Conclusion

platform MAC address characteristics Generate method Privacy Policy
Android Local management + unicast 02:XX:XX:XX:XX:XX Randomization during scanning
iOS The same format but random behavior 02:XX:XX:XX:XX:XX Default randomization

This article provides a complete Java implementation and analyzes the differences in MAC address behaviors of different platforms. Developers can adjust the generation logic according to their needs, for example:

  • Simulate real equipment: combine OUI library to generate manufacturer MAC
  • Enhanced uniqueness: Introduce timestamps or UUIDs

This is the article about the principle of MAC address generation of Android and iOS devices and the detailed explanation of Java implementation. For more related content on Java generation of MAC address, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!