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]