Rutherford Nick
Rutherford Nick

Reputation: 11

how to write nfca after ndef technology

1. Context: I am working on a React Native app using react-native-nfc-manager. I need to write an NDEF message to an NTAG213 tag and then write a password in one go.

2. What I’ve Tried:

3. Code:

writingDataToCard = async (ndefData, passwordData) => {
  try {
    this.setState({ cardPlaceModal: true });

    // Request NDEF technology and write NDEF message
    await NfcManager.requestTechnology(NfcTech.Ndef);
    const tagData = await NfcManager.getTag();

    // Prepare and write NDEF message
    let jsonPayload = JSON.stringify(ndefData);
    if (jsonPayload.includes('"')) {
      jsonPayload = jsonPayload.slice(1, -1);
    }
    const message = this.createNdefMessage(jsonPayload);
    if (tagData.techTypes.includes("android.nfc.tech.NdefFormatable")) {
      await NfcManager.ndefFormatableHandlerAndroid.formatNdef(message);
    } else {
      await NfcManager.ndefHandler.writeNdefMessage(message);
    }

    // Switch to NFC-A technology and write additional data
    await NfcManager.cancelTechnologyRequest();
    await NfcManager.requestTechnology(NfcTech.NfcA);

    // Convert password data to appropriate format
    const passwordChunks = this.chunkArray(passwordData, 4);
    for (let i = 0; i < passwordChunks.length; i++) {
      const command = [0xA2, i, ...passwordChunks[i]]; // Modify based on tag's requirements
      await NfcManager.nfcAHandler.transceive(command);
    }

    this.setState({ LinkEnterModal: false });
    Alert.alert('Write Successful', 'Data written to NFC tag successfully.');
    this.setState({ FirstTimeWrite: false });
  } catch (error) {
    Alert.alert('Writing Error', error.toString());
  } finally {
    await NfcManager.cancelTechnologyRequest();
  }
}

Upvotes: 0

Views: 80

Answers (0)

Related Questions