This video is available to students only

Read And Write Pokemon Data

Hello, in this lesson, we will focus on how to read and write Pokemon data into NFC tags.

We will:

  • First learn to understand the NFC Type 2 Tag Platform, which includes:

    • memory structure

    • commands set

  • Implement our Pokemon read and write logic into our React Native app

Let's get started!

Understand the Type 2 NFC tag#

As mentioned in the previous lesson, NFC-Forum classifies NFC tags into different Tag Platforms, from Type 2 to Type 5, and each of them has its own specification published as a standard.

Since we will focus on the Type 2 NFC tag (or T2T in short) in our course, let's take a look at the outline of this specification.

Among these chapters, I will walk you through:

  • Chapter.2 Memory Structure and Management

  • Chapter.5 Command Set

T2T memory structure#

This is the memory structure diagram for NFC Type 2 Tag.

The tag itself is divided into blocks containing four bytes each. The number associated with a block is also called "block number," which starts from 0.

The blocks can be further grouped into sectors. A sector is defined as 256 contiguous blocks. So one sector is 1024 bytes or 1Kilo Bytes.

However, most of the tags have a memory size less than 1KB, so they are always in sector 0.

The first three blocks contain two pieces of information. First is the UID, which is used as a unique hardware identifier. We will use it to calculate our digital signature in the following lessons. The second part is the lock bytes for memory protection purposes. These three blocks are normally programmed by the NFC tag manufacturer rather than us.

The next block, Capability Container, or CC block for short, is used to indicate whether the current tag complies with the NFC-Forum-defined tag platform specification and also contains some meta information for our NFC tags.

When a tag complies with Type 2 specification:

  • The first byte of this block should always be E1 in hex. This is a magic number defined by NFC-Forum.

  • The second byte encodes the spec version number it complies with.

  • The third byte indicates the memory size of the data blocks divided by 8. For example, an NFC tag with 128 bytes data area should set this byte to 16 decimal, or 10 in hex

  • The last byte indicates the read and write access capability. The high nibble is for the read access, and the low nibble is for the write access. For both read and write, the value 0h indicates the operation granted without any security concern. All values other than 0 are either reserved or for proprietary usage defined by individual tag manufacturers. As you can see, this access control mechanism doesn't provide enough granularity for our application, so we choose to use a tag-specific password protection method for our application.

Starting with block number 4, we have the DATA blocks, which are used to store application data. All Pokemon data in this course will be written into this area.

The final "Lock & Reserved" blocks are often used by individual NFC Tag manufacturers to control their proprietary features, such as password protection, which will be discussed later in this course.

By the way, in the NFC domain, you might hear some people use the term "page" rather than "block." That's because some NFC manufacturers, like NXP, use this term in their proprietary documentation. But they mean the same thing: a 4-byte memory section in our NFC tag.

In this course, we might use these two terms, "block" and "page," interchangeably.

T2T commands#

Next, let's talk about the commands for the NFC Type 2 Tag.

First is the READ command. This command has only two bytes. The first byte is the command code 0x30 in hex, and the second byte is the block number to start reading. The tag will respond by sending sixteen bytes, starting from the block number you passed. For example, if the block number passed is 4, then blocks 4, 5, 6, and 7 are returned. We can check if the response length is equal to 16 to know whether there are any errors.

Next is the WRITE command. This command is used to write one block into our NFC tags. The first byte is the command code A2 in hex, and the second byte is the block number, followed by 4 bytes of data you'd like to write.

Start a new discussion. All notification go to the author.