B4A Library dgUID - unique ID for your device

Hi all,

sooner or later everyone reaches the point where a "reliable" way to get a unique ID to identify a device or a user is much needed.
I read almost all the forum's threads about the subject and did some research too, but I couldn't find a definitive word on it or an always good solution, so I decided to set up a library where to group all my findings and more: dgUID.

dgUID library is based exclusively on Core and Reflection libraries, so it should be fast and slim.

The intended audience is twofold: those who like to combine elements to come up with a personalized solution and those who prefer to stick with a given solution.

First group will eventually make full use of the whole bunch of GetXXXX functions in the library. Those are intended to expose basic building blocks to be freely combined to result in a custom solution.
The latter group will probably feel more comfortable with any MakeXXXX function, where an ID is returned based on some specific logic and a few parameters.

Following posts will detail about any release pecularity.

Release 0.20 - alpha release; please don't use "Make" functions for production code.
dgUID_020.zip - library files
dgUID_Test_020 - demo code​
 

Attachments

  • dgUID_020.zip
    6.3 KB · Views: 746
  • dgUID_Test_020.zip
    6.3 KB · Views: 661
Last edited:

udg

Expert
Licensed User
Longtime User
Thanks to the excellent Martin Pearman's tool, here it is my library's current version documentation:

Library name: dgUID
Author:
UDG
Version: 0.2
  • cl_dgUID
    Methods:
    • initialize As String
      Initializes the object. You can add parameters to this method if needed.
    • IsInitialized As Boolean
      Tests whether the object has been initialized.
    • getandroidid As String
      API 3 - No permissions required
      A 64-bit number (as a hex string) that is randomly generated when the user first sets up the device
      Changes after a factory reset. If more than one user on the same device, everyone has got its own Android_ID
    • getandroidserial As String
      API 9 - No permissions required
      Hardware serial number, if available. alphanumeric, case-insensitive
    • getfileid As Int
      Returns the integer stored in file "__id" in calling app's dir DirInternal.
      If file doesn't exist, it is created and filled with a random value which is then returned.
    • getgaccount As String
      API 5 - Requires permission: AddPermission(android.permission.GET_ACCOUNTS)
      https://www.b4x.com/android/forum/t...-mail-address-of-the-device.12244/#post-68859
      user's Google Email
    • getimei As String
      API 1 - Requires Permission: READ_PHONE_STATE
      Returns the unique device ID. For example, the IMEI for GSM and the MEID or ESN for CDMA phones. Return Null if it's not available.
    • getline1number As String
      API 1 - Requires Permission: READ_PHONE_STATE
      Returns the phone number string for line 1, for example, the MSISDN for a GSM phone. Return null if it is unavailable.
    • getmac As String
      API 1 - Requires permission: ACCESS_WIFI_STATE
      MAC address valido solo se WiFi esistente e in stato ON
    • getrandomuuid As String
      API 1 - No permissions required
      Generates a variant 2, version 4 (randomly generated number) UUID as per RFC 4122.
    • getsdkint As Int
      API 1 - No permissions required
      Returns an integer which represents the device's Android SDK version. For example: SDK=9=Android 2.3
      Complete list on http://developer.android.com/reference/android/os/Build.VERSION_CODES.html
    • getsimserial As String
      API 1 - Requires permission: READ_PHONE_STATE
      Returns the serial number of the SIM, if applicable. Return null if it is unavailable.
    • getsimstate As Int
      API 1 - No permissions required
      Return SIM state as: 0=unknown(initializing); 1=absent; 2=pin require; 3=puk req; 4=locked; 5=ready
    • getsimsubscriberid As String
      API 1 - Requires Permission: READ_PHONE_STATE
      Returns the unique subscriber ID, for example, the IMSI for a GSM phone. Return null if it is unavailable
    • iswifion As Boolean
      API 1 - Requires permission: ACCESS_WIFI_STATE
    • makedgcode0 (UseRandom As Boolean, UseFile As Boolean) As String
      Returns a UID depending on device values and function parameters. Needs no permissions.
      API < 3 : returns null string or value based on function parameters
      API 3 to 9: tries Android_ID; if it's null then tries with function parameters
      API > 9 : tries Android_ID AND SerialID first; if it's null then tries with function parameters
      If UseRandom is True AND previous modes failed then generates a UUID
      If UseFile is TRUE AND previous modes failed then generates random ID and store it in file (see GetFileID)
    • makedgcodes (UseSIM As Boolean, UseFile As Boolean) As String
      Returns a UID depending on device values and function parameters. May need permission READ_PHONE_STATE
      Similar to MakeDGCode0 as far APIs are concerned.
      If UseSIM is True AND previous modes failed then tries SIM values Serial and SubscriberID. Note: add
      permission READ_PHONE_STATE in calling app Manifest to use this option.
      If UseFile is TRUE AND previous modes failed then generates random ID and store it in file (see GetFileID)
    • makedgcodew (UseMAC As Boolean, UseFile As Boolean) As String
      Returns a UID depending on device values and function parameters. May need permission ACCESS_WIFI_STATE
      Similar to MakeDGCode0 as far APIs are concerned.
      If UseMAC is True AND previous modes failed then tries MAC. Note: that needs permission ACCESS_WIFI_STATE in calling app Manifest
      If UseFile is TRUE AND previous modes failed then generates random ID and store it in file (see GetFileID)
    • makeecode As String
      EREL's code from https://www.b4x.com/android/forum/threads/android-device-unique-id-alternative-to-phoneid.14759
      Per API < 9 genera numero random e lo memorizza localmente
      Per API >= 9 usa SERIAL
 

