package com.wpc.library.util;
|
|
import android.content.Context;
|
import android.net.wifi.WifiInfo;
|
import android.net.wifi.WifiManager;
|
import android.os.Build;
|
import android.text.TextUtils;
|
|
import java.io.BufferedReader;
|
import java.io.File;
|
import java.io.FileReader;
|
import java.io.IOException;
|
import java.net.NetworkInterface;
|
import java.util.Collections;
|
import java.util.List;
|
import java.util.Locale;
|
|
public class MacUtil {
|
|
private static String getMacDefault(Context context) {
|
String mac = "00:00:00:00:00:00";
|
if (context == null) {
|
return mac;
|
}
|
WifiManager wifi = (WifiManager) context.getApplicationContext()
|
.getSystemService(Context.WIFI_SERVICE);
|
if (wifi == null) {
|
return mac;
|
}
|
WifiInfo info = null;
|
try {
|
info = wifi.getConnectionInfo();
|
} catch (Exception e) {
|
}
|
if (info == null) {
|
return null;
|
}
|
mac = info.getMacAddress();
|
if (!TextUtils.isEmpty(mac)) {
|
mac = mac.toUpperCase(Locale.ENGLISH);
|
}
|
return mac;
|
}
|
|
|
private static String getMacFromFile() {
|
String wifiAddress = "00:00:00:00:00:00";
|
try {
|
wifiAddress = new BufferedReader(new FileReader(new File("/sys/class/net/wlan0/address"))).readLine();
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
return wifiAddress;
|
}
|
|
|
private static String getMacFromHardware() {
|
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 e) {
|
e.printStackTrace();
|
}
|
return "00:00:00:00:00:00";
|
}
|
|
public static String getMacAddress(Context context) {
|
String mac = "00:00:00:00:00:00";
|
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
|
mac = getMacDefault(context);
|
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
|
mac = getMacFromFile();
|
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
|
mac = getMacFromHardware();
|
}
|
return mac;
|
}
|
|
}
|