Will TelephonyManger.getDeviceId() return device id for Tablets like Galaxy Tab...?
Asked Answered
H

3

16

I want to get the device id that will be unique for each Android device. I am presently developing for a Tablet device. Want to get unique device id and store the corresponding values...

So, i want to know whether Tablet devices will return a value if i use TelephonyManager.getDeviceId()...???Or is there any other value that is unique for each device???

Heliotropism answered 27/9, 2010 at 9:46 Comment(1)
Dupe: #2785985Resonance
P
9

This is not a duplicate question. As it turns out, Google's CTS require that getPhoneType of TelephonyManager needs to be none and getDeviceId of TelephonyManager needs to be null for non-phone devices.

So to get IMEI, please try to use:

String imei = SystemProperties.get("ro.gsm.imei")

Unfortunately, SystemProperties is a non-public class in the Android OS, which means it isn't publicly available to regular applications. Try looking at this post for help accessing it: Where is android.os.SystemProperties

Proline answered 10/6, 2011 at 15:7 Comment(0)
C
61

TelephonyManger.getDeviceId() Returns the unique device ID, for example, the IMEI for GSM and the MEID or ESN for CDMA phones.

final TelephonyManager mTelephony = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);            
String myAndroidDeviceId = mTelephony.getDeviceId(); 

But i recommend to use:

Settings.Secure.ANDROID_ID that returns the Android ID as an unique 64-bit hex string.

    String   myAndroidDeviceId = Secure.getString(getApplicationContext().getContentResolver(), Secure.ANDROID_ID); 

Sometimes TelephonyManger.getDeviceId() will return null, so to assure an unique id you will use this method:

public String getUniqueID(){    
    String myAndroidDeviceId = "";
    TelephonyManager mTelephony = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
    if (mTelephony.getDeviceId() != null){
        myAndroidDeviceId = mTelephony.getDeviceId(); 
    }else{
         myAndroidDeviceId = Secure.getString(getApplicationContext().getContentResolver(), Secure.ANDROID_ID); 
    }
    return myAndroidDeviceId;
}
Counterbalance answered 16/11, 2011 at 0:37 Comment(6)
This solution has a problem: when the user switches to airplane mode, the telephony information disappears and the method will return the Secure.ANDROID_ID value. So the getUniqueID() method may be returning different values for different states of the same device.Farlay
@TedHopp, checked on several devices - in airplane mode getUniqueID() method is returning the same valueScoff
why is ANDROID_ID returning 16 digit id for me, instead of 64 digit hex @ElenasysVet
So why not just use Secure.ANDROID_ID always?Watts
ANDROID_ID is generated when user sets up the device. If the device is reset again, the id might change.Apposition
you should add try catch to avoid crash on Android 6+ runtime permissionCede
P
9

This is not a duplicate question. As it turns out, Google's CTS require that getPhoneType of TelephonyManager needs to be none and getDeviceId of TelephonyManager needs to be null for non-phone devices.

So to get IMEI, please try to use:

String imei = SystemProperties.get("ro.gsm.imei")

Unfortunately, SystemProperties is a non-public class in the Android OS, which means it isn't publicly available to regular applications. Try looking at this post for help accessing it: Where is android.os.SystemProperties

Proline answered 10/6, 2011 at 15:7 Comment(0)
D
2

Since Android 8 everything's changed. You should use Build.getSerial(), to get the serial number of the device and add the permission READ_PHONE_STATE.

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    serial = Build.getSerial(); // Requires permission READ_PHONE_STATE
} else {
    serial = Build.SERIAL; // Will return 'unknown' for device >= Build.VERSION_CODES.O
}

And get the IMEI or MEID this way:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    String imei = tm.getImei(); // Requires permission READ_PHONE_STATE
    serial = imei == null ? tm.getMeid() : imei; // Requires permission READ_PHONE_STATE
} else {
    serial = tm.getDeviceId(); // Requires permission READ_PHONE_STATE
}
Doubly answered 7/12, 2017 at 19:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.