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"