How to use Java code to obtain Mac addresses of Android mobile terminals and android mobile terminals

Source: Internet
Author: User

How to use Java code to obtain Mac addresses of Android mobile terminals and android mobile terminals

Now, let's take a look at how to use Java code to obtain the Mac address of the Android mobile terminal:

You can use the following code to obtain the Mac address from a Wi-Fi connection:

/*** The device activates a Wi-Fi connection and obtains the Mac address through wifiManager ** @ author */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 to use wifi to connect to WifiManager wifi Manager = (WifiManager) context. getSystemService (Context. WIFI_SERVICE); if (! WifiManager. isWifiEnabled () {// if the current wifi is unavailable wifiManager. setWifiEnabled (true);} WifiInfo wifiInfo = wifiManager. getConnectionInfo (); return wifiInfo. getMacAddress ();} return null ;}
In addition to the above method, two other methods are provided on the Internet:

1. Obtain the Mac address by calling the Linux busybox command:

/*** Obtain the Mac address by calling the Linux busybox command ** @ author Gao huanjie */private static String getMacFromCallCmd () {try {String readLine = ""; process process = runtime.getruntime(cmd.exe c ("busybox ifconfig"); BufferedReader bufferedReader = new BufferedReader (new InputStreamReader (process. getInputStream (); while (readLine = bufferedReader. readLine ())! = Null) {// Execute Command cmd. Only if (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 accurately obtain the Mac address in the Android Pad, but it cannot be accurately obtained in the Android phone.

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

/*** Query the MAC address file (file path: "/proc/net/arp ") obtain the Mac address ** @ author Gao huanjie */private static String getMacFromFile (Context context) {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 = readLine. 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: neither Android Pad nor Android phone can obtain the Mac address accurately.

0-point download example]

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.