2

I'm developing an Android application which needs to read an NFC card (the card technology is NFC-F). There, I always get following exception:

android.nfc.TagLostException: Tag was lost.

Here is my code:

private void handleIntent(Intent intent) {
    String action = intent.getAction();
    if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action)) {

    } else if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(action)) {

    } else if(NfcAdapter.ACTION_TAG_DISCOVERED.equals(action)) {

        Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
        if (tag != null) {
            NfcF nfcf = NfcF.get(tag);
            try {
                nfcf.connect();
                byte[] AUTO_POLLING_START = {(byte) 0xE0, 0x00, 0x00, 0x40, 0x01};
                byte[] response = nfcf.transceive(AUTO_POLLING_START);
                nfcf.close();
            } catch (Exception e) {
                e.printStackTrace();
                mTextView.setText(e.toString());
            }
        }
    }
}

Can anyone help me regarding this issue?

Michael Roland
  • 39,663
  • 10
  • 99
  • 206
chami
  • 43
  • 6

1 Answers1

0

You receive a TagLostException because you send an invalid command to the tag. Since NFC-F tags remain silent upon reception of invalid commands, Android cannot distinguish between an actual loss of communication or the negative response to an unsupported/invalid command and throws a TagLostException in both cases.

Valid FeliCa (NFC-F) commands have the form

+----------+----------+------------------+-----------------------------------------------+
| LEN      | COMMAND  | IDm              | PARAMETER                                     |
| (1 byte) | (1 byte) | (8 bytes)        | (N bytes)                                     |
+----------+----------+------------------+-----------------------------------------------+

You could assemble them with the following method:

public byte[] transceiveCommand(NfcF tag, byte commandCode, byte[] idM, byte[] param) {
    if (idM == null) {
        // use system IDm
        idM = tag.getTag().getId();
    }
    if (idM.length != 8) {
        idM = Arrays.copyOf(idM, 8);
    }

    if (param == null) {
        param = new byte[0];
    }

    byte[] cmd = new byte[1 + 1 + idM.length + param.length];

    // LEN: fill placeholder
    cmd[0] = (byte)(cmd.length & 0x0FF); 

    // CMD: fill placeholder
    cmd[1] = commandCode;

    // IDm: fill placeholder
    System.arraycopy(idM, 0, cmd, 2, idM.length);

    // PARAM: fill placeholder
    System.arraycopy(param, 0, cmd, 2 + idM.length, param.length);

    try {
        byte[] resp = tag.transceive(cmd);
        return resp;
    } catch (TagLostException e) {
        // WARN: tag-loss cannot be distinguished from unknown/unexpected command errors
    }

    return null;
}

For instance, a command that should succeed on most tags is the REQUEST SYSTEM CODE command (0x0E):

nfcf.connect();
byte[] resp = transceiveCommand(nfcf, (byte)0x0C, null, null);
nfcf.close();
Michael Roland
  • 39,663
  • 10
  • 99
  • 206