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:

LucaMs

Expert
Licensed User
In that calculation you have nothing about possible matching.

You just calculated average distance between 2 neighbor numbers
from sorted list of 100,000 random generated intigers from interval [0, 2,147,483,648]

Lets say person X1 got one of 2,147,483,648 numbers.
Person X2 will get the same number with 1 / 2,147,483,648 chance = 0.0000000004656...
Person X2 will get different number with 2,147,483,647 / 2,147,483,648 chance = 0.9999999995343...

but we did't test all possible matching.
we tested only person X1 with person X2

we must test:

(X1 with X2) AND (X1 with X3) AND (X1 with X4) AND ... AND (X1 with X100,000)
AND
(X2 with X3) AND (X2 with X34 AND (X2 with X5) AND ... AND (X2 with X100,000)
AND
.
.
.
AND
(X99,999 with X100,000)

There is 4,999,950,000 possible matching (Binomial coefficient "100,000 choose 2")

So,
Probability that one pair have different numbers is:
2,147,483,647 / 2,147,483,648 chance = 0.9999999995343

Probability that all 4,999,950,000 pairs have different numbers is:
0.9999999995343^4,999,950,000=0.0974

If we take the complement of that:
Probability that there is at least one pair that have same numbers is:
1 - 0.0974 = 0.9026 = 90.26 % :eek:

Maybe contra intuitive but, like this,
similar "paradox" is that in group of just 23 people
greater chance is that there exist pair of two people that celebrates birthday
on same day than there doesn't exist such pair.
And here is 365 / 23 = 15.87 average days distance between 2 neighbor birthdays

Hope this helps! :)

Your answer is very complicated and at this time I have my usual headaches (also English does not help).

If you're right, a test is sufficient.

100 random pairs (or more, 100,000) and calculate how many of them are equal.
 

danijel

Active Member
Licensed User
Is it correct?

It runs 10 tests and count how many of them fails or not
total success will be around 50% (on 55,000)
total success will be around 10% (on 100,000)

WARNING: REALLY SLOW :D
 

Attachments

  • prob2.zip
    18.1 KB · Views: 207

LucaMs

Expert
Licensed User
It runs 10 tests and count how many of them fails or not
total success will be around 50% (on 55,000)
total success will be around 10% (on 100,000)

WARNING: REALLY SLOW :D

I did not understand.

The app calculates how many times two "Id" calculated at random are equal in proportion to the number of attempts. Probably never.

The probability that they are equal is 1 / (2 ^ 31)!



[using an emulator it is very fast, much less than one second]



[P.S. ops, you were referring to your project]
 
Last edited:

danijel

Active Member
Licensed User
Ok, we don't understand each other.

The probability that two numbers are equal is 1 / (2 ^ 31)! - That's correct.
But if you have 100,000 users i'm trying to say that very often 99,998 users will get unique number and only 2 users will get the same number !
so not all users will have unique number and that Test is fail!
If happens that all 100,000 users get unique number that test is successful!

And i'm saying that in 90% cases Test will faill

Like in lottery... chance for one person to win is 1:15,000,000,000
but every month somebody score. How is that? - Lot's of people play.
 

danijel

Active Member
Licensed User
To be precise there is 90.25% chance that at least 2 of 100,000 users will get the same number.
Math is an exact science:

calc.png
 

LucaMs

Expert
Licensed User
To be precise there is 90.25% chance that at least 2 of 100,000 users will get the same number.
Math is an exact science:

View attachment 32765

I'm not at all an expert in mathematics.

What is the operation represented by the position of the binomial coefficient?

(I do not know how to express well the question)

upload_2015-3-9_2-30-32.png
 

Shay

Well-Known Member
Licensed User
Using the code on first page (Erel) I cannot get ID from android tv device (getting 123456..)
and using the WIFI code I am getting 2:0:0:0:0... which is also not valid.
Any other way to get unique device id (especially from android tv devices?)
 

Informatix

Expert
Licensed User

LucaMs

Expert
Licensed User
Working method to get an unique ID:
https://stackoverflow.com/a/14893618
From that page:
  • Phone number/SIM card number (if available or just use zeros)
  • Android ID
  • Mac Address (if available or just use zeros)
So, if first and third element are zeros, you will use only Android ID. If it was enough, why should you use the other two?

Creating a rnd num (long) and saving it in the DirInternal is not a valid solution?
 

Informatix

Expert
Licensed User
From that page:
  • Phone number/SIM card number (if available or just use zeros)
  • Android ID
  • Mac Address (if available or just use zeros)
So, if first and third element are zeros, you will use only Android ID. If it was enough, why should you use the other two?

Creating a rnd num (long) and saving it in the DirInternal is not a valid solution?
I was in a bit of a hurry yesterday and I didn't elaborate. The idea to remember is that it is better to make an aggregate of several values than to simply rely on a single one. I implemented the proposed solution in my first application, My Playground, and I'm satisfied with it, but I didn't have a strict requirement.

Creating a random number that you save in a directory exposes you to the risk of hacking. Using the ID of someone else becomes pretty easy.
 
Status
Not open for further replies.
Top