Android Question targetsdk 28 get device id

Addo

Well-Known Member
Licensed User
Longtime User
i am using this code to get device id

B4X:
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

*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
main_vvvvvv6 (java line: 707)
java.lang.StackOverflowError: stack size 8MB
at java.lang.reflect.Field.get(Native Method)
at anywheresoftware.b4a.agraham.reflection.Reflection.GetStaticField(Reflection.java:373)
at b4a.example.main._vvvvvv6(main.java:707)
at b4a.example.main._vvvvvv6(main.java:718)
at b4a.example.main._vvvvvv6(main.java:718)
at b4a.example.main._vvvvvv6(main.java:718)
at b4a.example.main._vvvvvv6(main.java:718)
at b4a.example.main._vvvvvv6(main.java:718)
at b4a.example.main._vvvvvv6(main.java:718)
at b4a.example.main._vvvvvv6(main.java:718)
at b4a.example.main._vvvvvv6(main.java:718)
at b4a.example.main._vvvvvv6(main.java:718)
at b4a.example.main._vvvvvv6(main.java:718)
at b4a.example.main._vvvvvv6(main.java:718)
at b4a.example.main._vvvvvv6(main.java:718)
at b4a.example.main._vvvvvv6(main.java:718)
at b4a.example.main._vvvvvv6(main.java:718)
at b4a.example.main._vvvvvv6(main.java:718)
at b4a.example.main._vvvvvv6(main.java:718)
at b4a.example.main._vvvvvv6(main.java:718)
at b4a.example.main._vvvvvv6(main.java:718)
at b4a.example.main._vvvvvv6(main.java:718)
at b4a.example.main._vvvvvv6(main.java:718)
at b4a.example.main._vvvvvv6(main.java:718)
at b4a.example.main._vvvvvv6(main.java:718)
at b4a.example.main._vvvvvv6(main.java:718)
at b4a.example.main._vvvvvv6(main.java:718)
at b4a.example.main._vvvvvv6(main.java:718)
at b4a.example.main._vvvvvv6(main.java:718)
at b4a.example.main._vvvvvv6(main.java:718)
at b4a.example.main._vvvvvv6(main.java:718)
at b4a.example.main._vvvvvv6(main.java:718)
at b4a.example.main._vvvvvv6(main.java:718)
at b4a.example.main._vvvvvv6(main.java:718)
at b4a.example.main._vvvvvv6(main.java:718)
at b4a.example.main._vvvvvv6(main.java:718)
at b4a.example.main._vvvvvv6(main.java:718)
at b4a.example.main._vvvvvv6(main.java:718)
at b4a.example.main._vvvvvv6(main.java:718)
at b4a.example.main._vvvvvv6(main.java:718)
at b4a.example.main._vvvvvv6(main.java:718)
at b4a.example.main._vvvvvv6(main.java:718)
at b4a.example.main._vvvvvv6(main.java:718)
at b4a.example.main._vvvvvv6(main.java:718)
at b4a.example.main._vvvvvv6(main.java:718)
at b4a.example.main._vvvvvv6(main.java:718)
at b4a.example.main._vvvvvv6(main.java:718)
at b4a.example.main._vvvvvv6(main.java:718)
at b4a.example.main._vvvvvv6(main.java:718)
at b4a.example.main._vvvvvv6(main.java:718)
at b4a.example.main._vvvvvv6(main.java:718)
at b4a.example.main._vvvvvv6(main.java:718)
at b4a.example.main._vvvvvv6(main.java:718)
at b4a.example.main._vvvvvv6(main.java:718)
at b4a.example.main._vvvvvv6(main.java:718)
at b4a.example.main._vvvvvv6(main.java:718)
at b4a.example.main._vvvvvv6(main.java:718)
at b4a.example.main._vvvvvv6(main.java:718)
at b4a.example.main._vvvvvv6(main.java:718)
at b4a.example.main._vvvvvv6(main.java:718)
at b4a.example.main._vvvvvv6(main.java:718)
at b4a.example.main._vvvvvv6(main.java:718)
at b4a.example.main._vvvvvv6(main.java:718)
at b4a.example.main._vvvvvv6(main.java:718)
at b4a.example.main._vvvvvv6(main.java:718)
at b4a.example.main._vvvvvv6(main.java:718)
at b4a.example.main._vvvvvv6(main.java:718)
at b4a.example.main._vvvvvv6(main.java:718)
at b4a.example.main._vvvvvv6(main.java:718)
at b4a.example.main._vvvvvv6(main.java:718)
at b4a.example.main._vvvvvv6(main.java:718)
at b4a.example.main._vvvvvv6(main.java:718)
at b4a.example.main._vvvvvv6(main.java:718)
at b4a.example.main._vvvvvv6(main.java:718)
at b4a.example.main._vvvvvv6(main.java:718)
at b4a.example.main._vvvvvv6(main.java:718)
at b4a.example.main._vvvvvv6(main.java:718)
at b4a.example.main._vvvvvv6(main.java:718)
at b4a.example.main._vvvvvv6(main.java:718)
at b4a.example.main._vvvvvv6(main.java:718)
at b4a.example.main._vvvvvv6(main.java:718)
at b4a.example.main._vvvvvv6(main.java:718)
at b4a.example.main._vvvvvv6(main.java:718)
at b4a.example.main._vvvvvv6(main.java:718)
at b4a.example.main._vvvvvv6(main.java:718)
at b4a.example.main._vvvvvv6(main.java:718)
at b4a.example.main._vvvvvv6(main.java:718)
at b4a.example.main._vvvvvv6(main.java:718)
at b4a.e
Message longer than Log limit (4000). Message was truncated.
 

Addo

Well-Known Member
Licensed User
Longtime User
i manage to get it fixed like following

B4X:
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 ?
 
Upvote 0

Addo

Well-Known Member
Licensed User
Longtime User
i get it work here is what i did

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

B4X:
r.GetStaticField("android.os.Build", "SERIAL")'unknown on api 26+

i have tried to replace SERIAL with getSerial Like

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


then i got this exception


*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
main_vvvvvv6 (java line: 750)
java.lang.NoSuchFieldException: No field getSerial in class Landroid/os/Build; (declaration of 'android.os.Build' appears in /system/framework/framework.jar!classes2.dex)
at java.lang.Class.getDeclaredField(Native Method)
at anywheresoftware.b4a.agraham.reflection.Reflection.GetStaticField(Reflection.java:371)
at b4a.example.main._vvvvvv6(main.java:750)
at b4a.example.main$ResumableSub_Activity_Create.resume(main.java:612)
at anywheresoftware.b4a.BA.checkAndRunWaitForEvent(BA.java:250)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:190)
at anywheresoftware.b4a.BA$2.run(BA.java:370)
at android.os.Handler.handleCallback(Handler.java:891)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:207)
at android.app.ActivityThread.main(ActivityThread.java:7470)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:524)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:958)


Then i decided to use javaobject as temporarily solution



B4X:
'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 >=9) And (Api < 26) Then
 
    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:
Upvote 0
Top