Search code examples
react-nativenfc

NFC using foregroundDispatch in react-native and android


I'm trying to develop an app in react-native that should use android's foreground dispatch system to intercept nfc events before any other activity does. The android part is not a problem, I have done it in a native app.

What would the correct way of doing this be in an app that uses react-native?


Solution

  • I ended up adding creation of pending intent and adding of tag/NDEF filters to MainActivity. Then onNewIntent parses the relevant data and relays it to js via

    getReactInstanceManager()
      .getCurrentReactContext()
      .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
      .emit("nfcTagEvent", params);
    

    Then, in my main RN component

    DeviceEventEmitter.addListener('nfcTagEvent', event =>
      // do something with event
    );