android - 将 Mifare Ultralight 格式化为 NDEF 抛出 IO 异常

标签 android format nfc mifare ndef

我想使用以下代码格式化从未使用过的 MIFARE Ultralight 卡:

NdefFormatable formatable = NdefFormatable.get(tag);
if (formatable != null) {
    String result = "Afifly";
    try {
        formatable.connect();

        try {
            formatable.format(new NdefMessage(new NdefRecord(NdefRecord.TNF_EMPTY, null, null, null)));
        } catch (Exception e) {
            // let the user know the tag refused to format
            System.out.println("error ");//+getStackTrace(e));
            result = "Fail 1";
        }
    } catch (Exception e) {
        // let the user know the tag refused to connect
        System.out.println("eeeerrror 2"+e);
        result = "Fail 2";
    } finally {
        try {
            formatable.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return;
}

但在调用方法 formatable.format(...) 时,它总是抛出 IOException(没有任何有意义的消息)。

我尝试了其他几张卡,结果都相同。但是,这些卡可以使用 NXP TagWriter 等进行格式化。

我已经找到了问题/答案“Formatting a Mifare Ultralight C to NDEF”,但这个解决方案对我不起作用。我仍然得到相同的 IOException。

标签的前四页(页 0-3)包含以下字节:

04 F1 E9 94
42 AB 4A 80
23 48 00 00  (all lock-bits cleared)
00 00 00 00  (no capability container)

因此,标签为空且未锁定。

最佳答案

在空 MIFARE Ultralight 标签上调用 NdefFormatable.format() 时出现 IOException 最可能的原因是您的设备不支持“格式化”(即,初始化为 NFC 论坛类型 2 标签)该类型的标签。如果是这样的话,那么您甚至会看到 NdefFormatable 技术,这显然是一个错误。

在这种情况下,您唯一的选择是手动执行格式化过程(有关详细信息,请参阅 NFC 论坛类型 2 标签操作规范)。这也是各种标签写入应用程序(例如 NXP TagWriter)所做的事情。对于 MIFARE Ultralight (MF0ICU1) 标签(不要尝试将其用于较大的标签!),如下所示:

NfcA nfcA = NfcA.get(tag);
if (nfcA != null) {
    try {
        nfcA.connect();
        nfcA.transceive(new byte[] {
            (byte)0xA2,  // WRITE
            (byte)0x03,  // page = 3
            (byte)0xE1, (byte)0x10, (byte)0x06, (byte)0x00  // capability container (mapping version 1.0, 48 bytes for data available, read/write allowed)
        });
        nfcA.transceive(new byte[] {
            (byte)0xA2,  // WRITE
            (byte)0x04,  // page = 4
            (byte)0x03, (byte)0x00, (byte)0xFE, (byte)0x00  // empty NDEF TLV, Terminator TLV
        });
    } catch (Exception e) {
    } finally {
        try {
            nfcA.close();
        } catch (Exception e) {
        }
    }
}

关于android - 将 Mifare Ultralight 格式化为 NDEF 抛出 IO 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35985287/

相关文章:

android - Android Digital Clock 如何显示日期?

android - android中的计时器不调用函数

android - Google 论坛短网址在 Android 应用程序 Webview 中不起作用

python - pandas 数据框样式格式不打印指定的精度

Java格式化

android - 如何在 recyclerview 或 ListView 中的对话之间显示日期

android - 错误消息 : Incorrect formatting string ___, '%, c' 中缺少转换字符

java - NFC 应用程序在 enableForegroundDispatch 上崩溃

android - 如何通过 API 启用 NFC 阅读器?

java - 重新连接 USB 设备 (ACR122U),无需拔下插头