获取设备位移识别码

1. IMEI

<u>不建议获取IMEI码, 此码其实是对应sim卡的, 而某些安卓设备可能没有sim卡, 而另一些可能双卡双待有两个sim卡</u>

2. WLAN MAC Address

使用wifi的物理地址来作为识别码, 每个设备都可以使用, 而且每个设备都不一样

参考: https://stackoverflow.com/questions/1972381/how-to-get-the-devices-imei-esn-programmatically-in-android

使用方法:

WifiManager m_wm = (WifiManager)getSystemService(Context.WIFI_SERVICE); 
String m_wlanMacAdd = m_wm.getConnectionInfo().getMacAddress();

权限:

 <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

2.1 android 6.x 以上物理地址获取失败

参考:https://developer.android.com/about/versions/marshmallow/android-6.0-changes.html
wifi 和蓝牙会返回 02:00:00:00:00:00
参考:
1.貌似是吵得 少了个函数:
http://blog.csdn.net/randyjiawenjie/article/details/6589489

  1. 这个是全的, 不知道是否是原编辑:
    http://blog.csdn.net/andoop/article/details/54633077
    3.获取安卓系统版本, 顺便一提.SDK(string)已经过时, 使用.SDK_INT(int)替代
    https://stackoverflow.com/questions/3093365/how-can-i-check-the-system-version-of-android
    我经过改良了一下, 原代码判断不成立
//获取wifi mac地址
    public static String getMacAddress(Context context) {  
        //该方法在6.0 系统 不再开发,返回 02:00:00:00:00:00  
        if (isUnderAndroid6()) {  
            WifiManager wifiMgr = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);  
            WifiInfo wifiInfo = wifiMgr.getConnectionInfo();  
            return wifiInfo.getMacAddress();
        }  
        String macAddress = null;  
        StringBuffer buf = new StringBuffer();  
        NetworkInterface networkInterface = null;  
        try {
            networkInterface = NetworkInterface.getByName("wlan0");
            if (networkInterface == null) {  
                networkInterface = NetworkInterface.getByName("eth1");
            }  
            if (networkInterface == null) {  
                return "02:00:00:00:00:02";  
            }  
            byte[] addr = networkInterface.getHardwareAddress();  
            for (byte b : addr) {  
                buf.append(String.format("%02X:", b));  
            }  
            if (buf.length() > 0) {  
                buf.deleteCharAt(buf.length() - 1);  
            }  
            macAddress = buf.toString();  
        } catch (SocketException e) {  
            e.printStackTrace();  
            return "02:00:00:00:00:02";  
        }   
        return macAddress;  
    }  
    
    //检测系统是否低于android6.0
    private static boolean isUnderAndroid6() {
        Log.i("debug","sdk version:"+android.os.Build.VERSION.SDK_INT);
        if (android.os.Build.VERSION.SDK_INT<=23) {
            //Log.e("----->" + "NetInfoManager", "isAccessWifiStateAuthorized:" + "access wifi state is enabled");
            return true;
        } else
            return false;
    }

2.2 md5加密

获取唯一码有时候需要md5来生成特征码:
结合了编辑和楼下的评论合并:
http://blog.csdn.net/randyjiawenjie/article/details/6589489
md5在线校验网站: https://md5jiami.51240.com/

//加密 md5 32位 小写
    public static String MD5It(String str){  
        MessageDigest md5 = null;
        try {
            md5 = MessageDigest.getInstance("MD5");
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }  
        if(md5!=null){
            md5.update(str.getBytes());
            BigInteger mBInteger = new BigInteger(1, md5.digest());
            return mBInteger.toString(16);
        }
         return str;
    }  

推荐阅读更多精彩内容