revtel / react-native-nfc-manager

React Native NFC module for Android & iOS
MIT License
1.38k stars 317 forks source link

Calling a context hook before cancelTechnologyRequest causes it to fail with "Not even registered" #613

Closed jaimemarijke closed 1 year ago

jaimemarijke commented 1 year ago

I am able to open the NFC reader and scan a tag, but when I attempt to call cancelTechnologyRequest it fails with a "Not even registered" error, and the NFC scanner modal is not dismissed.

After some debugging, I discovered that the error only repros when I call my setTagRecordData context hook from within the try block (TagRecordFormDataContext is a Context Provider). If I don't call the setTagRecordData hook, everything works correctly.

const TagScanScreen = () => {
  const { tagRecordData, setTagRecordData } = useContext(TagRecordFormDataContext);

  NfcManager.start();

  async function openNfcReaderAndScanTag() {
    try {
      await NfcManager.requestTechnology(NfcTech.Ndef);
      const tag = await NfcManager.getTag();
      LOG.debug('Tag found', tag);

      // If I comment out this call to setTagRecordData, the later call to cancelTechnologyRequest succeeds
      setTagRecordData({ ...tagRecordData, tagId: tag.id });

      await NfcManager.setAlertMessage('Tag scanned!');
    } catch (e) {
      LOG.warn('Error while scanning for tag!', e);
    } finally {
      await NfcManager.cancelTechnologyRequest({ throwOnError: true });
    }
  }

  return (
    <View>
      <Pressable onPress={() => openNfcReaderAndScanTag()}>
        <Text style={styles.bodyText}>(PROTOTYPE) Scan the RFID tag</Text>
      </Pressable>
    </View>
  );
};

Here's the stack trace:

LOG  10:09:03 AM | DEBUG : Tag found 
{
  "id": "04D0A7320A5480",
  "tech": "mifare",
  "ndefMessage": [
    {
      "payload": [
        2,
        101,
        110,
        57,
        48,
        48,
        49,
        49,
        49,
        56,
        56,
        49,
        53,
        51,
        50,
        51,
        56,
        55
      ],
      "type": [
        84
      ],
      "id": [],
      "tnf": 1
    }
  ]
}
 LOG  10:09:03 AM | WARN : Error while scanning for tag! Not even registered
 WARN  Possible Unhandled Promise Rejection (id: 0):
Error: Not even registered
construct@[native code]
construct@[native code]
_construct@http://192.168.1.162:8082/index.bundle?platform=ios&dev=true&hot=false:19238:28
Wrapper@http://192.168.1.162:8082/index.bundle?platform=ios&dev=true&hot=false:19200:25
construct@[native code]
_createSuperInternal@http://192.168.1.162:8082/index.bundle?platform=ios&dev=true&hot=false:188263:322
NfcErrorBase@http://192.168.1.162:8082/index.bundle?platform=ios&dev=true&hot=false:188270:26
buildNfcExceptionIOS@http://192.168.1.162:8082/index.bundle?platform=ios&dev=true&hot=false:188599:28
@http://192.168.1.162:8082/index.bundle?platform=ios&dev=true&hot=false:188619:41
generatorResume@[native code]
asyncGeneratorStep@http://192.168.1.162:8082/index.bundle?platform=ios&dev=true&hot=false:21009:26
_throw@http://192.168.1.162:8082/index.bundle?platform=ios&dev=true&hot=false:21031:29
tryCallOne@http://192.168.1.162:8082/index.bundle?platform=ios&dev=true&hot=false:25622:16
@http://192.168.1.162:8082/index.bundle?platform=ios&dev=true&hot=false:25703:27
@http://192.168.1.162:8082/index.bundle?platform=ios&dev=true&hot=false:26598:26
_callTimer@http://192.168.1.162:8082/index.bundle?platform=ios&dev=true&hot=false:26517:17
_callReactNativeMicrotasksPass@http://192.168.1.162:8082/index.bundle?platform=ios&dev=true&hot=false:26547:17
callReactNativeMicrotasks@http://192.168.1.162:8082/index.bundle?platform=ios&dev=true&hot=false:26710:44
__callReactNativeMicrotasks@http://192.168.1.162:8082/index.bundle?platform=ios&dev=true&hot=false:20147:46
@http://192.168.1.162:8082/index.bundle?platform=ios&dev=true&hot=false:19959:45
__guard@http://192.168.1.162:8082/index.bundle?platform=ios&dev=true&hot=false:20131:15
flushedQueue@http://192.168.1.162:8082/index.bundle?platform=ios&dev=true&hot=false:19958:21
flushedQueue@[native code]
invokeCallbackAndReturnFlushedQueue@[native code]

I am testing using a development build on my iPhone 12 mini with iOS 16.3.

github-actions[bot] commented 1 year ago

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] commented 1 year ago

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