Android: a reliable method for obtaining the mac address of a device

Source: Internet
Author: User

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);}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.