首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何避免从NFC标记类型A读取数据时出现的异常“”?

如何避免从NFC标记类型A读取数据时出现的异常“”?
EN

Stack Overflow用户
提问于 2017-11-14 11:40:02
回答 1查看 973关注 0票数 0

我正在开发一个应用程序,可以模仿任何类型的NFC标签。在读取零块和页面标记的数据之后,使用以下方法:"NFC类型A“和"MIFARE Ultralight”(我拥有的标签类型):

代码语言:javascript
复制
nfcA.transceive()

代码语言:javascript
复制
mifareUltralight.readPages()

其余的数据无法读取,而是抛出一个异常“”。

以下是适当方法的代码。对于NfcA:

代码语言:javascript
复制
private static ArrayList<IdNameValue> readeNfcADataset(final Tag tag, final Context cxt) {

    String excMsg = "";
    ArrayList<IdNameValue> dataSet = new ArrayList<>();
    NfcA nfcA = NfcA.get(tag);

    if (nfcA != null) {

        dataSet.add(new IdNameValue(cxt.getString(R.string.atqa) + " ",
                "0x" + byteArrayToHexString(nfcA.getAtqa())));
        dataSet.add(new IdNameValue(cxt.getString(R.string.sak) + " ",
                "0x" + shortToHexString(nfcA.getSak())));

        byte blockNumber = 0x00;
        int bytesNumber = 1, pagesNumber = 16, counter = 0;
        byte[][] bytes = new byte[pagesNumber][bytesNumber];

        excMsg = "_________________________________________________\n" +
                cxt.getString(R.string.picc_compliant_with_iso_iec_14443_3A_fnca) + ":";
        Log.i(TAG, excMsg);

        for (int i = 0; i < pagesNumber; i++) {

            excMsg = "";
            while (!nfcA.isConnected()) {
                try {
                    nfcA.connect();
                    excMsg = cxt.getString(R.string.connecting_to_tag_has_been_created);
                    Log.i(TAG, 109 + ": " + excMsg);

                    bytes[i] = nfcA.transceive(new byte[]{
                            (byte) 0x30,  // READ
                            (byte) (blockNumber & 0x0FF)
                    });
                    excMsg = cxt.getString(R.string.made_to_read_data_from_a_block)
                            + Integer.toHexString(blockNumber);
                    Log.i(TAG, 117 + ": " + excMsg);
                    if (nfcA.isConnected()) nfcA.close();
                    excMsg += cxt.getString(R.string.tag_has_been_closed);
                    break;
                } catch (IOException e) {
                    counter++;
                    excMsg = e.getMessage() + ":> " + (excMsg.equals("") ?
                            cxt.getString(R.string.the_connection_tag_is_not_created) :
                            (excMsg.contains(cxt.getString(R.string.connecting_to_tag_has_been_created)) ||
                                    excMsg.contains(cxt.getString(R.string.made_to_read_data_from_a_page))) ?
                                    "Failed to read data from a tag block: "
                                            + Integer.toHexString(blockNumber) :
                                    cxt.getString(R.string.error_closing_tag));
                    Log.e(TAG, 130 + ": " + excMsg);
                }

                if (nfcA.isConnected()) try {
                    nfcA.close();
                } catch (IOException e1) {
                    excMsg += (":> " + e1.getMessage());
                    Log.e(TAG, 137 + ": " + excMsg);
                }

                if (counter > 9) {
                    excMsg = cxt.getString(R.string.the_number_of_connection_attempts_exceeded_the_number_of_ten);

                    Log.e(TAG, 143 + ": " + excMsg);
                    break;
                }
            }
            counter = 0;
            blockNumber += (byte) bytes[i].length;
        }

        if (excMsg.equals(cxt.getString(R.string.the_number_of_connection_attempts_has_exceeded_one_hundred))) {
            dataSet.add(new IdNameValue("Error", " reading the data tag"));
            return dataSet;
        }

        dataSet.add(new IdNameValue("Page", "    0    1    2    3"));

        String name = "";
        String value = "";
        for (int i = 0; i < pagesNumber; i++) {
            if (!value.equals("")) dataSet.add(new IdNameValue(name, value));
            name = (i > 9 ? " " : "    ") + String.valueOf(i) + "   ";
            value = "";
            for (int j = 0; j < bytes[i].length; j++) {
                value += ("  " + byteToHexString(bytes[i][j]));
            }
        }
        dataSet.add(new IdNameValue(name, value));
        return dataSet;
    }
    return dataSet;
}

