Android: A reliable way to get the MAC address of a device

Source: Internet
Author: User

/** * Gets the MAC address of the device * * @param AC * @param callback * After successfully acquiring the MAC address, this method is recalled */public static void Getmacaddress (final Activity AC, Final Simplecallback callback) {final Wifimanager wm = (Wifimanager) AC. Getsystemservice (Service.wifi_servi (CE)//If WiFi is turned on after the boot, the MAC information can be obtained directly. return data immediately. Wifiinfo info = wm.getconnectioninfo (); if (info! = null && info.getmacaddress () = null) {if (callback! = null) {C Allback.oncomplete (Info.getmacaddress ());} return;} Try to open WiFi and get 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 (Callba CK = null) {Ac.runonuithread (new Runnable () {@Overridepublic void run () {Callback.oncomplete (info.getmacaddress ());}});} return;} Systemclock.sleep (+); trycount++;} Did not get to MAC address if (callback! = null) {callback.oncomplete (null);}}}). Start ();} 

The Simplecallback is a simple callback interface:

public interface Simplecallback {void OnComplete (String result);

Android: A reliable way to get the MAC address of a device

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.