2

I wrote an Android app that uses the transceive() function to communicate with an NFC-V card. My problem is that line

byte[] response = nfcv.transceive(command)

always throws a tag lost exception.

Could someone help me?

String action = intent.getAction();

Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
NfcV nfcv = NfcV.get(tag);
if(nfcv != null) {
    Toast.makeText(this, "nfcv detected", Toast.LENGTH_LONG).show();
}

try {
    nfcv.connect();
    Toast.makeText(this, "connected", Toast.LENGTH_LONG).show();
    byte[] command = new byte[]{                      
            (byte) 0x00, // Flags
            (byte) 0x20, // Command: Read single block
            (byte) 0x00, // First block (offset)
            (byte) 0x04  // Number of blocks};
    byte[] response = nfcv.transceive(command);
    nfcv.close();
} catch(Exception e) {
    Toast.makeText(this, "Error exception!", Toast.LENGTH_LONG).show();
}

I get the following exception:

android.nfc.TagLostException: Tag was lost.
    at android.nfc.TransceiveResult.getResponseOrThrow(TransceiveResult.java:48)
    at android.nfc.tech.BasicTagTechnology.transceive(BasicTagTechnology.java:151)
    at android.nfc.tech.NfcV.transceive(NfcV.java:115)
    at com.example.nxf07589.nfc.MainActivity.onCreate(MainActivity.java:148)
    at android.app.Activity.performCreate(Activity.java:6374)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2767)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2879)
    at android.app.ActivityThread.access$900(ActivityThread.java:182)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1475)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:145)
    at android.app.ActivityThread.main(ActivityThread.java:6141)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
Michael Roland
  • 39,663
  • 10
  • 99
  • 206
  • 2
    You are discarding the exception message and stack trace, which can be very useful for this kind of problem. Just below the toast in the catch block, please add `android.util.Log.e("NFC", "Exception", e);` to read the exception info in your LogCat. Then add the stack trace to your post. – ris8_allo_zen0 Aug 04 '16 at 13:40

1 Answers1

3

You receive a TagLostException because your command is in a wrong format and, consequently, the tag does not answer.

The READ SINGLE BLOCK command (command code 0x20) reads, as its name suggests, a single block. Therefore, there is no length ("number of blocks") field in this command. The correct command would look like this:

int blockAddress = 0;
byte[] cmd = new byte[] {
        (byte) 0x00,  // FLAGS
        (byte) 0x20,  // READ_SINGLE_BLOCK
        (byte)(blockAddress & 0x0ff)
};
byte[] response = nfcv.transceive(cmd);

Note that if the tag does not understand the command (the READ SINGLE BLOCK is an optional command in ISO/IEC 15693), you may still get TagLostException then.

Finally, some Android platforms do not work well (or simply do not support) unaddressed commands for NFC-V. You might therefore want to use the addressed form of that command instead:

byte[] tagUid = tag.getId();  // store tag UID for use in addressed commands

int blockAddress = 0;
byte[] cmd = new byte[] {
        (byte)0x20,  // FLAGS
        (byte)0x20,  // READ_SINGLE_BLOCK
        0, 0, 0, 0, 0, 0, 0, 0,
        (byte)(blockAddress & 0x0ff)
};
System.arraycopy(tagUid, 0, cmd, 2, 8);  // paste tag UID into command
byte[] response = nfcv.transceive(cmd);
Michael Roland
  • 39,663
  • 10
  • 99
  • 206
  • Thanks for the details on the addressed cmd. Is there a document that defines what each element of the cmd array does? I'm trying to read further into memory than can be addressed by a single byte. – benino Sep 28 '22 at 15:36
  • @benino The ISO standard (and the individual manuals of tag products for things that go beyond the standard) – Michael Roland Sep 28 '22 at 19:28