This article was reproduced from: http://blog.csdn.net/crazyman2010/article/details/50464256
Today, the acquisition of MAC address to do some learning, the current way to get MAC address online is mainly as follows:
-Use BusyBox ifconfig
-Using cat/sys/class/net/wlan0/address
-Using wifiinfo.getmacaddress
-Using networkinterface.gethardwareaddress
In airplane mode, all basic methods are not available.
BusyBox ifconfig
Not all machines are loaded with busybox, so the compatibility with BusyBox is poor. This can be replaced with the Android Terminal Command Netcfg (the last column is the MAC address):
Cat/sys/class/net/wlan0/address
This method is generally more reliable by reading the MAC address information provided by the NIC driver, but as with BusyBox ifconfig or Netcfg, the returned string needs to be parsed manually.
Wifiinfo.getmacaddress
WifiManager wifiManager=(WifiManager) getSystemService(Context.WIFI_SERVICE);WifiInfo wifiInfo=wifiManager.getConnectionInfo();String mac=wifiInfo.getMacAddress();
This method is more general, the original has been used this, but recently found that on Android 6.0 system, this method is invalid, returned to the "02:00:00:00:00:00" constant, the Internet is said to be a permission problem, but has already opened all the permissions, or return this value, It could be a bug, and maybe the problem will be fixed in the future.
Netizens prompted, found that this is not a bug, Google's blog to find the following paragraph:
Most notably, Local WiFi and Bluetooth MAC addresses is no longer available. The Getmacaddress () method of a Wifiinfo object and the Bluetoothadapter.getdefaultadapter (). GetAddress () method would both Return 02:00:00:00:00:00 from now on.
Networkinterface.gethardwareaddress
This is my current method, the principle and cat/sys/class/net/wlan0/address is identical, but this is the upper API, do not need to handle the underlying data. Tested on Android 6.0 pass.
networkInterface = NetworkInterface.getByName("wlan0");return ConvertMacAddressBytesToString(networkInterface.getHardwareAddress());
Get WiFi MAC Address summary "Go"