calcrisk33
calcrisk33

Reputation: 587

Write to specified sector on tag using react-native-nfc-manager

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.

Upvotes: 1

Views: 989

Answers (1)

Andrew
Andrew

Reputation: 10162

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

Upvotes: 1

Related Questions