android - 在 Android 上读取 NFC-F 卡时出现 TagLostException

标签 android tags nfc ioexception contactless-smartcard

我正在开发一个需要读取 NFC 卡(卡技术是 NFC-F)的 Android 应用程序。在那里,我总是遇到以下异常:

android.nfc.TagLostException: Tag was lost.

这是我的代码:

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());
            }
        }
    }
}

谁能帮我解决这个问题?

最佳答案

您收到 TagLostException,因为您向标签发送了无效命令。由于 NFC-F 标签在收到无效命令时保持静默,Android 无法区分实际通信丢失或对不受支持/无效命令的否定响应,并在这两种情况下抛出 TagLostException

有效的 FeliCa (NFC-F) 命令具有以下形式

+----------+----------+------------------+-----------------------------------------------+
| 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;
}

例如,在大多数标签上应该成功的命令是 REQUEST SYSTEM CODE 命令 (0x0E):

nfcf.connect();
byte[] resp = transceiveCommand(nfcf, (byte)0x0C, null, null);
nfcf.close();

关于android - 在 Android 上读取 NFC-F 卡时出现 TagLostException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45690241/

相关文章:

android - 轨道重启、关闭、加电和断电

c# - Unity C# 可滚动 GUI.BOX 不适用于 android

mysql - 计算mysql中的匹配项

android - NFC标签安卓

java - 如何使用Android studio将2个项目合并为1个?

android - 为什么不调用此Android异步任务中的doInBackground

android - 卸载旧的 Android SDK 版本

ruby - 在 RSpec 循环中有条件地应用标签

java - 如何格式化 struts 2 select 标签中的日期列表?

android - 加密 NFC/RFID 标签?