소스 검색

修改提交

wangjk 4 년 전
부모
커밋
e7fd819709

+ 1 - 4
home/src/main/code/com/wdkl/ncs/android/component/home/activity/WatchRegisterActivity.kt

@@ -40,13 +40,10 @@ class WatchRegisterActivity: BaseActivity<WatchDevicePresenter, WatchActivityReg
     }
 
     override fun init() {
-        Constants.mac = NetHelper.getInstance().macAddress2
-//        Constants.mac = ""
+        Constants.mac = NetHelper.getInstance().macAddress
         Log.e(TAG,"mac "+Constants.mac)
         presenter.loadData(Constants.mac)
 
-
-
     }
 
     override fun bindEvent() {

+ 38 - 10
home/src/main/code/com/wdkl/ncs/android/component/home/util/NetHelper.java

@@ -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;
     }
 
     /**