react-native-nfc-manager icon indicating copy to clipboard operation
react-native-nfc-manager copied to clipboard

Enabling Multiple Uses of NFC requestTechnology Instead of One-time

Open divashuthron opened this issue 1 year ago • 2 comments

Hello. I am an IT developer working for a small company in Korea.

What I want to achieve is a functionality where, regardless of whether NFC is tapped once or not, pressing a 'Refresh' button turns off NFC reading and then turns it back on when pressed again.

However, I encountered an error after implementing this.

Could you please let me know if there is anything unusual in my source code?

image

divashuthron avatar Mar 12 '24 01:03 divashuthron

const nfcTagLogin = async () => { try { NfcManager.start(); await NfcManager.requestTechnology(NfcTech.Ndef); const tag = await NfcManager.getTag(); // console.log('NFC tagged, ID: ' + tag.id);

  webviewRef.current.postMessage(String(tag.id));
} catch (error) {
  console.error(error);
} finally {
  NfcManager.cancelTechnologyRequest().catch(() => 0);
}

};

const nfcClean = () => { NfcManager.unregisterTagEvent(); NfcManager.cancelTechnologyRequest(); };

const nfcRefresh =() => { nfcClean(); nfcTagLogin(); };

divashuthron avatar Mar 12 '24 02:03 divashuthron

I think the problem comes from trying to start the scanning immediatly after the cancellation. I had a similar problem and solved it by adding a small delay, which in your case will look like this

const nfcRefresh = () => {
  NfcManager.cancelTechnologyRequest().then(async () => {
    await new Promise((resolve) => setTimeout(resolve, 3500)).then(() => {
      nfcTagLogin()
    });
  })
}

There might be a better solution, but this it the one that i managed to come up with, hope it helps you. Note that I haven't tested that on android

georgipavlov-7DIGIT avatar Apr 25 '24 09:04 georgipavlov-7DIGIT

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days.

github-actions[bot] avatar Jul 25 '24 01:07 github-actions[bot]

This issue was closed because it has been stalled for 5 days with no activity.

github-actions[bot] avatar Aug 09 '24 01:08 github-actions[bot]