|
@@ -5,6 +5,7 @@ import android.content.Context;
|
|
|
import android.net.ConnectivityManager;
|
|
|
import android.net.wifi.WifiInfo;
|
|
|
import android.net.wifi.WifiManager;
|
|
|
+import android.os.Build;
|
|
|
import android.text.TextUtils;
|
|
|
|
|
|
import com.wdkl.ncs.android.lib.base.BaseApplication;
|
|
@@ -313,19 +314,46 @@ public class NetHelper {
|
|
|
* @return String
|
|
|
*/
|
|
|
public String getMacAddress() {
|
|
|
- String mac = "";
|
|
|
- try {
|
|
|
- mac = getLocalMacAddressFromIp();
|
|
|
- if (TextUtils.isEmpty(mac)) {
|
|
|
- mac = getNetworkMac();
|
|
|
+ if(Build.VERSION.SDK_INT <= Build.VERSION_CODES.O){//小于安卓8 走这里
|
|
|
+ String mac = "";
|
|
|
+ try {
|
|
|
+ mac = getLocalMacAddressFromIp();
|
|
|
+ if (TextUtils.isEmpty(mac)) {
|
|
|
+ mac = getNetworkMac();
|
|
|
+ }
|
|
|
+ if (TextUtils.isEmpty(mac)) {
|
|
|
+ mac = tryGetWifiMac();
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
}
|
|
|
- if (TextUtils.isEmpty(mac)) {
|
|
|
- mac = tryGetWifiMac();
|
|
|
+ return mac;
|
|
|
+ }else {//大于安卓8走这里
|
|
|
+ List<NetworkInterface> interfaces = null;
|
|
|
+ try {
|
|
|
+ interfaces = Collections.list(NetworkInterface.getNetworkInterfaces());
|
|
|
+ for (NetworkInterface networkInterface : interfaces) {
|
|
|
+ if (networkInterface != null && TextUtils.isEmpty(networkInterface.getName()) == false) {
|
|
|
+ if ("wlan0".equalsIgnoreCase(networkInterface.getName())) {
|
|
|
+ byte[] macBytes = networkInterface.getHardwareAddress();
|
|
|
+ if (macBytes != null && macBytes.length > 0) {
|
|
|
+ StringBuilder str = new StringBuilder();
|
|
|
+ for (byte b : macBytes) {
|
|
|
+ str.append(String.format("%02X:", b));
|
|
|
+ }
|
|
|
+ if (str.length() > 0) {
|
|
|
+ str.deleteCharAt(str.length() - 1);
|
|
|
+ }
|
|
|
+ return str.toString();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (SocketException e) {
|
|
|
+ e.printStackTrace();
|
|
|
}
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
+ return "unknown";
|
|
|
+
|
|
|
}
|
|
|
- return mac;
|
|
|
}
|
|
|
|
|
|
/**
|