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

 

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




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
Cookies are required to use this site. You must accept them to continue using the site. Learn more…