Skip to content

Instantly share code, notes, and snippets.

@hatamiarash7
Created March 26, 2018 16:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hatamiarash7/ae371bdc57245e305c63c752c6a4b4c8 to your computer and use it in GitHub Desktop.
Save hatamiarash7/ae371bdc57245e305c63c752c6a4b4c8 to your computer and use it in GitHub Desktop.
Android Generate Unique ID
@SuppressWarnings("deprecation")
@SuppressLint("HardwareIds")
public static HashMap<String, String> GenerateDeviceInformation(Context context) {
HashMap<String, String> result = new HashMap<>();
String pseudoId = "35" +
Build.BOARD.length() % 10 +
Build.BRAND.length() % 10 +
Build.CPU_ABI.length() % 10 +
Build.DEVICE.length() % 10 +
Build.DISPLAY.length() % 10 +
Build.HOST.length() % 10 +
Build.ID.length() % 10 +
Build.MANUFACTURER.length() % 10 +
Build.MODEL.length() % 10 +
Build.PRODUCT.length() % 10 +
Build.TAGS.length() % 10 +
Build.TYPE.length() % 10 +
Build.USER.length() % 10;
String androidId = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
String btId = "";
if (bluetoothAdapter != null)
btId = bluetoothAdapter.getAddress();
String longId = pseudoId + androidId + btId;
try {
MessageDigest messageDigest = MessageDigest.getInstance("MD5");
messageDigest.update(longId.getBytes(), 0, longId.length());
byte md5Bytes[] = messageDigest.digest();
StringBuilder identifier = new StringBuilder();
for (byte md5Byte : md5Bytes) {
int b = (0xFF & md5Byte);
if (b <= 0xF)
identifier.append("0");
identifier.append(Integer.toHexString(b));
}
identifier = new StringBuilder(identifier.toString().toUpperCase());
String mDeviceName = DeviceName.getDeviceName();
TelephonyManager manager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
String carrierName = manager.getNetworkOperatorName();
result.put("unique_id", identifier.toString());
result.put("name", mDeviceName);
result.put("os_name", System.getProperty("os.name"));
result.put("os_version", System.getProperty("os.version"));
result.put("version_release", android.os.Build.VERSION.RELEASE);
result.put("device", android.os.Build.DEVICE);
result.put("model", android.os.Build.MODEL);
result.put("product", android.os.Build.PRODUCT);
result.put("brand", android.os.Build.BRAND);
result.put("display", android.os.Build.DISPLAY);
result.put("abi", android.os.Build.CPU_ABI);
result.put("abi2", android.os.Build.CPU_ABI2);
result.put("unknown", android.os.Build.UNKNOWN);
result.put("hardware", android.os.Build.HARDWARE);
result.put("id", android.os.Build.ID);
result.put("manufacturer", android.os.Build.MANUFACTURER);
result.put("serial", android.os.Build.SERIAL);
result.put("user", android.os.Build.USER);
result.put("host", android.os.Build.HOST);
result.put("carrier", carrierName);
return result;
} catch (Exception e) {
Log.e("TAG", e.toString());
}
result.put("id", "null");
result.put("name", "null");
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment