Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Three different methods to get unique identifier on Android, they are all not the perfect solutions however could be used under project restriction limit.
/**
* Three different methods to get unique identifier on Android, they are all not the perfect solutions however could be used under project restriction limit.
*
* @author Xinyue Zhao
*/
public class DeviceUniqueUtil {
/**
* Android UUID method. It could be an unique solution.
* See. http://developer.android.com/reference/java/util/UUID.html
*/
public String getAndroidUUID(Context _c) {
return UUID.randomUUID().toString();
}
/**
* Get first primary GMAIL account as an unique bundle.
*/
public String getAndroidPrimaryGmail(Context _c) {
if (_c != null) {
Pattern emailPattern = Patterns.EMAIL_ADDRESS;
Account[] accounts = AccountManager.get(_c).getAccountsByType("com.google");
for (Account account: accounts) {
if (emailPattern.matcher(account.name).matches()) {
String email = account.name;
String manufactor = Build.MANUFACTURER;
String model = Build.MODEL;
String serial = Build.SERIAL;
StringBuilder sb = new StringBuilder();
appendNonEmptyString(email, sb);
appendNonEmptyString(manufactor, sb);
appendNonEmptyString(model, sb);
appendNonEmptyString(serial, sb);
if (isStandalone()) {
sb.append(getCenterId());
}
try {
deviceId = Util.SHA1(sb.toString());
} catch (NoSuchAlgorithmException _e) {
_e.printStackTrace();
} catch (UnsupportedEncodingException _e) {
_e.printStackTrace();
}
break;
}
}
}
return deviceId;
}
/**
* Fetch a MD5-Hash by getting IMEI or MAC or ANDROID's Secure-ID.
*/
private static class DeviceId {
/**
* requires permissions TELEPHONY_SERVICE on cell phones and WIFI_SERVICE on WiFi-only devices
*
* @param Context
* _cxt the current activity
* @return String
* @throws Exception
* when there is no unique ID
*/
static public String getDeviceId(Context _cxt) throws Exception {
String readableId;
// Requires READ_PHONE_STATE
TelephonyManager tm = (TelephonyManager)
_cxt.getSystemService(Context.TELEPHONY_SERVICE);
// gets the imei (GSM) or MEID/ESN (CDMA)
String imei = tm.getDeviceId();
if (null != imei && imei.length() > 0 && Long.parseLong(imei) > 0) { // 000000000000000 for emulator
readableId = imei;
} else {
// devices without SIM card (e.g. WiFi-only tablets)
// requires ACCESS_WIFI_STATE
WifiManager wm = (WifiManager) _cxt.getSystemService(Context.WIFI_SERVICE);
// gets the MAC address
String mac = wm.getConnectionInfo().getMacAddress();
if (null != mac && mac.length() > 0) {
readableId = mac;
} else {
// gets the android-assigned id
// unfortunately, this is not unique on some devices:
// http://groups.google.com/group/android-developers/browse_thread/thread/53898e508fab44f6/84e54feb28272384?pli=1
// so it's only a fallback for emulators
String androidId = Secure.getString(_cxt.getContentResolver(), Secure.ANDROID_ID);
if (null != androidId && androidId.length() > 0) {
readableId = androidId;
} else {
throw new Exception("cannot calculate a unique device ID");
}
}
}
return md5(readableId);
}
private static String md5(String _plaintext) throws NoSuchAlgorithmException {
MessageDigest digest = java.security.MessageDigest.getInstance("MD5");
digest.update(_plaintext.getBytes());
byte[] hash = digest.digest();
BigInteger bigInt = new BigInteger(1, hash);
return String.format("%1$032X", bigInt).toLowerCase();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment