Jump to content
Sign in to follow this  
dport

using MediaDrm api for getting unique id on adroid

Recommended Posts

We are using the serial number for unique device identification. In android Q we can't access that device information. 

https://stackoverflow.com/questions/58103580/android-10-imei-no-longer-available-on-api-29-looking-for-alternatives

there is a solution is to use the MediaDrm API PROPERTY_DEVICE_UNIQUE_ID

 

what is the delphi equivalent of the following code

val WIDEVINE_UUID = UUID(-0x121074568629b532L, -0x5c37d8232ae2de13L)
    val id = MediaDrm(WIDEVINE_UUID)
        .getPropertyByteArray(MediaDrm.PROPERTY_DEVICE_UNIQUE_ID)
    var encodedString: String = Base64.encodeToString(id,Base64.DEFAULT)
    Log.i("Uniqueid","Uniqueid"+encodedString)

 

Share this post


Link to post

I translated to delphi

 

function BytesToHex(const ABytes: TBytes): string;
var
  I: Integer;
begin
  Result := '';
  for I := Low(ABytes) to High(ABytes) do
    Result := Result + IntToHex(ABytes[I], 2);
end;

function get_media_drm_UNIQUE_ID:string;
var
 wvDrm:JMediaDrm;
 str:String;
 UUID:JUUID;
 data : TJavaArray<Byte>;
 datab: TBytes;
 i:integer;
begin
//UUID:=TJUUID.JavaClass.fromString(stringtojstring('12107456-8629-b532-5c37-d8232ae2de13'));   //EJNIException with message 'android.media.UnsupportedSchemeException: Failed to instantiate drm object.
  UUID:=TJUUID.JavaClass.fromString(stringtojstring('edef8ba9-79d6-4ace-a3c8-27dcd51d21ed'));//widevine_id

  wvDrm:=tJMediaDrm.Create;
  wvDrm:=tJMediaDrm.JavaClass.init(UUID);
  data:=wvDrm.getPropertyByteArray( TJMediaDrm.JavaClass.PROPERTY_DEVICE_UNIQUE_ID);
  if data <> nil then
  begin
      SetLength(datab, data.Length);
      for I := 0 to data.Length - 1 do
        datab[I] := data.Items[I];
      str:=BytesToHex(datab);
  end;
  result:=str;//length of str:64byte
 end;

procedure TForm1.Button3Click(Sender: TObject);
begin
 memo1.Lines.add(get_media_drm_UNIQUE_ID);
end;

result is for my phone: 42AAF4986AF6DA7F5CE736DDE4549F5D5BC44490646BCFA7A25B0C25B237C946

If you can try and post the result, we can see if it's unique or not

Edited by dport
added BytesToHex function

Share this post


Link to post

B3B4BFBF67D02522F059BB610D9FA6A21D3042F2B45640C3E6A79AF2567B1327

 

with 

 

function ByteArrayToHexString(BA: TArray<Byte>; Sep: string = ''): string;
var
  i, k: integer;
begin
  result:='';

  if Sep='' then begin
     for i:=low(BA) to high(BA) do
       result := result + IntToHex(BA, 2);
  end else begin
     k:=high(BA);
     for i:=low(BA) to k do begin
        result:= result + IntToHex(BA, 2);
        if k<>i then result := result + Sep;
     end;
  end;
end;

 

 

 

Share this post


Link to post
2 hours ago, mausmb said:

B3B4BFBF67D02522F059BB610D9FA6A21D3042F2B45640C3E6A79AF2567B1327

 

with 

 

function ByteArrayToHexString(BA: TArray<Byte>; Sep: string = ''): string;
var
  i, k: integer;
begin
  result:='';

  if Sep='' then begin
     for i:=low(BA) to high(BA) do
       result := result + IntToHex(BA, 2);
  end else begin
     k:=high(BA);
     for i:=low(BA) to k do begin
        result:= result + IntToHex(BA, 2);
        if k<>i then result := result + Sep;
     end;
  end;
end;

 

 

 

thanks mausmb,

sorry i forgot add BytesToHex function.

edited my previous post.

Share this post


Link to post

I tried it on a bunch of android devices and it works fine (9003 different devices to be exact)

And a correction below: (disabled   wvDrm:=tJMediaDrm.Create; row)

function BytesToHex(const ABytes: TBytes): string;
var
  I: Integer;
begin
  Result := '';
  for I := Low(ABytes) to High(ABytes) do
    Result := Result + IntToHex(ABytes[I], 2);
end;

function get_media_drm_UNIQUE_ID:string;
var
 wvDrm:JMediaDrm;
 str:String;
 UUID:JUUID;
 data : TJavaArray<Byte>;
 datab: TBytes;
 i:integer;
begin
//UUID:=TJUUID.JavaClass.fromString(stringtojstring('12107456-8629-b532-5c37-d8232ae2de13'));   //EJNIException with message 'android.media.UnsupportedSchemeException: Failed to instantiate drm object.
  UUID:=TJUUID.JavaClass.fromString(stringtojstring('edef8ba9-79d6-4ace-a3c8-27dcd51d21ed'));//widevine_id

  //wvDrm:=tJMediaDrm.Create; //no need create
  wvDrm:=tJMediaDrm.JavaClass.init(UUID);
  try
   data:=wvDrm.getPropertyByteArray( TJMediaDrm.JavaClass.PROPERTY_DEVICE_UNIQUE_ID);
   if data <> nil then
   begin
      SetLength(datab, data.Length);
      for I := 0 to data.Length - 1 do
        datab[I] := data.Items[I];
      str:=BytesToHex(datab);
   end;
   result:=str;//length of str:64byte
  finally
   wvDrm.release;
  end;   
 end;

procedure TForm1.Button3Click(Sender: TObject);
begin
 memo1.Lines.add(get_media_drm_UNIQUE_ID);
end;

 

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×