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:

KapiteinOllie

New Member
Change
public String getUniqueID() {
to
public static String getUniqueID() {
Thank you for replying Erel,

I have tried your solutions but

First try of compilation it gave me an error:

(AndroidX SDK)
Compiling resources (0.05s)
Linking resources (0.23s)
Compileren gegenereerde Java code. Error
src\b4a\example\b4xmainpage.java:111: error: non-static variable initialVal cannot be referenced from a static context
return initialVal;
^
Note: src\b4a\example\main.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
1 error

javac 14.0.1

so i changed
private String initialVal = "";
to
private static String initialVal = "";

No error with compilation but when i run the app it gave me the same error:

*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create (first time) **
*** mainpage: B4XPage_Created
*** mainpage: B4XPage_Appear
** Activity (main) Resume **
b4xmainpage_vvvv3 (java line: 75)
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._vvvv3(b4xmainpage.java:75)
at b4a.example.b4xmainpage._button1_click(b4xmainpage.java:51)
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)
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Sorry, missed the issue.

When you use this code in a class (such as B4XMainPage) it should be like this:
B4X:
Sub GetUniqueID As String
    Dim JO As JavaObject = Me
    Return JO.RunMethod("getUniqueID", Null)
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
 
Top