Android Question targetsdk 28 get device id

Discussion in 'Android Questions' started by PassionDEV, Apr 21, 2019.

  1. PassionDEV

    PassionDEV Active Member Licensed User

    i am using this code to get device id

    Code:
    Sub GetDeviceId As String
        
    Dim id As String
        
    Dim r As Reflector
        
    Dim Api As Int
        Api = r.GetStaticField(
    "android.os.Build$VERSION""SDK_INT")
        
    If Api < 9 Then
            
    'Old device
            id= GetDeviceId
        
    Else
            
    'New device
            id= r.GetStaticField("android.os.Build""SERIAL")
            
    If id.ToLowerCase = "unknown" Then
                
    id= GetDeviceId
            
    End If
            
        
    End If
        
    Return id
    End Sub
    it was working on targetsdk 26 but after setting the manifest to targetsdk 28 i got this error

     
  2. PassionDEV

    PassionDEV Active Member Licensed User

    i manage to get it fixed like following

    Code:
    Sub Grabdevid As String
        
    Dim id As String
        
    Dim r As Reflector
        
    Dim Api As Int
        
    Dim pi As PhoneId
       
        Api = r.GetStaticField(
    "android.os.Build$VERSION""SDK_INT")
        
    If Api < 9 Then
            
    'Old device
            id= pi.GetDeviceId
        
    Else
            
    'New device
            id= r.GetStaticField("android.os.Build""SERIAL")
            
    If id.ToLowerCase = "unknown" Then
            
    id= pi.GetDeviceId
        
    End If
           
        
    End If
        
    Return id
    End Sub
    but serial always return unknown on my honor play device on sdk 26 serial retrieved without any problems what should i change ?
     
  3. PassionDEV

    PassionDEV Active Member Licensed User

    i get it work here is what i did

    Android Build.Serial is deprecated in sdk 26+ thats why the following return unknown

    Code:
    r.GetStaticField("android.os.Build""SERIAL")'unknown on api 26+
    i have tried to replace SERIAL with getSerial Like

    Code:
    id= r.GetStaticField("android.os.Build""getSerial")

    then i got this exception



    Then i decided to use javaobject as temporarily solution



    Code:
    'Private NativeMe As JavaObject i have declared in process_Global


    Sub GetDeviceId As String
        
    Dim id As String
        
    Dim r As Reflector
        
    Dim Api As Int
        
    Dim pi As PhoneId
     
        Api = r.GetStaticField(
    "android.os.Build$VERSION""SDK_INT")

        
    If Api < 9 Then
        
    'Old device
         
        
    id= pi.GetDeviceId
     
        
    Else if (Api >=9And (Api < 26Then
     
        
    id= r.GetStaticField("android.os.Build""SERIAL")
     
        
    Else If Api >= 26 Then
     
        
    id= NativeMe.RunMethod("getSerialNew"Null)
     
        
    End If
     
     
        
    If id.ToLowerCase = "unknown" Then
        
    id= pi.GetDeviceId
        
    End If
     
     
     
        
    Return id
    End Sub



    #If Java
    import java.lang.reflect.Method;
    import android.content.Context;
    import android.content.pm.PackageManager;
    import android.os.Build;
    import android.provider.Settings;
    import android.telephony.TelephonyManager;

    /**
       * Gets serial New.
       *
       * @return the serial
       */
      public String getSerialNew() {
        String result = initialVal;
        try {
          result = Build.getSerial();
        } catch (Exception e) {
          e.printStackTrace();
        }
        if (result == null || result.length() == 0) {
          result = initialVal;
        }
        return result;
      }


    #End If
     
    Last edited: Apr 21, 2019
  4. Erel

    Erel Administrator Staff Member Licensed User

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