bonjour - How to get MACaddress using Android - jmdns -


i need mac-address of particular device.i'm using android - jmdns service scan devices , ip-address need mac-address of particular device.can mac-address while getting ip-address using android - jmdns service or other way mac-address ip-address?

you need permission in androidmanifest.xml

// androidmanifest.xml permissions <uses-permission android:name="android.permission.access_wifi_state"></uses-permission> <uses-permission android:name="android.permission.update_device_stats"></uses-permission> <uses-permission android:name="android.permission.change_wifi_state"></uses-permission> 

you can try fist solution

wifimanager wifimanager = (wifimanager) this.getsystemservice(context.wifi_service);  if(wifimanager.iswifienabled()) {     // wifi enabled. grab mac address here     wifiinfo info = wifimanager.getconnectioninfo();     string address = info.getmacaddress(); } else {     // enable wifi first     wifimanager.setwifienabled(true);      // wifi enabled. grab mac address here     wifiinfo info = wifimanager.getconnectioninfo();     string address = info.getmacaddress(); } 

or second one:

// test functions utils.getmacaddress("wlan0"); utils.getmacaddress("eth0"); utils.getipaddress(true); // ipv4 utils.getipaddress(false); // ipv6  

utils.java

import java.io.*; import java.net.*; import java.util.*;    import org.apache.http.conn.util.inetaddressutils;      public class utils {          /**          * convert byte array hex string          * @param bytes          * @return          */         public static string bytestohex(byte[] bytes) {             stringbuilder sbuf = new stringbuilder();             for(int idx=0; idx < bytes.length; idx++) {                 int intval = bytes[idx] & 0xff;                 if (intval < 0x10) sbuf.append("0");                 sbuf.append(integer.tohexstring(intval).touppercase());             }             return sbuf.tostring();         }          /**          * utf8 byte array.          * @param str          * @return  array of null if error found          */         public static byte[] getutf8bytes(string str) {             try { return str.getbytes("utf-8"); } catch (exception ex) { return null; }         }          /**          * load utf8withbom or ansi text file.          * @param filename          * @return            * @throws java.io.ioexception          */         public static string loadfileasstring(string filename) throws java.io.ioexception {             final int buflen=1024;             bufferedinputstream = new bufferedinputstream(new fileinputstream(filename), buflen);             try {                 bytearrayoutputstream baos = new bytearrayoutputstream(buflen);                 byte[] bytes = new byte[buflen];                 boolean isutf8=false;                 int read,count=0;                            while((read=is.read(bytes)) != -1) {                     if (count==0 && bytes[0]==(byte)0xef && bytes[1]==(byte)0xbb && bytes[2]==(byte)0xbf ) {                         isutf8=true;                         baos.write(bytes, 3, read-3); // drop utf8 bom marker                     } else {                         baos.write(bytes, 0, read);                     }                     count+=read;                 }                 return isutf8 ? new string(baos.tobytearray(), "utf-8") : new string(baos.tobytearray());             } {                 try{ is.close(); } catch(exception ex){}              }         }          /**          * returns mac address of given interface name.          * @param interfacename eth0, wlan0 or null=use first interface           * @return  mac address or empty string          */         public static string getmacaddress(string interfacename) {             try {                 list<networkinterface> interfaces = collections.list(networkinterface.getnetworkinterfaces());                 (networkinterface intf : interfaces) {                     if (interfacename != null) {                         if (!intf.getname().equalsignorecase(interfacename)) continue;                     }                     byte[] mac = intf.gethardwareaddress();                     if (mac==null) return "";                     stringbuilder buf = new stringbuilder();                     (int idx=0; idx<mac.length; idx++)                         buf.append(string.format("%02x:", mac[idx]));                            if (buf.length()>0) buf.deletecharat(buf.length()-1);                     return buf.tostring();                 }             } catch (exception ex) { } // eat exceptions             return "";             /*try {                 // linux hack                 return loadfileasstring("/sys/class/net/" +interfacename + "/address").touppercase().trim();             } catch (ioexception ex) {                 return null;             }*/         }          /**          * ip address first non-localhost interface          * @param ipv4  true=return ipv4, false=return ipv6          * @return  address or empty string          */         public static string getipaddress(boolean useipv4) {             try {                 list<networkinterface> interfaces = collections.list(networkinterface.getnetworkinterfaces());                 (networkinterface intf : interfaces) {                     list<inetaddress> addrs = collections.list(intf.getinetaddresses());                     (inetaddress addr : addrs) {                         if (!addr.isloopbackaddress()) {                             string saddr = addr.gethostaddress().touppercase();                             boolean isipv4 = inetaddressutils.isipv4address(saddr);                              if (useipv4) {                                 if (isipv4)                                      return saddr;                             } else {                                 if (!isipv4) {                                     int delim = saddr.indexof('%'); // drop ip6 port suffix                                     return delim<0 ? saddr : saddr.substring(0, delim);                                 }                             }                         }                     }                 }             } catch (exception ex) { } // eat exceptions             return "";         }      } 

possibly duplicate:

how find mac address of android device programmatically

how ip address of device code?


Comments

Popular posts from this blog

yii2 - Yii 2 Running a Cron in the basic template -

asp.net - 'System.Web.HttpContext' does not contain a definition for 'GetOwinContext' Mystery -

mercurial graft feature, can it copy? -