1. *** New version of B4J is available ***
    B4J v7.8
    Dismiss Notice

Android Tutorial Android Device Unique ID - Alternative to PhoneId

Discussion in 'Tutorials & Examples' started by Erel, Feb 7, 2012.

  1. Erel

    Erel Administrator Staff Member Licensed User

    This tutorial is based on the following blog post: Identifying App Installations | Android Developers Blog

    The standard way to get a unique id is using PhoneId.GetDeviceId. However this method has several disadvantages:
    - It requires the "android.permission.READ_PHONE_STATE" permission which is quite a sensitive permission.
    - It doesn't work on devices without phone functionality.
    - Apparently there is a bug in some devices which return the same id.

    Starting from Android 2.3 there is a new field that returns a unique id and should work on all devices (without requiring any permission).

    A general solution is to use this field for modern devices and to use a "fake" id for older devices. The fake id is randomly created when the application first runs.

    Here is the code (requires the Reflection library):
    Sub Activity_Create(FirstTime As Boolean)
    End Sub

    Sub GetDeviceId As String
    Dim r As Reflector
    Dim Api As Int
       Api = r.GetStaticField(
    If Api < 9 Then
    'Old device
          If File.Exists(File.DirInternal, "__id"Then
    Return File.ReadString(File.DirInternal, "__id")
    Dim id As Int
    id = Rnd(0x100000000x7FFFFFFF)
    File.WriteString(File.DirInternal, "__id"id)
    Return id
    End If
    'New device
          Return r.GetStaticField("android.os.Build""SERIAL")
    End If
    End Sub
    ilan, koaunglay, cyiwin and 4 others like this.
  2. COBRASoft

    COBRASoft Active Member Licensed User

    Erel: this returns 'unknown' on my SE Xperia Arc S. Any idea?
  3. Erel

    Erel Administrator Staff Member Licensed User

    Not really. Apparently your device doesn't implement this API properly.
  4. nad

    nad Active Member Licensed User

    Very interesting read. Thanks erel.
    Last edited: Feb 20, 2012
  5. timo

    timo Active Member Licensed User

    With Api < 9 the problem is that when a user reinstall the app a new random number is generated. If you have a trial app running x times, it is so easy bypassed.

    Attached Files:

    Last edited: Apr 5, 2012
  6. nad

    nad Active Member Licensed User

    One of my apps was being cracked and is not anymore. I added a delay of one minute between license checks to avoid that so after a failure any try to check license under a minute fails with ExitApplication and some other trcks. Maybe that helped (or they just didn't care enough to crack posterior updates)

    Sent from my GT-I9100 using Tapatalk 2
  7. alexposta2

    alexposta2 Member Licensed User

    On my huawei mediapad7 return unknow

    android 3.2
  8. gxlujd

    gxlujd Member Licensed User

    My Phone is Lenovo P700,android 4.0.3,return '123456789ABCDEF',My android Pad version 4.0.3 return 'unknown'.

    Do not seem right:confused:
  9. keirS

    keirS Well-Known Member Licensed User

    Erel that Blog post recommends using android_id and not the serial number. Perhaps a mixture of all 4 would be best?

    If a Phone use the Phone ID. If no phone id use the Serial Number and the android_Id; as last resort try and get the mac address and if that fails generate a UID.
  10. Erel

    Erel Administrator Staff Member Licensed User

    Seems like they updated this blog post. You can fetch the android_id value with Phone.GetSettings("android_id")
    luke2012 likes this.
  11. NeoTechni

    NeoTechni Well-Known Member Licensed User

    I cheated. For my star trek app, I just generation a starship registry ID

    ie: NCC-1701-D
    Peter Simpson likes this.
  12. magicuser68

    magicuser68 Member Licensed User

    I am using the following with success for a trial app. I am using Parse to store the information, so far I see unique ids coming in.


    Sub GetDeviceId As String
    Dim p As Phone
    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 = p.GetSettings("android_id")
    Return id
    'New device
    id = r.GetStaticField("android.os.Build", "SERIAL")
    If id.ToLowerCase = "unknown" Then
    id = p.GetSettings("android_id")
    End If
    Return id
    End If
    End Sub
  13. Roger Garstang

    Roger Garstang Well-Known Member Licensed User

    Code Formatting helps it be easier to read:

    Sub GetDeviceId As String
    Dim p As Phone
    Dim id As String
    Dim r As Reflector
    Dim Api As Int

       Api = r.GetStaticField(
    If Api < 9 Then
    'Old device
          id= p.GetSettings("android_id")
    'New device
          id= r.GetStaticField("android.os.Build""SERIAL")
    If id.ToLowerCase = "unknown" Then id= p.GetSettings("android_id")
    End If
    Return id
    End Sub
    ilan, rraswisak and Peter Simpson like this.
  14. wheretheidivides

    wheretheidivides Active Member Licensed User

    So, this is dumb question. How do you get this to tell what device is being used for the licensing key? Also, I get device ID: 'unkown' for my Samsung galaxy s epic 4g.
    Last edited: Feb 10, 2013
  15. Erel

    Erel Administrator Staff Member Licensed User

    Call GetDeviceId instead of p.GetDeviceId (see the licensing example).
  16. rgately

    rgately Member Licensed User

    What should I do with the ID number?

    I see how to get the unique number from each device but I don't understand what to do with the number. Please give me some ideas of how to use the number in a copy protection scheme. Thanks!
  17. JoanRPM

    JoanRPM Active Member Licensed User

    I use it to identify the device in a http server query .
    So the server recognizes which device does the query.
    Initially, when you install the program, each device informs the server of its ID number, user name, etc..

  18. rgately

    rgately Member Licensed User

    JoanRPM thank you, your response helps.

    I think I'm headed towards a scenario that looks like this...

    1) When the app starts it checks the device ID and looks for a licence number in a file somewhere.

    2) If the license file does not exist then an input box pops up asking for a license number and creates a license file.

    I'm not sure how to use these two numbers together in order to protect an application. Perhaps if the license number matches the device ID?

    Thanks again
  19. JoanRPM

    JoanRPM Active Member Licensed User


    Yes, something like that.

    I use a external database, which is on a server. In this DB are all the ID's of the users who initially were recorded.
    At application startup, the server asked if this user are already registered. If so, we can proceed to request more data, open the application, etc.

    If you just want access to the server once to enable the program, what I do is, after obtaining the agreement of the server, save in a file an encrypted ID with some internal key. When you start the app, look if this file exists and if it is correct.

  20. joneden

    joneden Active Member Licensed User

    Coming in on an old thread but it's to do with the android_id method so appropriate.

    Using code as below I can get a nicely scrambled ID, unique to my set of devices at least. BUT if I have to redo a unit's ROM or do a factory reset on a unit then I get a new number.

    Other than an IMEI number which you don't get on some devices such as WIFI only tablets, is there any other way to get a persistent unique ID?



    Dim objPhone As Phone 
       DeviceID = objPhone.GetSettings(
    Peter Simpson and Powie like this.
  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