0

我目前正在开发一个简单的应用程序,用于扫描、读取 NFC 卡(在我的情况下是 Mifare NFC 卡)中的数据并显示在屏幕上。我已经使用这个例子构建了它。

该应用程序应该可以在 Android 和 iOS 上运行,但目前,我只在 android 设备(具有 NFC 功能并激活开发者模式的 Oppo 设备)上对其进行了测试。

在应用启动时,似乎一切正常,NfcManager 已成功启动,但是当应用尝试请求读卡技术时出现问题,即我必须先将应用带入后台然后再次在前台显示消息requestTechnology 成功,否则,它根本不会被调用。

在此之后,承诺NfcManager.getTag()被拒绝并显示错误消息:没有可用的参考

这是我的代码:

componentDidMount() {
    NfcManager.start({
      onSessionClosedIOS: () => {
        alert('ios session closed');
      },
    }).then(() => console.warn('NfcManager started')) // successfully started
    .catch((error) => alert('Error starting NfcManager: ', error));

}

{... componentWillUnmount and render method ...}

 _read = async () => {
    try {
      let tech = Platform.OS === 'ios' 
        ? NfcTech.MifareIOS : [NfcTech.MifareClassic, NfcTech.NfcA, NfcTech.IsoDep, NfcTech.Ndef];
      let resp = await NfcManager.requestTechnology(tech, {
        alertMessage: 'Ready to do some custom Mifare cmd!'
      })
      .then((value) => alert('requestTechnology success', value)) // here the value is empty, so no NfcTech
      .catch(() => console.warn('reuqestTechnology error'));

      const tag = await NfcManager.getTag()
      .then((value) => alert('Tag event: ', value))
      .catch((err) => console.warn('error getting tag: ', err));

      // this part of the code is reached, but not working properly since the tag.id is not correctly retrieved
      if (Platform.OS === 'ios') {
         resp = await NfcManager.sendMifareCommandIOS([0x30, 0x00]);
      } else {
         resp = await NfcManager.transceive([0x30, 0x00]);
      }
      console.warn('Response: ', resp);
      this._cleanUp();
    } catch (ex) {
      console.warn(ex);
      this._cleanUp();
    }
  }

如果我在设备上扫描卡,它发出的声音就像它已被扫描,但似乎没有显示任何内容。

有谁知道为什么需要将应用程序带到后台以便请求技术?其次,getTag()方法的失败与它有什么关系吗?

我希望任何人都可以帮助我解决这个问题,我已经为这个问题苦苦挣扎了很长一段时间,但我还没有找到任何解决方案。

4

1 回答 1

0

可能与github.com/revtel/react-native-nfc-manager/issues/423有关??似乎有一种情况,这个包没有正确配置 enableForegroundDispatch 并通过将其发送到后台手动暂停和恢复应用程序将修复它。

于 2022-01-12T13:56:06.357 回答