Monday, December 21, 2015

Android 디바이스 UUID 획득하기

public static String getDeviceUUID(final Context context) {
        // preference에서 저장된 것이 있는지 확인해봄
        final SharedPreferences prefs = context.getSharedPreferences("UUIDTEST", MODE_PRIVATE);
        final String id = prefs.getString("UUID", null);

        UUID uuid = null;
        if (id != null) {
            uuid = UUID.fromString(id);
        } else {
            final String androidId = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
            try {
                // 예전에 아래와 같은 고정값이 리턴되는 문제가 있었음
                if (!"9774d56d682e549c".equals(androidId)) {
                    uuid = UUID.nameUUIDFromBytes(androidId.getBytes("utf8"));
                } else {
                    final String deviceId = ((TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE)).getDeviceId();
                    uuid = deviceId != null ? UUID.nameUUIDFromBytes(deviceId.getBytes("utf8")) : UUID.randomUUID();
                }
            } catch (UnsupportedEncodingException e) {
                throw new RuntimeException(e);
            }

            final SharedPreferences.Editor editor = prefs.edit();
            editor.putString("UUID", uuid.toString());
            editor.commit();
        }

        return uuid.toString();
    }

No comments:

Post a Comment