Android Question getdeviceid doesn't work in some latest mobiles?

asales

Well-Known Member
Licensed User
Try this function:
B4X:
Sub GetDeviceId As String
    Dim r As Reflector
    Dim Api As Int
    Dim id As String
    Dim p As Phone
    
    Api = r.GetStaticField("android.os.Build$VERSION", "SDK_INT")
    
    If Api < 9 Then
        'Old device
        If File.Exists(File.DirInternal, "__id") Then
           id = File.ReadString(File.DirInternal, "__id")
        Else
           id = Rnd(0x10000000, 0x7FFFFFFF)
           File.WriteString(File.DirInternal, "__id", id)
        End If
    Else
        'New device
        id = r.GetStaticField("android.os.Build", "SERIAL")
        'device doesn't implement the API properly
        If id.ToLowerCase = "unknown" Then id = p.GetSettings("android_id") 
    End If
    
    Return id
End Sub
 
Top