Reputation: 3213
I'm developing an app in which I'm gonna need the unique ID of a device.
This post: Is there a unique Android device ID? first leads me to consider using ANDROID_ID
. But I need a reliable solution for both tablets and phones, with or without any google account/software on it, like in China where most of its services are blocked. I'm therefore looking for an other solution.
Important: for security reasons, this id must be safe from any change. Of course when the device is rooted or when someone really want to mess with the app, it's impossible, but at least for the average users. Also, the Mac address
and the TelephonyManager.getDeviceId()
are not reliable.
As I'm targeting both phones and tablets, with or without Google, is it a way to get a unique device Id unmodifiable for each device?
Upvotes: 1
Views: 1839
Reputation: 348
You can use the Android unique serial number it is generated when the device is booted for the first time and remains same for the lifetime.
Settings.Secure.getString(getContext().getContentResolver(), Settings.Secure.ANDROID_ID)
Upvotes: 0
Reputation: 1711
I am sorry that I flagged this question before fully understanding it. Like @Johann said considering rooted users into the equation will make it a lot more difficult to identify the device.
Using the assumption that your application uses webservice, user must connect to internet in some way i.e. either by data plan(users with sim card) or by wifi.
So combining id's from both the hardware and generating a unique hashcode of the resulting string, you should be able to uniquely identify the device(in theory i.e). But I haven't tested this code, so check if works for you.
final TelephonyManager tm = (TelephonyManager) getBaseContext().getSystemService(Context.TELEPHONY_SERVICE);
final String tmDevice, tmSerial, androidId, wifi;
tmDevice = "" + tm.getDeviceId();
tmSerial = "" + tm.getSimSerialNumber();
androidId = "" + android.provider.Settings.Secure.getString(getContentResolver(), android.provider.Settings.Secure.ANDROID_ID);
WifiManager manager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
wifi = ""+ manager.getConnectionInfo().getMacAddress();
String unique_id = makeSHA1Hash(tmDevice + tmSerial + androidId + wifi);
makeSHA1Hash
public String makeSHA1Hash(String input)
throws NoSuchAlgorithmException, UnsupportedEncodingException
{
MessageDigest md = MessageDigest.getInstance("SHA1");
md.reset();
byte[] buffer = input.getBytes("UTF-8");
md.update(buffer);
byte[] digest = md.digest();
String hexStr = "";
for (int i = 0; i < digest.length; i++) {
hexStr += Integer.toString( ( digest[i] & 0xff ) + 0x100, 16).substring( 1 );
}
return hexStr;
}
Hope it helps you...
Upvotes: 1
Reputation: 1531
After the device is rooted, the IMEI numbers and the MAC/WiFi address can be modified. Moreover, from Android Marshmallow, the user may revoke the permission for WiFi/MAC address and Bluetooth address. If you try to retrieve those values, you will be presented with a simple dummy ID, which remains common across all Marshmallow devices.
Check this link
Upvotes: 0
Reputation: 604
I think it would be good to identify devices by using Mac address o Wi-Fi module. It's quite unique even on cheap chinese devices. Take a look at WifiInfo class: http://developer.android.com/reference/android/net/wifi/WifiInfo.html
Should works fine for device identification.
Upvotes: 0
Reputation: 505
you can use the device imei number.
You want to call-
android.telephony.TelephonyManager.getDeviceId().
You'll need the following permission in your AndroidManifest.xml:
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
Upvotes: 0
Reputation: 26054
You can use the wifi mac address:
private String getWifiMacAddress() {
WifiManager manager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
WifiInfo info = manager.getConnectionInfo();
return info.getMacAddress();
}
Upvotes: 0