react-native-hce icon indicating copy to clipboard operation
react-native-hce copied to clipboard

HCE_STATE_READ event is fired multiple times on tag reading

Open buenaondalab opened this issue 2 years ago • 3 comments

I'm trying to emulate a NFCtagType4 with an Android device. I'm able to read the tag but HCE_STATE_READ event is fired 3 times. Is that the desired behaviour? I fixed it locally in respondRead method inside NFCTagType4 class:

if (realLength == slicedResponse.length) {
      this.hceModel.getLastState()
                   .setValue(HceViewModel.HCE_STATE_READ);
    }

Could it be a valid solution to avoid multiple firings?

buenaondalab avatar May 17 '23 13:05 buenaondalab

Same here, it keeps bumping up and the only way to fix it is to close the app to reset the session . . even if the component is unmount and re-mount it triggers a lot

neilanthonyte avatar Nov 02 '24 12:11 neilanthonyte

any solution on how to fix this?

let removeListener = session.on(HCESession.Events.HCE_STATE_READ, async () => {
              Alert.alert(
                        'Success',
                        `NFC connection successfully completed.`,
                        [{text: 'Ok', onPress: async () => {
                            
                        }}]
                    );
            });

            removeListener();
        listener is not removed even if the component is unmounted . .

neilanthonyte avatar Nov 02 '24 12:11 neilanthonyte

This is still an issue as of July 2025.

Edit: I have found a temporary fix:

    listener = session.addListener("hceState", (eventData) => {
        console.log("HCE State Changed:", eventData);
    });
    ...
    listener.remove();

After adding debug logs to the onChanged function in EventManager.java, I verified that events are being emitted correctly to the React Native bridge. However, I noticed that the .on() implementation in the JavaScript layer triggers multiple times for the same event (specifically for 'read' events in my case) - so I figured to just listen to all of the events and this works for me.

Chiken983 avatar Jul 15 '25 12:07 Chiken983