SoFunction
Updated on 2025-04-09

Implementation method of Android programming to obtain device MAC address

This article describes the implementation method of Android programming to obtain the MAC address of the device. Share it for your reference, as follows:

/**
 * Get the device's mac address
 *
 * @param ac
 * @param callback
 * This method will be called back after successfully obtaining the mac address
 */
public static void getMacAddress(final Activity ac, final SimpleCallback callback) {
    final WifiManager wm = (WifiManager) ac .getSystemService(Service.WIFI_SERVICE);
    // If you turn on WIFI after booting this time, you can directly obtain the Mac information.  Return the data immediately.    WifiInfo info = ();
    if (info != null && () != null) {
      if (callback != null) {
        (());
      }
      return;
    }
    // Try to open WIFI and get the mac address    if (!()) {
      (true);
    }
    new Thread(new Runnable() {
      @Override
      public void run() {
        int tryCount = 0;
        final int MAX_COUNT = 10;
        while (tryCount < MAX_COUNT) {
          final WifiInfo info = ();
          if (info != null && () != null) {
            if (callback != null) {
              (new Runnable() {
                @Override
                public void run() {
                  (());
                }
              });
            }
            return;
          }
          (300);
          tryCount++;
        }
        // The mac address was not obtained        if (callback != null) {
          (null);
        }
      }
    }).start();
}

SimpleCallback is a simple callback interface:

public interface SimpleCallback {
  void onComplete(String result);
}

For more information about Android related content, please check out the topic of this site:Android programming activity operation skills summary》、《Android resource operation skills summary》、《Android development introduction and advanced tutorial》、《Android View View Tips Summary》、《Summary of Android's SQLite database skills》、《Android database operation skills summary》、《A summary of SD card operation methods for Android programming and development"and"Android control usage summary

I hope this article will be helpful to everyone's Android programming design.