Android Get MAC Address

Source: Internet
Author: User

Because the WiFi MAC address is a passive consulting. Generally after the boot, will not be active into the system. To wait for the WiFi hardware to boot, the relevant MAC address data will be recorded in the system. Therefore, the general Android system to obtain the MAC address when the following situations occur:

1. You can get the MAC address when WiFi is turned on

2, turn off WiFi, do not restart the system, you can also get to the MAC address

3, the system restarts, do not open WiFi, access to MAC address

4, the system restarts, do not open WiFi, open 2G, 3G Network, access to MAC address

To get Mac address:1, wait for WiFi to start

2. Actively activate WiFi in the program

The second case code is as follows:

1  PublicString macAddress =NULL;2 3      Public voidgetwifimacaddress (Context context) {4         FinalWifimanager WiFi =(Wifimanager) Getsystemservice (context.wifi_service);5         if(WiFi = =NULL)6             return;7Wifiinfo info =wifi.getconnectioninfo ();8          This. macAddress =info.getmacaddress ();9 Ten         if( This. macAddress = =NULL&&!wifi.iswifienabled ()) { One             NewThread () { A @Override -                  Public voidrun () { -Wifi.setwifienabled (true); the                      for(inti = 0; I < 10; i++) { -Wifiinfo _info =wifi.getconnectioninfo (); -MacAddress =_info.getmacaddress (); -                         if(MacAddress! =NULL) +                              Break; -                         Try { +Thread.Sleep (500); A}Catch(interruptedexception e) { at                             //TODO auto-generated Catch block - e.printstacktrace (); -                         } -                     } -Wifi.setwifienabled (false); -                 } in }.start (); -              toSystem.out.println ("Getmac:" +macAddress); +         } -}

Set permissions in Androidmanifest.xml:

<uses-permission android:name= "Android.permission.ACCESS_WIFI_STATE" ></uses-permission>    < Uses-permission android:name= "Android.permission.CHANGE_WIFI_STATE" ></uses-permission>    < Uses-permission android:name= "Android.permission.ACCESS_NETWORK_STATE" ></uses-permission>

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.