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>