How to get the Android mobile terminal MAC address using Java code

Source: Internet
Author: User

It's almost time to get off the clock. How to obtain Android mobile Terminal MAC address using Java code:

Getting a MAC address from a device with a WiFi connection is the most desirable and the code is as follows:

/** * Device enabled WiFi connection, get MAC address via Wifimanager *  * @author Gao Yingjie */public static String Getmacfromwifi (context context) { Connectivitymanager Connectivitymanager = (connectivitymanager) context.getsystemservice (Context.CONNECTIVITY_ SERVICE); state wifistate = Connectivitymanager.getnetworkinfo (Connectivitymanager.type_wifi). GetState (); if (wifistate = = NetworkInfo.State.CONNECTED) {//Determine whether WiFi connection is currently used        wifimanager Wifimanager = (wifimanager) context.getsystemservice (Context.wifi_service);           if (!wifimanager.iswifienabled ()) {//If the current WiFi is not available          wifimanager.setwifienabled (true);        }        Wifiinfo wifiinfo = Wifimanager.getconnectioninfo ();        return wifiinfo.getmacaddress ();}        return null;}
In addition to this method, there are two other methods available online:

1. Get the MAC address by calling the Linux busybox command:

/** * Get MAC address by calling the Linux busybox command *  * @author Gao Yingjie */private static string Getmacfromcallcmd () {        try {        string re Adline = "";         Process process = Runtime.getruntime (). EXEC ("BusyBox ifconfig");            BufferedReader BufferedReader = new BufferedReader (New InputStreamReader (Process.getinputstream ()));            while ((ReadLine = Bufferedreader.readline ()) = null) {//executes command cmd, taking only this line that contains "HWaddr" in the result (            readline.contains (" HWaddr ")) {            return readline.substring (Readline.indexof (" HWaddr ") +6, Readline.length ()-1);}}}        catch (Exception e) {  //If an exception occurs because the device does not support the BusyBox tool.             e.printstacktrace ();         }        return null;}
Note: This method can obtain the MAC address accurately in Android pad, but it can't be obtained accurately in Android phone.

2. Obtain the MAC address by querying the file that recorded the MAC address (file path: "/proc/net/arp"):

/** * Gets the MAC address by querying the file (file path: "/proc/net/arp") that recorded the MAC address * * @author Gao Yingjie */private static String Getmacfromfile (Context C        Ontext) {String readLine = "";        BufferedReader bufferedreader = null;            try {BufferedReader = new BufferedReader (new FileReader (New File ("/proc/net/arp"));            int rollindex = 0;            while ((ReadLine = Bufferedreader.readline ())!=null) {if (Rollindex = = 1) {break;            } Rollindex = Rollindex + 1;        }} catch (IOException e) {e.printstacktrace ();                } finally {if (BufferedReader! = null) {try {bufferedreader.close ();                } catch (IOException e) {e.printstacktrace (); }}} if (ReadLine!=null && readline.length () >1) {string[] Subreadlinearray = r        Eadline.split ("");    int rollindex = 1; for (int i = 0; i < subreadlinearray.length; ++i) {if (!            Textutils.isempty (Subreadlinearray[i])) {if (Rollindex = = 4) {return subreadlinearray[i];            } Rollindex = Rollindex + 1;    }}} return null; }
Note: This method is not able to get the MAC address accurately, either on Android pad or Android phone.

"0-minute download sample "

How to get the Android mobile terminal MAC address using Java code

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.