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
isn't that enough? (phone lib)

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

peacemaker

Expert
Licensed 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 ?
 

PassionDEV

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

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

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

Administrator
Staff 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
 

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: 71

PassionDEV

Well-Known Member
Licensed 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
 
Top