Android Code Snippet Generate Unique Id on Android No permissions Needed.

Based on This StackOverflow

alternatives of IME

B4X:
Sub GETUniqueID As String
Dim JO As JavaObject
Dim ID As String
JO.InitializeContext
ID = JO.RunMethod("getUniqueID", Null)
Log(ID)
Return ID
End Sub

#If Java
import android.media.MediaDrm;
import android.os.Build;
import android.content.*;
import java.util.*;

//private final Context context;
private String initialVal = "";


public String getUniqueID() {
   UUID wideVineUuid = new UUID(-0x121074568629b532L, -0x5c37d8232ae2de13L);
   try {
      MediaDrm wvDrm = new MediaDrm(wideVineUuid);
      byte[] wideVineId = wvDrm.getPropertyByteArray(MediaDrm.PROPERTY_DEVICE_UNIQUE_ID);
      Base64.Encoder enc = Base64.getEncoder();
      String encodedUid = enc.encodeToString(wideVineId);
      return encodedUid;
   } catch (Exception e) {
      return initialVal;
   }
}

#End If

Tested On Factory Reset And it keeps the same Value, Any Suggestions are welcome. Good Luck
 
Last edited:

trepdas

Active Member
Licensed User
Longtime User
isn't that enough? (phone lib)

B4X:
DeviceID = objPhone.GetSettings("android_id")
 

peacemaker

Expert
Licensed User
Longtime User
I guess that
B4X:
AddManifestText(<uses-feature android:name="android.hardware.TELEPHONY" android:required="true"/>)
is required ?

So the code snippet is not for WiFi tablets without GSM module ?
 

Addo

Well-Known Member
Licensed User
Longtime User
I guess that
B4X:
AddManifestText(<uses-feature android:name="android.hardware.TELEPHONY" android:required="true"/>)
is required ?

So the code snippet is not for WiFi tablets without GSM module ?


this part is not required at all

B4X:
private TelephonyManager tm; // = (TelephonyManager) BA.applicationContext.getSystemService(Context.TELEPHONY_SERVICE);
 

asales

Expert
Licensed User
Longtime User
I get this error when I tried the function in an app with MultiDex enabled:
B4X:
java.lang.RuntimeException: Method: getUniqueID not found in: androidx.multidex.MultiDexApplication
 
Last edited:

AscySoft

Active Member
Licensed User
Longtime User
MultiDex enabled:
In my case is working fine even with multidex on. It logs something like "sGctap+5zFz/eiyCZTkQaVv...." etc.

Another question: How to put this code into a module in order to call it from any activityes. I put both java and b4a code Inside a code module, and call it from activity main. it crush with the folowing message "java.lang.RuntimeException: Method: getUniqueID not found in: packagename.main"?
 

Num3

Active Member
Licensed User
Longtime User
Another question: How to put this code into a module in order to call it from any activityes. I put both java and b4a code Inside a code module, and call it from activity main. it crush with the folowing message "java.lang.RuntimeException: Method: getUniqueID not found in: packagename.main"?
B4X:
'Try Changing

Sub GETUniqueID As String

'To

Public Sub GETUniqueID As String
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Code modules have many limitations. Better to use a class.

Change this Java line:
public String getUniqueID()

To
public static String getUniqueID()

Untested code:
B4X:
Sub GETUniqueID As String
    Dim JO As JavaObject
    JO.InitializeStatic(Application.PackageName & ".modulenamelowercased")
    Dim ID As String = JO.RunMethod("getUniqueID", Null)
    Log(ID)
    Return ID
End Sub
 

Daniel44

Active Member
Licensed User
Code modules have many limitations. Better to use a class.

Change this Java line:
public String getUniqueID()

To
public static String getUniqueID()

Untested code:
B4X:
Sub GETUniqueID As String
    Dim JO As JavaObject
    JO.InitializeStatic(Application.PackageName & ".modulenamelowercased")
    Dim ID As String = JO.RunMethod("getUniqueID", Null)
    Log(ID)
    Return ID
End Sub
It doesn't work, neither as the author says nor as Erel indicated. What am I doing wrong? Im testing on Bluestack
 

Attachments

  • GETUNIQUEID.zip
    98.3 KB · Views: 410

Addo

Well-Known Member
Licensed User
Longtime User
better version that returns the id as byte

B4X:
#If Java
import android.media.MediaDrm;
import android.content.*;
import java.util.*;
import java.io.*;

 

 public byte[] getUniqueID() {
   UUID wideVineUuid = new UUID(-0x121074568629b532L, -0x5c37d8232ae2de13L);
   try {
      MediaDrm wvDrm = new MediaDrm(wideVineUuid);
      byte[] wideVineId = wvDrm.getPropertyByteArray(MediaDrm.PROPERTY_DEVICE_UNIQUE_ID);
      return wideVineId;
   } catch (Exception e) {
      // Inspect exception
      return null;
   }
   //
}

 

#End If

usage

B4X:
Dim devid() As Byte
Dim su As StringUtils

devid = nativeMe.RunMethod("getUniqueID", Null)

If devid = Null Then
log("no id found")
return
end if

log(su.EncodeBase64(devid))

this code will work in devices that have a google services . on devices doesn't have google in it will return null
 

KapiteinOllie

New Member
What do I wrong? I have created clean B4XPages project and insert the code into it. When I run it on the phone it crashes:

*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create (first time) **
*** mainpage: B4XPage_Created
*** mainpage: B4XPage_Appear
** Activity (main) Resume **
b4xmainpage_button1_click (java line: 61)
java.lang.RuntimeException: Method: getUniqueID not found in: b4a.example.main
at anywheresoftware.b4j.object.JavaObject$MethodCache.getMethod(JavaObject.java:363)
at anywheresoftware.b4j.object.JavaObject.RunMethod(JavaObject.java:120)
at b4a.example.b4xmainpage._button1_click(b4xmainpage.java:61)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:221)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:201)
at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:80)
at android.view.View.performClick(View.java:7146)
at android.view.View.performClickInternal(View.java:7119)
at android.view.View.access$3500(View.java:803)
at android.view.View$PerformClick.run(View.java:27533)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7386)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:980)

When I create a Default project, insert the code and run it. I works and get result.

I ran the projects on the same phone.
 
Top