对于MifareUltraligh:

代码语言:javascript
复制
private static ArrayList<IdNameValue> readeMifareUltralightDataset(final Tag tag, final Context cxt) {

    MifareUltralight mifareUltralight = MifareUltralight.get(tag);
    String excMsg = "";
    int bytesNumber = 1, pagesNumber = 4, counter = 0, startBlock;
    byte[][] bytes = new byte[pagesNumber][bytesNumber];
    startBlock = 0x00;

    excMsg = "_________________________________________________\n" +
            cxt.getString(R.string.mifare_ultralight_or_ultralight_c) + ":";
    Log.i(TAG, excMsg);

    for (int i = 0; i < pagesNumber; i++) {

        while (!mifareUltralight.isConnected()) {
            excMsg = "";
            try {
                mifareUltralight.connect();
                excMsg = cxt.getString(R.string.connecting_to_tag_has_been_created);
                Log.i(TAG, 201 + ": " + excMsg);

                bytes[i] = mifareUltralight.readPages(startBlock);
                excMsg = cxt.getString(R.string.done_reading_pages_twice_blocks)
                        + Integer.toHexString(startBlock) + " - "
                        + Integer.toHexString((startBlock + bytes[i].length) - 1);
                Log.i(TAG, 207 + ": " + excMsg);
                if (mifareUltralight.isConnected()) mifareUltralight.close();
                excMsg = cxt.getString(R.string.tag_has_been_closed);
                Log.i(TAG, 210 + ": " + excMsg);
                break;
            } catch (IOException e) {
                counter++;
                excMsg = e.getMessage() + ":> " + (excMsg.equals("") ?
                        cxt.getString(R.string.the_connection_tag_is_not_created) :
                        (excMsg.contains(cxt.getString(R.string.connecting_to_tag_has_been_created)) ||
                                excMsg.contains(cxt.getString(R.string.done_reading_pages_twice_blocks)) ?
                                "Failed to read page from tag blocks: "
                                        + Integer.toHexString(startBlock) + " - "
                                        + Integer.toHexString(startBlock + bytes[i].length - 1) :
                                cxt.getString(R.string.error_closing_tag)));
                Log.e(TAG, 222 + ": " + excMsg);
            }

            if (mifareUltralight.isConnected()) try {
                mifareUltralight.close();
                break;
            } catch (IOException e) {
                excMsg = (":> " + e.getMessage());
                Log.e(TAG, 230 + ": " + excMsg);
            }

            if (counter > 9) {
                excMsg = cxt.getString(R.string.the_number_of_connection_attempts_exceeded_the_number_of_ten);
                Log.e(TAG, 236 + ": " + excMsg);
                break;
            }
        }
        counter = 0;
        startBlock += bytes[i].length;
    }

    ArrayList<IdNameValue> dataSet = new ArrayList<>();
    if (excMsg.equals(cxt.getString(R.string.the_number_of_connection_attempts_has_exceeded_one_hundred))) {
        dataSet.add(new IdNameValue("Error", " reading the data tag"));
        return dataSet;
    }

    dataSet.add(new IdNameValue("Page", "    0    1    2    3"));

    int n = -1;
    String name = "";
    String value = "";
    for (int i = 0; i < pagesNumber; i++) {
        for (int j = 0; j < bytes[i].length; j++) {
            if (j % 4 == 0) {
                n++;
                if (!value.equals("")) dataSet.add(new IdNameValue(name, value));
                name = (n > 9 ? " " : "    ") + String.valueOf(n) + "   ";
                value = "";
            }
            value += ("  " + byteToHexString(bytes[i][j]));
        }
    }
    dataSet.add(new IdNameValue(name, value));
    return dataSet;
}

