Android gets Ethernet, WiFi IP and MAC addresses

Source: Internet
Author: User


/** * Get local IP * @return */private string getlocalipaddress () {try {String IPv4 = null; list<networkinterface> nilist = collections.list (Networkinterface.getnetworkinterfaces ()); for ( NetworkInterface ni:nilist) {list<inetaddress> ialist = Collections.list (ni.getinetaddresses ()); for ( InetAddress address:ialist) {IPv4 = address.gethostaddress (); if (!address.isloopbackaddress () && Inetaddressutils.isipv4address (IPv4)) {return IPv4;}}} catch (SocketException ex) {}return "0.0.0.0";}


Get MAC address via IP

/** *  get MAC address  *  @return  */@SuppressWarnings ("finally") via local IP private string  GETLOCALMACADDRESSFROMIP ()  {String mac_s =  "";try {byte[] mac; String ip = getlocalipaddress ();if  (! Inetaddressutils.isipv4address (IP))  {return mac_s;} Inetaddress ipaddress = inetaddress.getbyname (IP);if  (ipaddress == null)  { return mac_s;} Networkinterface ne = networkinterface.getbyinetaddress (ipAddress);mac =  Ne.gethardwareaddress ();if  (mac.length > 0)  {mac_s = byte2mac (MAC);}}  catch  (exception e)  {e.printstacktrace ();} finally{return mac_s;}} Private string byte2mac (byte[] b)  {stringbuffer hs = new stringbuffer ( B.length); string stmp =  "";int len = b.length;for  (int n = 0; n  < len; n++) &NBSp {stmp = integer.tohexstring (B[n] & 0xff);if  (Stmp.length ()  == 1)  { Hs = hs.append ("0"). Append (stmp);}  else {hs = hs.append (STMP);}} Stringbuffer str = new stringbuffer (HS);for  (int i = 0; i  < str.length ();  i++)  {if  (i % 3 == 0)  {str.insert (i,  ': ') ;}} Return str.tostring (). substring (1);}

Because it is the MAC address obtained through IP, so when the WiFi connection is the IP gets to the WiFi Mac, if it is Ethernet connection, then get the MAC address of Ethernet


The following approach is to get Ethernet's Mac directly

/** *  get Ethernet's MAC address  *  @return  */private string getmacaddress ()  {try  {return loadfileasstring ("/sys/class/net/eth0/address"). toUpperCase (locale.english). substring (0, &NBSP;17);}  catch  (Ioexception e)  {return null;}} Private string loadfileasstring (String filepath)  throws java.io.IOException{           StringBuffer fileData = new  StringBuffer (          bufferedreader reader);  = new bufferedreader (New filereader (filePath));            char[] buf = new char[1024]; int numRead=0;            while ((Numread=reader.read (BUF))  != -1) {                string readdata = string.valueof (Buf, 0, numread);                filedata.append (ReadData);            }            reader.close ();            return  filedata.tostring ();       }

There's also an easier way to get Ethernet's Mac

/** * Get Ethernet MAC Address * @return */private String getmacaddress () {Ethernetmanager Ethmanager = (ethernetmanager) mainacti Vity.this.getSystemService ("Ethernet"); return ethmanager.getmacaddr () ==null? ": Ethmanager.getmacaddr ();}


Get the MAC address of WiFi

/** * Get WiFi Mac * @return */private String Getwifimac () {Wifimanager WiFi = (wifimanager) getsystemservice (context.wifi_se Rvice); Wifiinfo info = wifi.getconnectioninfo (); return info.getmacaddress () ==null? ": Info.getmacaddress ();}


Android gets Ethernet, WiFi IP and MAC addresses

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.