0

I'm using an NTAG I2C plus 2k memory tag and using the react-native-nfc-manager library to interface with the tag.

Is there a way to specify the sector that you're intending to write to?

I know there is an API to specify the page offset to write to, but how do you specify the sector the page offsets are in?

(Updated to include below code sample)

let tech = Platform.OS === 'ios' ? NfcTech.MifareIOS : NfcTech.NfcA;
  await NfcManager.requestTechnology(tech, {
    alertMessage: 'Hold your phone close to the NFC tag.',
  });

  let fullLength = data.length + 7;
  let payloadLength = data.length + 3;

  let cmd =
    Platform.OS === 'ios'
      ? NfcManager.sendMifareCommandIOS
      : NfcManager.transceive;

  // select sector 2 prior to writing data
  await cmd([0xc2, 0xff]);
  await cmd([0x02, 0x00, 0x00, 0x00]);

  await cmd([
    0xa2,
    MEMORY_MAPPING[`${chunkToWriteTo}`][1],
    0x03,
    fullLength,
    0xd1,
    0x01,
  ]);
  await cmd([
    0xa2,
    MEMORY_MAPPING[`${chunkToWriteTo}`][2],
    payloadLength,
    0x54,
    0x02,
    0x65,
  ]);

  let currentPage = MEMORY_MAPPING[`${chunkToWriteTo}`][0] + 2;
  let currentPayload = [0xa2, currentPage, 0x6e];

  for (let i = 0; i < data.length; i++) {
    currentPayload.push(parseInt(data[i]));
    if (currentPayload.length == 6) {
      try {
        await cmd(currentPayload);
      } catch (error) {
        console.log(error);
      }
      currentPage += 1;
      currentPayload = [0xa2, currentPage];
    }
  }

Thanks in advance.

calcrisk33
  • 437
  • 1
  • 6
  • 15

1 Answers1

1

So "NTAG I2C plus 2k" seems to be a Certified Type 2 tag using NfcA comms.

It's datasheet

This Tag has additional commands over the Type 2 Standard to select the sector because Type 2 Tag's don't normally have Sectors.

So reading the datasheet Section 10.12 you would transceive the following commands bytes an example

C2h FFh - Select Sector

03h 00h 00h 00h - Sector 3

Then write to page address as normal with the A2h command byte

react-native-nfc-manage offers an nfcAHandler with a transceive method to send and receive these low level commands to the NFC chip.

Update:

For iOS it treats Type 2 Tags as Mifare Ultralight's and thus sendMifareCommandIOS from the API to send the same commands.

(Both Android and iOS have the nfcAHandler)

Note I've not tried this, I just do things with normal Type 2 Tags

Andrew
  • 8,198
  • 2
  • 15
  • 35
  • sounds like the above will get it done on Android ... do you know how to achieve the same on iOS via react-native-nfc-manager? – calcrisk33 Aug 09 '21 at 20:38
  • I updated the question to include my code sample. After adding `await cmd([0xc2, 0xff]); await cmd([0x02, 0x00, 0x00, 0x00]);` to switch sectors, I'm not getting the expected result, data being written to sector 2. Any suggestions? – calcrisk33 Aug 10 '21 at 22:09
  • 1
    If I'm reading the Memory Organisation section of the datasheet correctly then on the 2k Tag there is nothing you can write to or read from in sector 2, it should always return invalid access NAK. Do you want to write to the second sector? As they are zero indexed that would be sector `1` – Andrew Aug 10 '21 at 22:18
  • ugh ... nice catch. I'm able to successfully write now. much appreciated! – calcrisk33 Aug 11 '21 at 02:39
  • iOS is working perfectly but I'm getting the below error on Android from this command `await cmd([0x01, 0x00, 0x00, 0x00]);` ... [this issue](https://stackoverflow.com/questions/68746957/ntag-i2c-fast-read-is-erring-out-after-a-particular-page-address) seems similar but that workaround isn't working for me. Tried setting a timeout as well with no success. `Error: transceive fail` – calcrisk33 Aug 12 '21 at 21:45
  • So that command gives what they call is a "Passive ACK" which is a pain which is no response if successful where as Android is probably always expecting some data in return or an "Active ACK" (return 4bit code of Ah) or "Active NAK" , so you probably just need to catch that error and continue on if not data was returned (if there was data you should check the return data for a NAK 4bit code) – Andrew Aug 13 '21 at 00:44