package com.qimai.android.widget;

import android.os.Build;

import java.lang.reflect.Method;

/**
 * <pre>
 *     author : leiwang
 *     e-mail : xxx@xx
 *     time   : 2021/08/12
 *     desc   :
 *     version: 1.0
 * </pre>
 */
public class Utils {

    /**
     * 获取SN
     * @return
     */
    public static String getSN()
    {
        String serial = "";
        //通过android.os获取sn号
        try {
            serial = android.os.Build.SERIAL;
            if (!serial.equals("") && !serial.equals("unknown")) {
                return serial;
            }
        } catch (Exception e) {
            serial = "";
        }

        //通过反射获取sn号
        try {
            Class<?> c = Class . forName ("android.os.SystemProperties");
            Method get = c . getMethod ("get", String.class);
            serial = (String) get . invoke (c, "ro.serialno");
            if (!serial.equals("") && !serial.equals("unknown")) return serial;

            //9.0及以上无法获取到sn，此方法为补充，能够获取到多数高版本手机 sn
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) serial = Build.getSerial();
        } catch (Exception e) {
            serial = "";
        }
        return serial;

    }
}