它分解了生成的日志:

代码语言:javascript
复制
...cardreader I/TagController: _________________________________________________
                               PICC compliant with ISO/IEC 14443-3A (NFCA):
...cardreader I/TagController: 109: The connecting to tag has been created
...cardreader I/TagController: 117: Made to read data from a block: 0
...cardreader I/TagController: 109: The connecting to tag has been created
...cardreader E/TagController: 130: Transceive failed:> Failed to read data from a tag block: 10
...cardreader I/TagController: 109: The connecting to tag has been created
...cardreader E/TagController: 130: Transceive failed:> Failed to read data from a tag block: 10
...........................................................
...cardreader I/TagController: 109: The connecting to tag has been created
...cardreader E/TagController: 130: Transceive failed:> Failed to read data from a tag block: 1e
...cardreader E/TagController: 143: The number of connection attempts exceeded the number of ten.
...cardreader I/TagController: _________________________________________________
                               MIFARE Ultralight or Ultralight C:
...cardreader I/TagController: 201: The connecting to tag has been created
...cardreader I/TagController: 207: Done reading pages twice blocks:0 - f
...cardreader I/TagController: 210: Tag has been closed
...cardreader I/TagController: 201: The connecting to tag has been created
...cardreader E/TagController: 222: Transceive failed:> Failed to read page from tag blocks: 10 - 10
...cardreader I/TagController: 201: The connecting to tag has been created
...cardreader E/TagController: 222: Transceive failed:> Failed to read page from tag blocks: 11 - 11
...cardreader I/TagController: 201: The connecting to tag has been created
...cardreader E/TagController: 222: Transceive failed:> Failed to read page from tag blocks: 12 - 12

其余块中的数据可用这一事实显示了输出端口监视程序"Arduino 1.8.5":

代码语言:javascript
复制
Card UID: 04 3A 31 02 54 2E 82
Card SAK: 00
PICC type: MIFARE Ultralight or Ultralight C
Page  0  1  2  3
  0   04 3A 31 87
  1   02 54 2E 82
  2   FA 48 F0 00
  3   FF FF FF FC
  4   45 D9 B0 49
  5   9E 6A 2D 00
  6   1E 82 00 00
  7   1E 82 00 00
  8   1E 7E 05 00
  9   40 00 08 09
 10   B7 DD DB FF
 11   1E 7E 98 48
 12   1E 7E 05 00
 13   40 00 08 09
 14   B7 DD DB FF
 15   1E 7E 98 48

在下面的屏幕截图中,应用程序演示了标签允许读取有关类型、技术和UID:从标签读取的关于其类型、技术和UID的输出数据。的数据。

下面显示了从标签中读取的输出,前16个字节的数据。对于NFC类型A:从数据标签读取的输出(对于NFC类型A)

对于所见的MIFARE Ultralight:从数据标签读取的输出(用于MIFARE超视距),系统只读取前16个字节。有什么不对的?请有任何想法。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-11-23 10:15:45

长时间地浏览文档,即"MF0ICU1 MIFARE超光速非接触式单程票IC“/2004年7月3.9-23修订版#028639"MF0ICU2 MIFARE超光速C“/修订版3.2-19 2009年5月19日#171432,最终理解了这个问题:块地址中的所有salt都提供给命令输入READE。类型标记NFCA的数字块的计算应该如下所示:

代码语言:javascript
复制
blockNumber = (byte) (i * 4);

对于标签类型Mifare超视距:

代码语言:javascript
复制
startBlock = i * 4;

然后一切都起飞了。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47284884

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档