0

I have a project that reads and writes NFC tags in android and ios platforms.Both read and write are used inside one function.But I'm having a problem when I click the "Cancel" button on NFC prompt.

The first time the prompt shows up okay. After I click "Cancel" and try to write NFC tag again it says "You can only issue one request at a time" and after that it says "no reference available". These errors are shown as alert messages. After removing these alerts and try again to write nfc tag the NFC popup is displayed perfectly.

So basically when clicking the button to read/write NFC tag the first time it works, the second time it doesn’t work (shows the errors) and the third time it still works.

The code:


const writeNdef = async function (profileToken) {

    if (Platform.OS !== 'ios') {
      promptRef.current.setVisible(true); // the android nfc popup
    }
    
    NfcManager.start();
    
    try {
    
      await NfcManager.requestTechnology(NfcTech.Ndef);
      
      // the resolved tag object will contain `ndefMessage` property
      
      const tag = await NfcManager.getTag();
      
      getSerialNumber(tag.id);
      
      if (isOwnedNfc === true) {
        
        const profileUrl = “my url here”;        
        const bytes = Ndef.encodeMessage([Ndef.uriRecord(profileUrl)]);
        
        await NfcManager.ndefHandler.writeNdefMessage(bytes);
        
        setIsOwnedNfc(false);
        
        if (Platform.OS !== 'ios'){
          promptRef.current.setVisible(false);
        }
        
        Alert.alert (
          "",
          "Success",
          [{ text: "OK" }]
        );
        
      }
      
    } catch (ex) {
      
      if (Platform.OS !== 'ios') {
        promptRef.current.setVisible(false);
      }
      
      Alert.alert (
        "",
        ex.message.toString(),
        [{ text: "OK" }]
      );
      
    } finally {
      
      NfcManager.cancelTechnologyRequest();
    
      if (Platform.OS !== 'ios') {
        
        promptRef.current.setVisible(false);
      }
    }
}

I was expecting the NfcManager.cancelTechnologyRequest() to stop the first NfcManager request. I'm guessing that the 'Cancel' button doesn't cancel the request.

Xho Xho
  • 1
  • 2

0 Answers0