소스 검색

测试设备信息

zengjiebin 7 년 전
부모
커밋
d35b4aae34

+ 355 - 0
app/src/main/java/com/sheep/gamegroup/util/MacDeviceUtil.java

@@ -0,0 +1,355 @@
+package com.sheep.gamegroup.util;
+
+import android.content.Context;
+import android.content.pm.PackageManager;
+import android.net.wifi.WifiInfo;
+import android.net.wifi.WifiManager;
+import android.os.Build;
+import android.text.TextUtils;
+import android.util.Log;
+import android.widget.Toast;
+
+import java.io.BufferedReader;
+import java.io.FileReader;
+import java.io.InputStreamReader;
+import java.io.LineNumberReader;
+import java.io.Reader;
+import java.net.InetAddress;
+import java.net.NetworkInterface;
+import java.net.SocketException;
+import java.util.Enumeration;
+
+/**
+ * Created by realicing on 2019/3/19.
+ * realicing@sina.com
+ */
+public class MacDeviceUtil {
+    public static String getMac(Context context) {
+        String strMac = null;
+
+        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
+            Log.e("=====", "6.0以下");
+            Toast.makeText(context, "6.0以下", Toast.LENGTH_SHORT).show();
+            strMac = getLocalMacAddressFromWifiInfo(context);
+            return strMac;
+        } else if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N
+                && Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
+            Log.e("=====", "6.0以上7.0以下");
+            Toast.makeText(context, "6.0以上7.0以下", Toast.LENGTH_SHORT).show();
+            strMac = getMacAddress(context);
+            return strMac;
+        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
+            Log.e("=====", "7.0以上");
+            if (!TextUtils.isEmpty(getMacAddress())) {
+                Log.e("=====", "7.0以上1");
+                Toast.makeText(context, "7.0以上1", Toast.LENGTH_SHORT).show();
+                strMac = getMacAddress();
+                return strMac;
+            } else if (!TextUtils.isEmpty(getMachineHardwareAddress())) {
+                Log.e("=====", "7.0以上2");
+                Toast.makeText(context, "7.0以上2", Toast.LENGTH_SHORT).show();
+                strMac = getMachineHardwareAddress();
+                return strMac;
+            } else {
+                Log.e("=====", "7.0以上3");
+                Toast.makeText(context, "7.0以上3", Toast.LENGTH_SHORT).show();
+                strMac = getLocalMacAddressFromBusybox();
+                return strMac;
+            }
+        }
+
+        return "02:00:00:00:00:00";
+    }
+
+    /**
+     * 根据wifi信息获取本地mac
+     * @param context
+     * @return
+     */
+    public static String getLocalMacAddressFromWifiInfo(Context context){
+        WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
+        WifiInfo winfo = wifi.getConnectionInfo();
+        String mac =  winfo.getMacAddress();
+        return mac;
+    }
+    /**
+     * android 6.0及以上、7.0以下 获取mac地址
+     *
+     * @param context
+     * @return
+     */
+    public static String getMacAddress(Context context) {
+
+        // 如果是6.0以下,直接通过wifimanager获取
+        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
+            String macAddress0 = getMacAddress0(context);
+            if (!TextUtils.isEmpty(macAddress0)) {
+                return macAddress0;
+            }
+        }
+        String str = "";
+        String macSerial = "";
+        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 (Exception ex) {
+            Log.e("----->" + "NetInfoManager", "getMacAddress:" + ex.toString());
+        }
+        if (macSerial == null || "".equals(macSerial)) {
+            try {
+                return loadFileAsString("/sys/class/net/eth0/address")
+                        .toUpperCase().substring(0, 17);
+            } catch (Exception e) {
+                e.printStackTrace();
+                Log.e("----->" + "NetInfoManager",
+                        "getMacAddress:" + e.toString());
+            }
+
+        }
+        return macSerial;
+    }
+
+    private static String getMacAddress0(Context context) {
+        if (isAccessWifiStateAuthorized(context)) {
+            WifiManager wifiMgr = (WifiManager) context
+                    .getSystemService(Context.WIFI_SERVICE);
+            WifiInfo wifiInfo = null;
+            try {
+                wifiInfo = wifiMgr.getConnectionInfo();
+                return wifiInfo.getMacAddress();
+            } catch (Exception e) {
+                Log.e("----->" + "NetInfoManager",
+                        "getMacAddress0:" + e.toString());
+            }
+
+        }
+        return "";
+
+    }
+
+    /**
+     * Check whether accessing wifi state is permitted
+     *
+     * @param context
+     * @return
+     */
+    private static boolean isAccessWifiStateAuthorized(Context context) {
+        if (PackageManager.PERMISSION_GRANTED == context
+                .checkCallingOrSelfPermission("android.permission.ACCESS_WIFI_STATE")) {
+            Log.e("----->" + "NetInfoManager", "isAccessWifiStateAuthorized:"
+                    + "access wifi state is enabled");
+            return true;
+        } else
+            return false;
+    }
+
+    private static String loadFileAsString(String fileName) throws Exception {
+        FileReader reader = new FileReader(fileName);
+        String text = loadReaderAsString(reader);
+        reader.close();
+        return text;
+    }
+
+    private static String loadReaderAsString(Reader reader) throws Exception {
+        StringBuilder builder = new StringBuilder();
+        char[] buffer = new char[4096];
+        int readLength = reader.read(buffer);
+        while (readLength >= 0) {
+            builder.append(buffer, 0, readLength);
+            readLength = reader.read(buffer);
+        }
+        return builder.toString();
+    }
+    /**
+     * 根据IP地址获取MAC地址
+     *
+     * @return
+     */
+    public static String getMacAddress() {
+        String strMacAddr = null;
+        try {
+            // 获得IpD地址
+            InetAddress ip = getLocalInetAddress();
+            byte[] b = NetworkInterface.getByInetAddress(ip)
+                    .getHardwareAddress();
+            StringBuffer buffer = new StringBuffer();
+            for (int i = 0; i < b.length; i++) {
+                if (i != 0) {
+                    buffer.append(':');
+                }
+                String str = Integer.toHexString(b[i] & 0xFF);
+                buffer.append(str.length() == 1 ? 0 + str : str);
+            }
+            strMacAddr = buffer.toString().toUpperCase();
+        } catch (Exception e) {
+        }
+        return strMacAddr;
+    }
+    /**
+     * 获取移动设备本地IP
+     *
+     * @return
+     */
+    private static InetAddress getLocalInetAddress() {
+        InetAddress ip = null;
+        try {
+            // 列举
+            Enumeration<NetworkInterface> en_netInterface = NetworkInterface
+                    .getNetworkInterfaces();
+            while (en_netInterface.hasMoreElements()) {// 是否还有元素
+                NetworkInterface ni = (NetworkInterface) en_netInterface
+                        .nextElement();// 得到下一个元素
+                Enumeration<InetAddress> en_ip = ni.getInetAddresses();// 得到一个ip地址的列举
+                while (en_ip.hasMoreElements()) {
+                    ip = en_ip.nextElement();
+                    if (!ip.isLoopbackAddress()
+                            && ip.getHostAddress().indexOf(":") == -1)
+                        break;
+                    else
+                        ip = null;
+                }
+
+                if (ip != null) {
+                    break;
+                }
+            }
+        } catch (SocketException e) {
+
+            e.printStackTrace();
+        }
+        return ip;
+    }
+
+    /**
+     * 获取本地IP
+     *
+     * @return
+     */
+    private static String getLocalIpAddress() {
+        try {
+            for (Enumeration<NetworkInterface> en = NetworkInterface
+                    .getNetworkInterfaces(); en.hasMoreElements(); ) {
+                NetworkInterface intf = en.nextElement();
+                for (Enumeration<InetAddress> enumIpAddr = intf
+                        .getInetAddresses(); enumIpAddr.hasMoreElements(); ) {
+                    InetAddress inetAddress = enumIpAddr.nextElement();
+                    if (!inetAddress.isLoopbackAddress()) {
+                        return inetAddress.getHostAddress().toString();
+                    }
+                }
+            }
+        } catch (SocketException ex) {
+            ex.printStackTrace();
+        }
+        return null;
+    }
+    /**
+     * android 7.0及以上 (2)扫描各个网络接口获取mac地址
+     *
+     */
+    /**
+     * 获取设备HardwareAddress地址
+     *
+     * @return
+     */
+    public static String getMachineHardwareAddress() {
+        Enumeration<NetworkInterface> interfaces = null;
+        try {
+            interfaces = NetworkInterface.getNetworkInterfaces();
+        } catch (SocketException e) {
+            e.printStackTrace();
+        }
+        String hardWareAddress = null;
+        NetworkInterface iF = null;
+        if (interfaces == null) {
+            return null;
+        }
+        while (interfaces.hasMoreElements()) {
+            iF = interfaces.nextElement();
+            try {
+                hardWareAddress = bytesToString(iF.getHardwareAddress());
+                if (hardWareAddress != null)
+                    break;
+            } catch (SocketException e) {
+                e.printStackTrace();
+            }
+        }
+        return hardWareAddress;
+    }
+
+    /***
+     * byte转为String
+     *
+     * @param bytes
+     * @return
+     */
+    private static String bytesToString(byte[] bytes) {
+        if (bytes == null || bytes.length == 0) {
+            return null;
+        }
+        StringBuilder buf = new StringBuilder();
+        for (byte b : bytes) {
+            buf.append(String.format("%02X:", b));
+        }
+        if (buf.length() > 0) {
+            buf.deleteCharAt(buf.length() - 1);
+        }
+        return buf.toString();
+    }
+    /**
+     * android 7.0及以上 (3)通过busybox获取本地存储的mac地址
+     *
+     */
+
+    /**
+     * 根据busybox获取本地Mac
+     *
+     * @return
+     */
+    public static String getLocalMacAddressFromBusybox() {
+        String result = "";
+        String Mac = "";
+        result = callCmd("busybox ifconfig", "HWaddr");
+        // 如果返回的result == null,则说明网络不可取
+        if (result == null) {
+            return "网络异常";
+        }
+        // 对该行数据进行解析
+        // 例如:eth0 Link encap:Ethernet HWaddr 00:16:E8:3E:DF:67
+        if (result.length() > 0 && result.contains("HWaddr") == true) {
+            Mac = result.substring(result.indexOf("HWaddr") + 6,
+                    result.length() - 1);
+            result = Mac;
+        }
+        return result;
+    }
+
+    private static String callCmd(String cmd, String filter) {
+        String result = "";
+        String line = "";
+        try {
+            Process proc = Runtime.getRuntime().exec(cmd);
+            InputStreamReader is = new InputStreamReader(proc.getInputStream());
+            BufferedReader br = new BufferedReader(is);
+
+            while ((line = br.readLine()) != null
+                    && line.contains(filter) == false) {
+                result += line;
+            }
+
+            result = line;
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+        return result;
+    }
+}

