About getting android6.0 's MAC address

Source: Internet
Author: User

Original: http://blog.csdn.net/suziluo123/article/details/51382410

Recently contacted 6.0, found using a simple wifiManager.getConnectionInfo().getMacAddress(); method to obtain a MAC address. Google later found that Google's access to hardware information, its control more stringent. The SD card reads and writes, in addition to the Authority Declaration, should also request dynamically, otherwise will be forbidden. As for MAC network address acquisition, the latest result returned by the corresponding API is 02:00:00:00:00:00 this fixed value. You can see Getting MAC address in Android 6.0.
According to the official explanation, this is to 更好的数据保护 see the Android 6.0 change article in the Access to Hardware Identifier section.

Traditional ways to get a MAC:
final WifiManager wifiManager = (WifiManager) getApplication()                .getApplicationContext().getSystemService(Context.WIFI_SERVICE);final String wifiMACaddress = wifiManager.getConnectionInfo() .getMacAddress();

However, this method 6.0 does not do a very well, how to do?

Query the data to find a corresponding solution:
Api--networkinterface.getnetworkinterfaces (), which uses Java to obtain device network device information, can still obtain the MAC address indirectly.

//interested friends can see how networkinterface is implemented in the Android framework     Public StaticString macAddress ()throwssocketexception {String address=NULL; //storing the access network interface on the current machine in the enumeration collectionenumeration<networkinterface> interfaces =networkinterface.getnetworkinterfaces ();  while(Interfaces.hasmoreelements ()) {NetworkInterface netWork=interfaces.nextelement (); //If there is a hardware address and can be accessed with the given current permission, the hardware address (usually MAC) is returned.                 byte[] by =network.gethardwareaddress (); if(by = =NULL|| By.length = = 0) {                    Continue; } StringBuilder Builder=NewStringBuilder ();  for(byteb:by) {Builder.append (String.Format ("%02X:", b)); }                if(Builder.length () > 0) {Builder.deletecharat (Builder.length ()-1); } String mac=builder.tostring (); LOG.D ("Mac", "Interfacename=" +network.getname () + ", mac=" +mac); //from the MAC address List of the online device on the router, you can verify that the device WiFi name is Wlan0                if(Network.getname (). Equals ("Wlan0") {LOG.D ("Mac", "InterfaceName =" +network.getname () + ", mac=" +mac); Address=mac; }            }            returnaddress; } 

This will be able to get to the 6.0 MAC address normally! I hope we can help you.

About getting android6.0 's 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.