Fix Android 6.0 get WiFi MAC address for 02:00:00:00:00:00 problem "go"

Source: Internet
Author: User

This article was reproduced from: http://www.jb51.net/article/128398.htm

This article mainly describes the Android 6.0 get WiFi MAC address for 02:00:00:00:00:00 solution, very good, with reference value, need friends can refer to the next

Objective:

The previous project was older, the mobile version was relatively low, and it was developed using eclipse. Need to get a mobile WiFi MAC address. The following code is used:

The previous version of Android 6.0 can be used in a method (simulator can be used)   private string Getmacaddrold ()   {     string macstring = "";     Wifimanager wifimsg = (wifimanager) getsystemservice (context.wifi_service);     if (wifimsg! = null)     {       if (wifimsg.getconnectioninfo () = null)       {         if (Wifimsg.getconnectioninfo (). Getmacaddress ()! = null)         {           macstring = Wifimsg.getconnectioninfo (). getmacaddress ();}}     }     return macstring;   }

▲ Problems arise:

With this method, the WiFi MAC address is normally available on the emulator, but on Android 6.0, there is a problem and the return is "02:00:00:00:00:00"

▲ Problem Analysis:

Originally Google official in order to give users more data protection, starting from this 6.0 version, Android removed via WiFi and Bluetooth API to the application of programmable access to the local hardware identifier. Both the wifiinfo.getmacaddress () and Bluetoothadapter.getaddress () methods will now return 02:00:00:00:00:00

▲ Solution:

There is a policy on the so-called, there are countermeasures. We can use the following code to get the mobile WiFi MAC address, the same can solve the problem of more than 6.0 version. It is worth noting that the simulator uses the following code to obtain is not get!

public static String getmacaddr () {     try {       list<networkinterface> all = Collections.list ( Networkinterface.getnetworkinterfaces ());       for (NetworkInterface nif:all) {         if (!nif.getname (). Equalsignorecase ("Wlan0")) continue;         byte[] macbytes = nif.gethardwareaddress ();         if (macbytes = = null) {           return "";         }         StringBuilder res1 = new StringBuilder ();         for (byte b:macbytes) {           res1.append (String.Format ("%02x:", b));         }         if (res1.length () > 0) {           Res1.deletecharat (res1.length ()-1);         }         return res1.tostring ();       }     } catch (Exception ex) {     }     return "02:00:00:00:00:00";   }

The phone must be in a network state, and be careful not to forget to add permissions

  <uses-permission android:name= "Android.permission.INTERNET"/>  <uses-permission android:name= " Android.permission.ACCESS_NETWORK_STATE "/>

Summarize

The above is a small part of the introduction of Android 6.0 to get WiFi MAC address for the 02:00:00:00:00:00 solution, we hope to have some help, if you have any questions please give me a message, small series will promptly reply to you. Thank you very much for the support of the Scripting House website!

Fix Android 6.0 get WiFi MAC address for 02:00:00:00:00:00 problem "go"

Related Article

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.