+ 45 - 19
app/src/main/java/com/sheep/gamegroup/util/SysAppUtil.java

@@ -3,7 +3,6 @@ package com.sheep.gamegroup.util;
 import android.app.Activity;
 import android.app.Dialog;
 import android.content.ComponentName;
-import android.content.DialogInterface;
 import android.content.Intent;
 import android.content.pm.ApplicationInfo;
 import android.content.pm.PackageInfo;
@@ -686,7 +685,7 @@ public class SysAppUtil {
         } catch (Exception e) {
             e.printStackTrace();
         }
-        return TextUtils.isEmpty(sb) ? String.format(Locale.CHINA, "Android %s/%d; Model %s; Brand %s", getSystemVersion(), Build.VERSION.SDK_INT, getSystemModel(), getDeviceBrand()) : sb.toString();
+        return TextUtils.isEmpty(sb) ? String.format(Locale.CHINA, "Android %s/%d; Model %s; Brand %s", getSystemVersion(), Build.VERSION.SDK_INT, getDeviceModel(), getDeviceBrand()) : sb.toString();
     }
 
     /**
@@ -698,23 +697,6 @@ public class SysAppUtil {
         return android.os.Build.VERSION.RELEASE;
     }
 
-    /**
-     * 获取手机型号
-     *
-     * @return  手机型号
-     */
-    public static String getSystemModel() {
-        return android.os.Build.MODEL;
-    }
-
-    /**
-     * 获取手机厂商
-     *
-     * @return  手机厂商
-     */
-    public static String getDeviceBrand() {
-        return android.os.Build.BRAND;
-    }
 
     public static String readCpuInfo() {
         String result = "";
@@ -748,4 +730,48 @@ public class SysAppUtil {
         }
         return false;
     }
