如何在 android 上以编程方式写保护 NTAG203 NFC 芯片?

How to write protect an NTAG203 NFC chip programmatically on android?

我一直在研究用于会员卡解决方案的 NTAG203 NFC 芯片,并打算在每张卡上设置和写入唯一 ID。

奇怪的是,我在网上找不到很多关于如何写保护 NTAG203 芯片的信息,尽管我可以找到正在出售的写保护芯片。我也见过提供写保护服务的应用程序。

如何编写 android 应用程序以在 NTAG203 上启用写保护?

谢谢!

(为清楚起见编辑了问题)

是的,这是可能的。 NTAG203 (datasheet) 是一个 ISO/IEC 14443 A 类 ("NFC-A") 标签,遵循 NFC 论坛 2 类标签操作规范。为了激活此类标签的物理写保护功能,您需要设置锁定位。

在 Android,您可以通过为您的标签句柄获取 NfcA 技术连接器 class 的实例来访问此类标签:

Tag tag = ...  // I assume you already received the tag handle by means of an NFC discovery intent
NfcA nfcA = NfcA.get(tag);
if (nfcA != null) {
    // this is an NFC-A tag
    ...

NTAG203 的锁定位位于第 2 页 (0x02) 字节 2-3 和第 40 页 (0x28) 字节 0-1。这 4 个字节的每一位都控制 NTAG203 内存区域某些页面的锁定状态。为了激活锁定,您必须通过对包含锁定位的页面发出写命令来将锁定位设置为 '1'。因此,对于锁定整个标签的最简单情况,您可以这样做:

    // connect to the tag
    nfcA.connect();

    byte[] result;
    // write all-ones to the lock bits on page 0x02
    result = nfcA.transceive(new byte[]{
            (byte)0xA2,  // Command: WRITE
            (byte)0x02,  // Address: page 0x02 (2)
            (byte)0x00, (byte)0x00, (byte)0xFF, (byte)0xFF  // Data: set bytes 2-3 to all '1'
    });
    // write all-ones to the lock bits on page 0x28
    result = nfcA.transceive(new byte[]{
            (byte)0xA2,  // Command: WRITE
            (byte)0x02,  // Address: page 0x28 (40)
            (byte)0xFF, (byte)0x11, (byte)0x00, (byte)0x00  // Data: set byte 0 and lock bits in byte 1 to all '1'
    });

    nfcA.close();