Android Question java.lang.SecurityException: getDeviceId

marcick

Well-Known Member
Licensed User
Hello,
I have a PlayStore app that is running ok since some years.
Today I'm experiencing a new issue on a Huawey tablet after it has been updated to Android 9.

Trying to read the device IMEI returns the error in the subject and the app crash.
The code works ok in debug and in release also. The problem only appears if the app is downloaded from the store.

Anybody has this issue ? Hope it is not Trump related ....
 

marcick

Well-Known Member
Licensed User
Right ....
It's inside a library that a guys developed for me in the past, so I'm not sure which java code is.
I'll investigate and be back
 

marcick

Well-Known Member
Licensed User
mmmhhh...
I already request READ_PHONE_STATE permission at runtime.
Also, PhoneId.GetDeviceId does not return the IMEI as my library does.
I'm trying to get in touch with the developer.
 

marcick

Well-Known Member
Licensed User
This is the code that fail:

B4X:
  public static String GetDeviceId(BA pBA)
  {
    TelephonyManager telephonyManager = (TelephonyManager)pBA.context.getSystemService("phone");
    String deviceID = telephonyManager.getDeviceId();
    try
    {
      MessageDigest digest = MessageDigest.getInstance("MD5");
      digest.update(deviceID.getBytes());
      byte[] messageDigest = digest.digest();
      
      StringBuffer hexString = new StringBuffer();
      int i = 0;
      for (int j = messageDigest.length; i < j; i++)
      {
        String h = Integer.toHexString(0xFF & messageDigest[i]);
        while (h.length() < 2) {
          h = "0" + h;
        }
        hexString.append(h);
      }
      deviceID = hexString.toString();
    }
    catch (NoSuchAlgorithmException e)
    {
      e.printStackTrace();
    }
    return deviceID;
  }
 

marcick

Well-Known Member
Licensed User
PhoneId.GetDeviceId returns a number, I see that my code convert it in HEX format, but this is not a problem, ok.

All my apps (hundreds of customers) are registered on my server with their IMEI and I use IMEI to identify cisomer, to send notification.
If suddenly the app can't read IMEI I must force the customer to register again with another code.
If there is no other solution I'll do it. But reading the link above, it doesn't look to me that it is no longer possible to read IMEI, it's enough you ask runtime permission and I'm doing it. Am I wrong ?
 

DonManfred

Expert
Licensed User
But reading the link above, it doesn't look to me that it is no longer possible to read IMEI, it's enough you ask runtime permission and I'm doing it. Am I wrong ?
Note: If your app is the device or profile owner app, you need only the READ_PHONE_STATE permission to access non-resettable device identifiers, even if your app targets Android Q. Also, if your app has special carrier permissions, you don't need any permissions to access the identifiers.

Make your app the device ownerapp (kiosk App) and you probably get the IMEI.

It is not possible anymore for normal apps.
 

jose luis gudino

Member
Licensed User
PhoneId.GetDeviceId returns a number, I see that my code convert it in HEX format, but this is not a problem, ok.

All my apps (hundreds of customers) are registered on my server with their IMEI and I use IMEI to identify cisomer, to send notification.
If suddenly the app can't read IMEI I must force the customer to register again with another code.
If there is no other solution I'll do it. But reading the link above, it doesn't look to me that it is no longer possible to read IMEI, it's enough you ask runtime permission and I'm doing it. Am I wrong ?
hello I have exactly the same problem, I have 4000 applications and to control the expiration date I use the IMEI the annual expiration of the application, that will change my whole structure
 

marcick

Well-Known Member
Licensed User
It's a long time we read that sooner or later it will not be possible to read the IMEI.
Actually it still works but .. how long it's a big question mark.
I'm planning to switch to the advertising id as Erel suggested.
 
Top