+
+
+    /**
+     * 获取厂商
+     */
+    public static String getDeviceManufacturer(){
+        return Build.MANUFACTURER;
+    }
+    /**
+     * 获取产品名
+     */
+    public static String getDeviceProduct(){
+        return Build.PRODUCT;
+    }
+    /**
+     * 获取手机品牌
+     */
+    public static String getDeviceBrand(){
+        return Build.BRAND;
+    }
+    /**
+     * 获取手机型号
+     */
+    public static String getDeviceModel(){
+        return Build.MODEL;
+    }
+    /**
+     * 获取主板名
+     */
+    public static String getDeviceBoard(){
+        return Build.BOARD;
+    }
+    /**
+     * 获取设备名
+     */
+    public static String getDeviceDevice(){
+        return Build.DEVICE;
+    }
+    /**
+     * 获取mac地址
+     */
+    public static String getDeviceMac(){
+        return MacDeviceUtil.getMac(SheepApp.getInstance());
+    }
 }

+ 16 - 1
app/src/main/java/com/sheep/gamegroup/util/TestUtil.java

@@ -264,7 +264,7 @@ public class TestUtil {
      */
     public static void test(final Activity activity) {
         final String[] items = {"复制token", "添加token", "复制打点数据", "从jenkins下载小绵羊安装包", "测试表情包",
-                "跳转QQ", "跳转QQ群","下载视频测试","爱奇艺VIP会员", "分享我的签名的小绵羊apk", "分享当前小绵羊apk",
+                "跳转QQ", "跳转QQ群", "测试设备信息","下载视频测试","爱奇艺VIP会员", "分享我的签名的小绵羊apk", "分享当前小绵羊apk",
                 "测试升级对话框","少数民族姓名", "开启皮肤", "不开启皮肤", "我的游戏", "优惠券弹窗", "填写收件地址弹窗",
                 "朗读文字", "游戏搜索", "测试插件","测试bitmap", "剪切视频", "我的关注", "足迹",
                 "测试联通卡", "测试联通卡2", "测试签名1", "测试签名2", "测试孔剑秋faq正式服",
@@ -289,6 +289,9 @@ public class TestUtil {
             return;
         }
         switch (item) {
+            case "测试设备信息":
+                testDeviceInfo();
+                break;
             case "下载视频测试":
                 String downloadUrl = "http://cdn.video.17xmy.com/ljok9RZZvXc5qwvoCxEGudchO6Ti";
                 String fileName = System.currentTimeMillis()+".mp4";
@@ -694,6 +697,18 @@ public class TestUtil {
         }
     }
 
+    /**
+     * 测试设备信息
+     */
+    private static void testDeviceInfo() {
+        String info = String.format(Locale.CHINA, "厂商\t%s\n产品名\t%s\n品牌\t%s\n型号\t%s\n主板名\t%s\n设备名\t%s\nmac地址\t%s", SysAppUtil.getDeviceManufacturer()
+                , SysAppUtil.getDeviceProduct(), SysAppUtil.getDeviceBrand(), SysAppUtil.getDeviceModel()
+                , SysAppUtil.getDeviceBoard(), SysAppUtil.getDeviceDevice(), SysAppUtil.getDeviceMac());
+        StringUtils.CopyText(info);
+        G.showToast(info);
+        LogUtil.println(info);
+    }
+
     private static void testUpgradeDialog() {
         SheepApp.getInstance().getNetComponent().getApiService().getNewVersion(SheepApp.getInstance().getConnectAddress().getVersionType())
                 .subscribeOn(Schedulers.io())

+ 1 - 1
app/src/main/java/com/sheep/gamegroup/util/WebViewJs.java

@@ -121,7 +121,7 @@ public class WebViewJs {
 
     @JavascriptInterface
     public String getPhoneModel() {
-        return SysAppUtil.getSystemModel();
+        return SysAppUtil.getDeviceModel();
     }
 
     @JavascriptInterface

+ 1 - 1
app/src/main/java/com/sheep/gamegroup/util/js/KFZSJs.java

@@ -225,7 +225,7 @@ public class KFZSJs {
 
     @JavascriptInterface
     public String getPhoneModel() {
-        return SysAppUtil.getSystemModel();
+        return SysAppUtil.getDeviceModel();
     }
 
     @JavascriptInterface