Get your Android device WiFi MAC address "MAC address"

Source: Internet
Author: User

It should be noted that Wi-Fi status and WiFi AP status is mutually exclusive, that is, once the WiFi AP is turned on, WiFi cannot be opened.

Get WiFi MAC address of Android device, first need to turn off WiFi personal hotspot (AP) in the device, WiFi status and WiFi AP status are mutually exclusive two states. In other words: WiFi access is not allowed to open in the state of the WiFi AP.

The Android system gets the MAC address in a variety of ways to traverse.

Method One: Use NetworkInterface

Method Two:

Private Staticstring Getipandmacaddress () {string IP= ""; BooleanIsbreak =false; String name= ""; Try {             for(Enumeration<networkinterface> en =networkinterface. getnetworkinterfaces (); en.hasmoreelements ();) {NetworkInterface intf=en.nextelement (); Name=Intf.getname ();  for(Enumeration<inetaddress> enumipaddr =intf. getinetaddresses (); enumipaddr.hasmoreelements ();) {inetaddress inetaddress=enumipaddr.nextelement (); if(!inetaddress.isloopbackaddress ()&& inetaddressinstanceofinet4address) {IP=inetaddress.gethostaddress (). toString (); Isbreak=true;  Break; }                }                if(isbreak) { Break; }            }        } Catch(Exception e) {e.printstacktrace (); } String mac= ""; if(!textutils.isempty (name)) {            Try {                byte[] address =networkinterface.getbyname (name). Gethardwareaddress (); if(Address! =NULL) {mac=Byte2hex (address, address.length); }            } Catch(SocketException e) {e.printstacktrace (); }        }        if(! Textutils.isempty (MAC) &&!textutils.isempty (IP)) {            returnIP + "_" +mac; }        return""; }    Private StaticString Byte2hex (byte[] B,intlength) {StringBuffer HS=Newstringbuffer (length); String stmp= ""; intLen =length;  for(intn = 0; n < Len; n++) {stmp= Integer.tohexstring (B[n] & 0xFF); if(stmp.length () = = 1) HS= Hs.append ("0"). Append (STMP); Else{HS=hs.append (STMP); }            if(n! = len-1) {hs.append (":"); }        }        returnstring.valueof (HS); }

Questions:

1. Which of the above two approaches is more efficient?

2. Are there any limitations in two different ways?

3. Leave a future ...

Get your Android device WiFi MAC address "MAC address"

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.