I have an application that needs to download data, for that the IMEI
device data has been previously registered in the base, it has worked well the problem is that now that I use a device with API 27 IMEI
it returns null.
public String getIMEINumber() {
String IMEINumber = "";
if (ActivityCompat.checkSelfPermission(this,
android.Manifest.permission.READ_PHONE_STATE) ==
PackageManager.PERMISSION_GRANTED) {
TelephonyManager telephonyMgr = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
IMEINumber = telephonyMgr.getImei();
} else {
IMEINumber = telephonyMgr.getDeviceId();
}
}
return IMEINumber;
}
I have read and I see that the documentation says that it returns null if IMEI
it is not available, I wonder in what cases it is not available? (if I have added the permissions in the MANIFEST)
There are several situations why you could not get the IMEI value, the first is that the permission is necessary
but for versions greater than 6.0 you must require the permission manually.
If you are using an emulator it may not get an IMEI value.
If you try to get an IMEI on a non-phone device like a tablet, you won't get an IMEI.
Sometimes it returns a
TelephonyManger.getDeviceId()
null value, so if you want one identifier for all your devices, I can recommend this method where, if the returned value is null (usinggetImei()
orgetDeviceId()
) also useSettings.Secure.ANDROID_ID
which returns an ID as a unique hexadecimal string of 64 bit:The cause may be that the device does not have an IMEI, since only devices that support telephone line (for normal calls and SMS) have this identifier (IMEI). For example some tablets do not support telephone line (IMEI).
If you do not have the device with you to verify that it supports a telephone line, you can search for it on the web by model and brand.
If you confirm that this is the problem and your application requires this Identifier (IMEI), you should exclude these devices from those supported by your application.