Android Question java.lang.SecurityException: getDeviceId

Discussion in 'Android Questions' started by marcick, Jun 6, 2019.

  1. marcick

    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 ....
     
  2. Erel

    Erel Administrator Staff Member Licensed User

    Where is the relevant code?
     
  3. marcick

    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
     
  4. Erel

    Erel Administrator Staff Member Licensed User

    Probably the same as PhoneId.GetDeviceId. You must request the READ_PHONE_STATE permission at runtime.
     
  5. marcick

    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.
     
  6. marcick

    marcick Well-Known Member Licensed User

    This is the code that fail:

    Code:
    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;
      }
     
  7. Erel

    Erel Administrator Staff Member Licensed User

  8. marcick

    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 ?
     
    jose luis gudino likes this.
  9. DonManfred

    DonManfred Expert Licensed User

    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.
     
  10. jose luis gudino

    jose luis gudino Member Licensed User

    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
     
  11. marcick

    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.
     
  12. jose luis gudino

    jose luis gudino Member Licensed User

    if it is true, we better change at this time so as not to have problems in the future,
    thanks for your time.
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice