How to get WiFi MAC address on Android

Source: Internet
Author: User

1. Frequent usage, call Android Api:wifimanager

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

Wifimanager WiFi = (wifimanager) getsystemservice (Context.wifi_service);
Wifiinfo info = Wifi.getconnectioninfo ();
return info.getmacaddress ();

This method needs to ensure that WiFi has been opened before this boot, otherwise it will return null. So you need to start the background to open WiFi and get it.

Demo Sample code:

Try to turn on WiFi
private static Boolean Tryopenmac (Wifimanager manager)
{
Boolean softopenwifi = false;
int state = Manager.getwifistate ();
if (state! = wifimanager.wifi_state_enabled && state! = wifimanager.wifi_state_enabling)
{
Manager.setwifienabled (TRUE);
Softopenwifi = true;
}
return Softopenwifi;
}

Try turning off your Mac
private static void Tryclosemac (Wifimanager manager)
{
Manager.setwifienabled (FALSE);
}

Try to get MAC address
private static String Trygetmac (Wifimanager Manager)
{
Wifiinfo wifiinfo = Manager.getconnectioninfo ();
if (Wifiinfo = = NULL | | Stringutil.isnull (Wifiinfo.getmacaddress ()))
{
return null;
}
String mac = Wifiinfo.getmacaddress (). ReplaceAll (":", ""). Trim (). toUpperCase ();
Mac = Formatidentify (MAC);
return mac;
}

Attempt to read MAC address
private static String getmacfromdevice (int internal)
{
String Mac=null;
Wifimanager Wifimanager = (wifimanager) getsystemservice (Context.wifi_service);
Mac = Trygetmac (Wifimanager);
if (! Stringutil.isnull (MAC))
{
return mac;
}

Get failed, try to open WiFi get
Boolean Isokwifi = Tryopenmac (Wifimanager);
for (int index=0;index<internal;index++)
{
Assume that the first time there is no success, the second time is 100 milliseconds delay.
if (index!=0)
{
Try
{
Thread.Sleep (100);
}
catch (Interruptedexception e)
{
E.printstacktrace ();
}
}
Mac = Trygetmac (Wifimanager);
if (! Stringutil.isnull (MAC))
{
Break
}
}

Try turning off WiFi
if (Isokwifi)
{
Tryclosemac (Wifimanager);
}
return mac;
}

==================================================================

2. query file path/sys/class/net/wlan0/address

ADB Shell cat/sys/class/net/wlan0/address

98:6c:f5:10:96:c4

However, assuming that WiFi has never been opened during this boot, the MAC address returned is different (not actual), such as the following:

Cat/sys/class/net/wlan0/address
00:90:4c:11:22:33

Demo Sample code:

String Getmac () {
String macserial = null;
String str = "";
try {
Process pp = Runtime.getruntime (). EXEC ("cat/sys/class/net/wlan0/address");
InputStreamReader ir = new InputStreamReader (Pp.getinputstream ());
LineNumberReader input = new LineNumberReader (IR);

for (; null! = str;) {
str = Input.readline ();
if (str! = null) {
Macserial = Str.trim ();
Break
}
}
} catch (IOException e) {
E.printstacktrace ();
}
return macserial;
}
==================================================================

3. Query the file "/proc/net/arp" that recorded the MAC address, but from the test results, the returned data is not the desired WiFi MAC address:

For example, the actual WiFi MAC address of the phone is 98:6c:f5:10:96:c4, but using adb shell cat Proc/net/arp returns:

Cat Proc/net/arp
IP address HW type Flags HW address Mask Device
192.168.0.3 0x1 0x2 00:23:89:B9:1A:FD * wlan0
192.168.0.1 0x1 0x2 08:00:27:7c:65:5a * wlan0

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.