Android: a reliable method for obtaining the mac address of a device
/*** Get the mac address of the device ** @ param ac * @ param callback * this method is called back after the mac address is obtained successfully */public static void getMacAddress (final Activity ac, final SimpleCallback callback) {final WifiManager wm = (WifiManager) ac. getSystemService (Service. WIFI_SERVICE); // If WIFI is enabled after the current Boot, the mac information can be obtained directly. Return data immediately. WifiInfo info = wm. getConnectionInfo (); if (info! = Null & info. getMacAddress ()! = Null) {if (callback! = Null) {callback. onComplete (info. getMacAddress () ;}return ;}// try to enable WIFI and obtain the mac address if (! Wm. isWifiEnabled () {wm. setWifiEnabled (true);} new Thread (new Runnable () {@ Overridepublic void run () {int tryCount = 0; final int MAX_COUNT = 10; while (tryCount <MAX_COUNT) {final WifiInfo info = wm. getConnectionInfo (); if (info! = Null & info. getMacAddress ()! = Null) {if (callback! = Null) {ac. runOnUiThread (new Runnable () {@ Overridepublic void run () {callback. onComplete (info. getMacAddress () ;}}) ;}return ;} SystemClock. sleep (300); tryCount ++;} // if (callback! = Null) {callback. onComplete (null) ;}}). start ();}
SimpleCallback is a simple callback interface:
public interface SimpleCallback {void onComplete(String result);}