Skip to content Skip to sidebar Skip to footer

How To Get Unique Device Numer In Android?

I have tried to these methods such as IMEI,MEID,mac address,android_id, but all not OK. How to get unique device numer in Android?

Solution 1:

There are several Unique Identifiers available for Android devices

IMEI

TelephonyManagerTelephonyMgr= (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
Stringimei= TelephonyMgr.getDeviceId();

permission

android.permission.READ_PHONE_STATE

WLAN MAC Address

WifiManagerwifi= (WifiManager)getSystemService(Context.WIFI_SERVICE);
StringwanMAC= wifi .getConnectionInfo().getMacAddress();

permission

android.permission.ACCESS_WIFI_STATE

Bluetooth MAC Address

BluetoothAdapterbluetoothAdapter=BluetoothAdapter.getDefaultAdapter();
StringbluetoothMAC= bluetoothAdapter .getAddress();

permission

android.permission.BLUETOOTH

Android ID

StringandroidID= Secure.getString(getContentResolver(), Secure.ANDROID_ID);

Solution 2:

As the requirement for most of the applications is to identify a particular installation and not a physical device, a good solution to get the unique id for a user if to use UUID class. The following solution has been presented by Reto Meier from Google in a Google I/O presentation :

privatestaticStringuniqueID=null;
privatestaticfinalStringPREF_UNIQUE_ID="PREF_UNIQUE_ID";
publicsynchronizedstatic String id(Context context) {
   if (uniqueID == null) {
      SharedPreferencessharedPrefs= context.getSharedPreferences(
         PREF_UNIQUE_ID, Context.MODE_PRIVATE);
      uniqueID = sharedPrefs.getString(PREF_UNIQUE_ID, null);
      if (uniqueID == null) {
         uniqueID = UUID.randomUUID().toString();
         Editoreditor= sharedPrefs.edit();
     editor.putString(PREF_UNIQUE_ID, uniqueID);
     editor.commit();
     }
   }
   return uniqueID;
}

UUID.randomUUID() method generates an unique identifier for a specific installation. You have just to store that value and your user will be identified at the next launch of your application. You can also try to associate this solution with Android Backup service to keep the information available to the user even if he installs your application on the other device.

You can explore more at https://medium.com/@ssaurel/how-to-retrieve-an-unique-id-to-identify-android-devices-6f99fd5369eb

Solution 3:

In Android 10 you can't have access to non resettable device ids, like IMEI ,MEID etc.

So I think your best chance is creating a UUID. You can do that using the code below and saving this UUID into database or preference file.

StringuniqueID= UUID.randomUUID().toString();

As a random number, everytime you call this method it'll return a different code, and I think is not you are looking for.

To get a "unique ID" from UUID, the best choise is the code below.It will work fine, but the documentation ask to avoid using Android_ID too.

StringandroidId= Settings.Secure.getString(context.getContentResolver(),
            Settings.Secure.ANDROID_ID);

   UUIDandroidId_UUID= UUID
            .nameUUIDFromBytes(androidId.getBytes("utf8"));

This code will give you a almost unique code because if the user do a factory reset, this number would be changed.

I'm looking for another way to get a unique ID but, until now, I couldn't find.

Here ar some Android Developers documentation that should be usefull. Good pratices to define a unique ID https://developer.android.com/training/articles/user-data-ids

How to define a unique Id based in you use case:

https://developer.android.com/training/articles/user-data-ids#common-use-cases

I hope this could help someone.

Solution 4:

Below code is I used in my project

To get Unique Android Id

staticStringandroidID= Settings.Secure.getString(MyApplication.getContext().getContentResolver(), Settings.Secure.ANDROID_ID);

I used this as a global so i can use anywhere in my project

Solution 5:

To retrieve the unique ID associated to your device, you can use the following code :

import android.telephony.TelephonyManager;
import android.content.Context;
// ...
 TelephonyManager telephonyManager;
  telephonyManager = (TelephonyManager) getSystemService(Context.
                TELEPHONY_SERVICE);
        /*
      * getDeviceId() returns the unique device ID.
       * For example,the IMEI for GSM and the MEID or ESN for CDMA phones.
         */String deviceId = telephonyManager.getDeviceId();
         /*
       * getSubscriberId() returns the unique subscriber ID,
         * For example, the IMSI for a GSM phone.
          */String subscriberId = telephonyManager.getSubscriberId();

Secure Android ID

StringandroidId= Settings.Secure.getString(getContentResolver(),
                 Settings.Secure.ANDROID_ID);

Post a Comment for "How To Get Unique Device Numer In Android?"