Android Question Extract Serial Number from a Galaxy S8 with Android 9

Jose Cuevas

Member
Licensed User
Hi, I have an App that extracts the serial number from Samsung devices, everything works fine, but when the Galaxy S8 were updated to Android 9, it stopped working.

This is our code:

B4X:
Sub GetManufacturerSerialNumber As String

    Dim sn As String
    Dim r As Reflector
    Dim m As Object
    Dim Types(1) As String

    Types(0) = "java.lang.String"
    r.target = r.CreateObject("android.os.SystemProperties")

    m = r.GetMethod("get", Types)
    sn = r.InvokeMethod(r.Target,m,Array("ril.serialnumber"))
 
    Return sn

End Sub
The App stops exactly in this line: r.target = r.CreateObject("android.os.SystemProperties")

This is the Serial Number that I need to extract:



And this is the Galaxy S8 info:



Thanks in advance.
 

Jose Cuevas

Member
Licensed User
Thank you very much for your help, this is a requirement from our client, but he will have to choose between the AdvertisingID or IMEI, to identify the devices.
 

DonManfred

Expert
Licensed User
My previous answer was not 100% correct.
It was as in Post #1 he only mentioned that he need to get the Serial (;)). No word about IMEI. He added this requirement in #4.

Edit: ohh, no. The IMEI is another available ID we can say...

Sorry, ignore my comment please
 

Star-Dust

Expert
Licensed User
Still, it is not correct to say that the advertising id is the only available id.
I remember the MAC ADDR of the wifi that can be used as an identifier. Now I don't know if it's possible to get it.

However I prefer the Google one for many reasons, even if the user could change it on request from the settings menu.
 

Jose Cuevas

Member
Licensed User
We already tested PhoneId.GetDeviceId with Android 9, and it works perfectly.

We are now in the process of convincing our client, that the serial number no longer works and the alternative is the IMEI.

Thank you guys for your help.
 

KZero

Active Member
Licensed User
i'm using this code for long time and it still working (tested on samsung S10 android 9)
it using SystemProperties so no dangerous permissions needed

B4X:
Sub GetDevoceSerial As String

    Dim JV As JavaObject
    JV.InitializeContext   
    Dim Result As String
    Dim alist As List
    alist.Initialize
    alist.AddAll(Array As String("ril.serialnumber","ro.serialno"))
    Dim str,K As String
    For i = 0 To alist.Size-1
        K=alist.Get(i)
        str = JV.RunMethod("gMS" ,Array(K))
        If str.Length>4 Then
            If (str <> "00000000000") And (str <> "0123456789ABCDEF") Then
                Result = str
                Exit
            End If
        End If
    Next
    Return(Result)
End Sub



#IF JAVA
import java.lang.reflect.Method;

public static String gMS(String K) {
  String serial = "-";
  try {
      Class<?> c = Class.forName("android.os.SystemProperties");
      Method get = c.getMethod("get", String.class, String.class);
      serial = (String) get.invoke(c, K, "-");
  } catch (Exception ignored) {}
  return serial;
}
#End If
 

Jose Cuevas

Member
Licensed User
Hi KZero, we tested your code, but stops in this line: str = JV.RunMethod("gMS" ,Array(K)). The error message is this:

cys_getdevoceserial (B4A line: 1993)
str = JV.RunMethod("gMS" ,Array(K))
java.lang.RuntimeException: Method: gMS not found in: CYS.RUTEO.main
at anywheresoftware.b4j.object.JavaObject$MethodCache.getMethod(JavaObject.java:366)
at anywheresoftware.b4j.object.JavaObject.RunMethod(JavaObject.java:119)
at CYS.RUTEO_getdevoceserial(cys.java:2590)
at CYS.RUTEO_parametrosiniciales(cys.java:3479)
at CYS.RUTEOmain._activity_create(main.java:1066)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:186)
at CYS.RUTEOmain.afterFirstLayout(main.java:102)
at CYS.RUTEOmain.access$000(main.java:17)
at CYS.RUTEOmain$WaitForLayout.run(main.java:80)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6944)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)

Thanks in advance for your help.

i'm using this code for long time and it still working (tested on samsung S10 android 9)
it using SystemProperties so no dangerous permissions needed

