0

I'm building a react native app using react-native-nfc-manager. I need to allow the user to read multiple NFC cards, processing the value returned between each read. I have no problem doing this on Android, where I call to .requestTechnology(), .getTag(), .mifareUltralightHandlerAndroid.mifareUltralightReadPages() and .cancelTechnologyRequest(). I then process the value returned and repeat the process to read the next card, until canceled by the user.

This approach doesn't work on iOS. No matter what I do, the first read succeeds, but the second read fails. Even though I have a 1.5 sec delay before calling readNfc() again, and the card is no longer next to the phone, the second call to getTag() always immediately returns the first card's data (as shown in a console.log of nfcTag.id) and fails on the call to .sendMifareCommandIOS(). No matter what I do, it seems the first card read is somehow still "seen" by .getTag() the second time my readNfc() is called.

Not shown here, but I've separated the calls to .requestTechnology() and .cancelTechnologyRequest() so they only get called before the first call and when canceled.

How can loop through reading cards and processing results on iOS without having to stop the NFC reader and require the user press a button to restart it?

const readNfc = async () => {

let readPages = null;
console.log('[nfcServices] readNfc Starting...');

try {
    // Wait for tag    
    await NfcManager.setAlertMessageIOS('Scan a card…’);
    const nfcTag = await NfcManager.getTag();
    console.log('[nfcServices] readNfc getTag returned ', nfcTag.id);
    
    // Read one four-page array from tag
    readPages = await NfcManager.sendMifareCommandIOS([0x30, 16]);
    await NfcManager.setAlertMessageIOS(‘Card read!');
    console.log('[nfcServices] readNfc readPages: ', readPages);

} catch (ex) {
    // Error
    console.log('[nfcServices] readNfc ERROR: ', ex);
    NfcManager.cancelTechnologyRequest().catch(() => 0);
} finally {
    console.log('[nfcServices] readNfc returning: ', readPages);
    return(readPages);
}

}
David Canora
  • 119
  • 1
  • 14

0 Answers0