Search code examples
androidnfcdelphi-xefiremonkeymifare

Communication with Mifare DESFire in Delphi


I want to write application in Delphi, which can communicate with Android phones and DESFire cards. I know, I have to send some bytes to card and card answers me. I read article about it:

https://ridrix.wordpress.com/2009/09/19/mifare-desfire-communication-example/

I have no idea, how can I write and read bytes from card? I wrote simple application according to Daniel Magin:

http://www.danielmagin.de/blog/index.php/2014/09/nfc-android-application-with-delphi-xe6-and-xe7/

This program can only read UID from card.

function TNfc.ReadNFCUID: string;
var
  Intent: JIntent;
  jIntentName: JString;
  IntentName: string;
  tagId: Androidapi.JNIBridge.TJavaArray<Byte>;
  tagFromIntent: JParcelable;
  id: string;
  i: Integer;

begin
  id := '';
  Intent := SharedActivity.getIntent;

  if Intent <> nil then
  begin
    jIntentName := Intent.getAction;
    IntentName := JStringToString(jIntentName);

    tagId := Intent.getByteArrayExtra(TJNFCAdapter.JavaClass.EXTRA_ID);

    tagFromIntent := Intent.getParcelableExtra
      (TJNFCAdapter.JavaClass.EXTRA_TAG);
    if (tagId <> nil) and (tagFromIntent <> nil) then
    begin
      for i := 0 to tagId.Length - 1 do
        id := id + IntToHex(tagId.Items[i], 2);
    end;
  end;

  Result := id;

end;

Solution

  • I find solution for my question:

    ..
    var
        isoNFC : JIsoDep;
        tag : JTag;
        aRawData : TJavaByteArray;
        aResponse : TJavaByteArray;
    
    begin
      aRawData := TJavaByteArray.Create(1);
    
      tag := TJTag.Wrap((CurrentNFCTag as ILocalObject).GetObjectID);
      isoNFC := TJIsoDep.JavaClass.get(tag);
      isoNFC.connect();
    
      aRawData.Items[0] := TCmd.GetApplicationIDs;
      aResponse := isoNFC.transceive(aRawData);
    ..