"Android" Get MAC Address "2"
Previously wrote "Android" Get MAC Address "1" Some not enough detail, now paste some other code, for reference only.
(1) Call the Android Api:networkinterface. Gethardwareaddress ()
The level of the API is 9, only Android 2.3 or more of the interface
(2) method of calling Java: Nbtstat/arp
Generally Android does not support these two commands. This method has not been tried.
(3) Call Android Api:wifimanager
Permissions:
<uses-permission android:name= "Android.permission.ACCESS_WIFI_STATE" ></uses-permission>
Code:
/** * Get MAC addresses via Wifimanager * @attention Wifi * @return mac address */private static String Getmacfromwifi (context context) { Wifimanager Wifimanager = (wifimanager) context.getsystemservice (context.wifi_service); if (!wifimanager.iswifienabled ()) { wifimanager.setwifienabled (true); } Wifiinfo wifiinfo = Wifimanager.getconnectioninfo (); String Mresult = wifiinfo.getmacaddress (); LOG.I (Tag_network, "Mac address (WiFi):" +mresult); return mresult;}
This is the need for the device to open the WiFi connection, get to the network card MAC address
Another, paste, to determine whether the current WiFi connection mode:
Determine whether you are currently using WiFi connection private static Boolean iswificonnected (context context) { Connectivitymanager cm; CM = (Connectivitymanager) context.getsystemservice (context.connectivity_service); Boolean result = Cm.getnetworkinfo (Connectivitymanager.type_wifi). GetState () = = NetworkInfo.State.CONNECTED? True:false; LOG.I (Tag_network, "iswificonnected:" +result); return result; }
(4) Call the Linux BusyBox
/** * Callcmd ("BusyBox ifconfig", "HWaddr") get MAC Address * @attention requires device with BusyBox tool * @return Mac address */private static St Ring Getmacfromcallcmd () {String result = ""; result = Callcmd ("BusyBox ifconfig", "HWaddr"); if (result = = NULL | | result.length () <= 0) {log.i (tag_network, "callcmd returns null or empty"); return null; }//parse the row data//For example: eth0 Link encap:ethernet HWaddr 00:16:e8:3e:df:67 if (Result.length () >0 && result.contains ("HWaddr") ==true) {String Mac = result.substring (Result.indexof ("HWaddr") +6, result . Length ()-1); LOG.I (Tag_network, "Mac:" +mac+ "Mac.length:" +mac.length ()); if (Mac.length () >1) {Mac = Mac.replaceall ("", ""); result = ""; string[] tmp = Mac.split (":"); for (int i = 0;i<tmp.length;++i) {result +=tmp[i]; }} log.i (tag_network,result+ "result.length:" +result.length ()); } LOG.I (Tag_network, "Mac address (Callcmd):" +result); return result;}
Other related functions:
public static string Callcmd (string cmd,string filter) { string result = ""; String line = ""; try { Process proc = runtime.getruntime (). exec (cmd); InputStreamReader is = new InputStreamReader (Proc.getinputstream ()); BufferedReader br = new BufferedReader (is); Executes command cmd, taking only this row of the result that contains the filter (line = Br.readline ()) = null && line.contains (filter) = = False) { result + = line; LOG.I ("Test", "line:" +line); } result = line; LOG.I ("Test", "Result:" +result); } catch (Exception e) { e.printstacktrace (); } return result;
This requires the device to support the BusyBox tool. Now that some devices do not have the tool installed, the use of this method will be an error.
(5) Querying the file "/proc/net/arp" that recorded the MAC address
The
needs to have this file, and the corresponding content is logged
/** * Get the Mac Address from the FILE/PROC/NET/ARP * @param context * @attention the File/proc/net/ar P Need exit * @return MAC Address */private static String Getmacfromfile (context context) {string MIP = Config . getipaddress (context); if (MIP = = NULL | | mip.length () <=0) return null; list<string> Mresult = Readfilelines ("/proc/net/arp"); LOG.D (Tag_network, "=======/proc/net/arp ========="); For (Int. I =0;i<mresult.size (); ++i) log.d ("line", Mresult.get (i)); LOG.D (Tag_network, "==========================="); if (Mresult!=null && mresult.size () >1) {for (int J =1;j<mresult.size (); ++j) {list<st ring> mlist = new arraylist<string> (); string[] Mtype = Mresult.get (j). Split (""); for (int i =0;i<mtype.length;++i) {if (Mtype[i]!=null && mtype[i].length () >0) ML Ist.add (Mtype[i]); } if (Mlist!=null && mlist.size () >4 && mlist.get (0). Equalsignorecase (MIP)) { String result= ""; string[] tmp = Mlist.get (3). Split (":"); for (int i = 0;i<tmp.length;++i) {result +=tmp[i]; } result = Result.touppercase (); LOG.I (Tag_network, "Mac address (file):" +result); return result; }}} return null; }
/** * Read files in behavioral units, often used to read line-oriented format files * /private static list<string> Readfilelines (String fileName) { File File = new file (fileName); BufferedReader reader = null; String tempstring = ""; list<string> Mresult = new arraylist<string> (); try { log.i ("result", "read the contents of the file in the behavior unit, read a whole line at a time:"); reader = new BufferedReader (new FileReader (file)); while ((tempstring = Reader.readline ())!=null) { mresult.add (tempstring); } Reader.close (); } catch (IOException e) { e.printstacktrace (); } finally { if (reader! = null) { try { Reader.close (); } catch (IOException E1) { }}} return mresult; }
The file "/proc/net/arp" that recorded the MAC address is roughly as follows:
IP address HW type flags HW address mask Device
10.63.253.193 0x1 0x2 00:11:92:06:85:3f * eth0
10.63.253.194 0x1 0x2 00:11:92:06:85:3a * eth1
10.63.253.195 0x1 0x2 00:11:92:06:85:3b * eth2
done!! Sleep ~
(GO) "Android" Get MAC Address "2"