B4X:
Sub GetDevoceSerial As String

    Dim JV As JavaObject
    JV.InitializeContext  
    Dim Result As String
    Dim alist As List
    alist.Initialize
    alist.AddAll(Array As String("ril.serialnumber","ro.serialno"))
    Dim str,K As String
    For i = 0 To alist.Size-1
        K=alist.Get(i)
        str = JV.RunMethod("gMS" ,Array(K))
        If str.Length>4 Then
            If (str <> "00000000000") And (str <> "0123456789ABCDEF") Then
                Result = str
                Exit
            End If
        End If
    Next
    Return(Result)
End Sub



#IF JAVA
import java.lang.reflect.Method;

public static String gMS(String K) {
  String serial = "-";
  try {
      Class<?> c = Class.forName("android.os.SystemProperties");
      Method get = c.getMethod("get", String.class, String.class);
      serial = (String) get.invoke(c, K, "-");
  } catch (Exception ignored) {}
  return serial;
}
#End If
 

KZero

Active Member
Licensed User
Hi KZero, we tested your code, but stops in this line: str = JV.RunMethod("gMS" ,Array(K)). The error message is this:

cys_getdevoceserial (B4A line: 1993)
str = JV.RunMethod("gMS" ,Array(K))
java.lang.RuntimeException: Method: gMS not found in: CYS.RUTEO.main
at anywheresoftware.b4j.object.JavaObject$MethodCache.getMethod(JavaObject.java:366)
at anywheresoftware.b4j.object.JavaObject.RunMethod(JavaObject.java:119)
at CYS.RUTEO_getdevoceserial(cys.java:2590)
at CYS.RUTEO_parametrosiniciales(cys.java:3479)
at CYS.RUTEOmain._activity_create(main.java:1066)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:186)
at CYS.RUTEOmain.afterFirstLayout(main.java:102)
at CYS.RUTEOmain.access$000(main.java:17)
at CYS.RUTEOmain$WaitForLayout.run(main.java:80)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6944)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)

Thanks in advance for your help.
did you copied the java code ?

B4X:
#IF JAVA
import java.lang.reflect.Method;

public static String gMS(String K) {
  String serial = "-";
  try {
      Class<?> c = Class.forName("android.os.SystemProperties");
      Method get = c.getMethod("get", String.class, String.class);
      serial = (String) get.invoke(c, K, "-");
  } catch (Exception ignored) {}
  return serial;
}
#End If
 

Jose Cuevas

Member
Licensed User
Hi KZero, my initial test was with my actual Project, so, I create a fresh Project and your code works like a charm.

Thank you very much.


i'm using this code for long time and it still working (tested on samsung S10 android 9)
it using SystemProperties so no dangerous permissions needed

B4X:
Sub GetDevoceSerial As String

    Dim JV As JavaObject
    JV.InitializeContext  
    Dim Result As String
    Dim alist As List
    alist.Initialize
    alist.AddAll(Array As String("ril.serialnumber","ro.serialno"))
    Dim str,K As String
    For i = 0 To alist.Size-1
        K=alist.Get(i)
        str = JV.RunMethod("gMS" ,Array(K))
        If str.Length>4 Then
            If (str <> "00000000000") And (str <> "0123456789ABCDEF") Then
                Result = str
                Exit
            End If
        End If
    Next
    Return(Result)
End Sub



#IF JAVA
import java.lang.reflect.Method;

public static String gMS(String K) {
  String serial = "-";
  try {
      Class<?> c = Class.forName("android.os.SystemProperties");
      Method get = c.getMethod("get", String.class, String.class);
      serial = (String) get.invoke(c, K, "-");
  } catch (Exception ignored) {}
  return serial;
}
#End If
 

Indic Software

Member
Licensed User
Hello,

We have been using following JAVA code in another development in all our apps:
B4X:
import android.content.Context;
    import android.content.Intent;
   
    import android.provider.Settings.Secure;
   
    public static String getAndroidID()
    {
        Context context = getApplicationContext();
       
        String android_id = Secure.getString(context.getContentResolver(),Secure.ANDROID_ID);
       
        return android_id ;
   
    }
The surprising thing is that it is working on all phones that we tested. We tested old phones with Android 5 and new phones with Android 9.

See if it this code works for you.
 
Top