Android Tutorial Android Device Unique ID - Alternative to PhoneId

Status
Not open for further replies.
This is an old tutorial. It will not work on new versions of Android. You should either generate a random value when the app starts for the first time or use the advertising id: https://www.b4x.com/android/forum/threads/advertising-id.101050/#content

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):
B4X:
Sub Activity_Create(FirstTime As Boolean)
   Log(GetDeviceId)
End Sub

Sub GetDeviceId 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
      If File.Exists(File.DirInternal, "__id") Then
         Return File.ReadString(File.DirInternal, "__id")
      Else
         Dim id As Int
         id = Rnd(0x10000000, 0x7FFFFFFF)
         File.WriteString(File.DirInternal, "__id", id)
         Return id
      End If
   Else
      'New device
      Return r.GetStaticField("android.os.Build", "SERIAL")
   End If
End Sub
 
Last edited:

nad

Active Member
Licensed User
Longtime User
Very interesting read. Thanks erel.
 
Last edited:

timo

Active Member
Licensed User
Longtime 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.
 

Attachments

  • reinstall.jpg
    reinstall.jpg
    27.6 KB · Views: 2,254
Last edited:

nad

Active Member
Licensed User
Longtime 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
 

gxlujd

Member
Licensed User
Longtime 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:
 

keirS

Well-Known Member
Licensed User
Longtime 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.
 

magicuser68

Member
Licensed User
Longtime 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
Else
'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
 

Roger Garstang

Well-Known Member
Licensed User
Longtime User
Code Formatting helps it be easier to read:

B4X:
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")
   Else
      '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
 

wheretheidivides

Active Member
Licensed User
Longtime 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:

rgately

Member
Licensed User
Longtime 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!
 

JoanRPM

Active Member
Licensed User
Longtime 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..

Regards.
 

rgately

Member
Licensed User
Longtime 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
 

JoanRPM

Active Member
Licensed User
Longtime User
rgately

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.

Greetings.
 

joneden

Active Member
Licensed User
Longtime 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?

Regards,

Jon

B4X:
   Dim objPhone As Phone 
   DeviceID = objPhone.GetSettings("android_id")
 
Status
Not open for further replies.
Top