itgirl

Active Member
Licensed User
Longtime User
I've always had a hard time to get unique IDs for devices , this will be handy thanks for the Lib for sure i'll give it a try
 

udg

Expert
Licensed User
Longtime User
Thank you all for your interest in my work.

Just a quick note about the "alpha" versioning meaning and an hint on how to use the lib on its current version.
I set "alpha" level because the lib will surely evolve into something different from what we have now, especially with regard to the MakeXXXX functions. So, alpha doesn't mean "a few (or many) bugs still present" but simply "pay caution when using it".
All the GetXXXX functions actually are calls to APIs so they should be safe.
MakeXXXX functions should work too, but I may change their inner workings in the future so, please, don't rely on them for production code. It is not guaranteed that they will generate the same IDs on following releases.

MakeXXXX collection will grow rapidly in order to take in account comments and suggestions from other members. After all, they are meant to be a quick way to return an UID based on a specific strategy. You're invited to post your ideas here.

Next release will set public two more MakeXXXX functions based on posts by @keirS and @nwhitfield you can read here about.

udg
 

CanguroCode

Active Member
Licensed User
Longtime User
Hi! Is it compatible with API 27? to this day is it still valid?
 

udg

Expert
Licensed User
Longtime User
I've to check it. GetXXX functions are based on API calls so, if they were not deprecated by Google they should work.
Anyway, let me have a quick look at it.

UPDATE: it seems that a few changes are needed due to methods deprecated and API permissions required. Let's see if on next weekend I'll be able to release a new version of the lib...
 
Last edited:

hatzisn

Well-Known Member
Licensed User
Longtime User
  • Like
Reactions: udg

udg

Expert
Licensed User
Longtime User
Hi all,

please find herein attached temporary release 0.30.
Since I currently haven't available an Android 8 device, I updated the code blindly. Please try it, expecially functions with names ending in "26", and report back. Once we will have a fully functional update, I'll promote it to post #1 along with updated documentation and notes.

TIA
 

Attachments

  • dgUID_030.zip
    7.1 KB · Views: 408
  • dgUID_test_030.zip
    6.6 KB · Views: 354

udg

Expert
Licensed User
Longtime User
Hi,
as briefly showed on post #2 above, for APIs > 9 (that is, everything these days..eheh) it tries one after the other the following:
1. if both AndroidID and SerialID are valid it concatenates the two in that order; if SerialID is not valid (null or unknown) it keeps just AndroidID
2. if AndroidID is empty and parameter UseRandom is true then a RandomUUID is OS generated and returned
3. if AndroidID is empty and parameter UseFile is true then then the value stored in file "_id" in DirInternals is returned (or generated and returned)

Note that Google is strongly suggesting to use the Advertising ID (which can be reset at any time by the user) as a unique identifier
 

Similar Threads

Top