React Native NFC Scans sometimes returns undefined on iOS units

0
18
React Native NFC Scans sometimes returns undefined on iOS units


I’ve a cell software written in React Native which additionally makes use of Expo. It makes use of NFC scanning as a mechanism for putting stamps on loyalty playing cards digitally, so you’ll be able to assume this can be a crucial performance for me to work flawlessly. It really works tremendous however one drawback I’ve observed is that generally when scanning on iOS units, the plugin will return undefined despite the fact that a tag was scanned and a examine seems within the iOS nfc drawer which pops up. I’ve tried debugging it utilizing logs in every single place a number of and nonetheless discovered no trigger for it taking place, besides that it occurs randomly and when it occurs, it tends to occur a number of instances afterwards.

That is my code for beginning the nfc supervisor within the app part:

useEffect(() => {
  const setupNfc = async () => {
    attempt {
      const isSupported = await NfcManager.isSupported();
      if (!isSupported) {
        console.warn("NFC will not be supported on this gadget.");
        return;
      }

      const isEnabled = await NfcManager.isEnabled();
      if (!isEnabled) {
        console.warn("NFC will not be enabled on this gadget.");
        return;
      }

      await NfcManager.begin();

      if (Platform.OS === "android") {
        await NfcManager.registerTagEvent();
      }
    } catch (error) {
      console.error("Error beginning NFC:", error);
    }
  };

  setupNfc();
}, []);

And that is my code for scanning an nfc tag:

const scanNFC = async (): Promise => {
  attempt {
    await NfcManager.requestTechnology(NfcTech.Ndef);

    const tag = await NfcManager.getTag();

    if (!tag || !tag.id) {
      console.log("NFC: No tag discovered or tag ID undefined");
      return undefined;
    }

    const tagId = tag.id;
    console.log(`NFC: Tag ID discovered: ${tagId}`);

    return tagId;
  } catch (ex) {
    if (ex instanceof NfcError.UserCancel) {
      console.log("NFC: Person canceled the NFC scan.");
    } else {
      console.error("NFC: NFC learn error:", ex);
    }
    return undefined;
  } lastly {
    await NfcManager.cancelTechnologyRequest();
  }
};

Listed here are some logs what can be useful for you:

 LOG  NFC: Tag ID discovered: 0422DA34BA2A81
 LOG  HomePage: Tag: 0422DA34BA2A81
 LOG  HomePage: Scanned tag id is: 0422DA34BA2A81
 LOG  HomePage: Scanned tag belongs to an admin, skipping updating stamps...
 LOG  NFC: Tag ID discovered: 0422DA34BA2A81
 LOG  HomePage: Tag: 0422DA34BA2A81
 LOG  HomePage: Scanned tag id is: 0422DA34BA2A81
 LOG  HomePage: Scanned tag belongs to an admin, skipping updating stamps...
 LOG  NFC: Tag ID discovered: 0422DA34BA2A81
 LOG  HomePage: Tag: 0422DA34BA2A81
 LOG  HomePage: Scanned tag id is: 0422DA34BA2A81
 LOG  HomePage: Scanned tag belongs to an admin, skipping updating stamps...
 LOG  NFC: Person canceled the NFC scan.
 LOG  HomePage: Tag: undefined
 LOG  HomePage: NFC scan didn't return a legitimate tag

I might be glad if any of you’ll be able to assist me with this!

LEAVE A REPLY

Please enter your comment!
Please enter your name here