react-native-nfc-manager icon indicating copy to clipboard operation
react-native-nfc-manager copied to clipboard

[Android] Native tag scan window keeps popping up upon reading tag even though NfcManager.start() is earlier called

Open strid408 opened this issue 3 years ago • 12 comments

Similarily to issue #381, the native android nfc scan keeps popping up upon scanning an nfc tag.

As suggested, I initially call NfcManager.start() without errors but still keep getting redirected to the native android tag screen when scanning a tag.

On another note, I have no issues reading tags connecting the readNdef function to a button, using example code from the documentation.

Device: Samsung Galaxy S9 Version: Android 9

strid408 avatar Aug 12 '21 08:08 strid408

I'm having the same issue for Samsung A31s any update on this?

saiganesh-tanneru avatar Sep 21 '21 15:09 saiganesh-tanneru

Me too, noticed that it happens when I have just turned on NFC on my android phone...If I have turned it on some moments earlier I do not see the native popup. Maybe start() crashes silently? @whitedogg13 Do you have any idea about this one? Thanks! (Honor View 20, latest version of library)

Bad-Listener avatar Oct 27 '21 08:10 Bad-Listener

Sorry for the late response. Have you tried to add intent filters into AndroidManifest? For example:

        <intent-filter>
          <action android:name="android.nfc.action.TECH_DISCOVERED"/>
        </intent-filter>
        <meta-data android:name="android.nfc.action.TECH_DISCOVERED"
                   android:resource="@xml/nfc_tech_filter" />

Full example please check here

whitedogg13 avatar Oct 29 '21 01:10 whitedogg13

I overcome the issue by setting a 2500 ms timeout after NfcManager.start(). This way it seems to scan properly every time, but i ll try this one too and post the results!. Thanks a lot for your prompt reply!

Bad-Listener avatar Oct 29 '21 08:10 Bad-Listener

Hey @whitedogg13, unfortunately the extra declaration on Android Manifest did not solve it. The only case that is broken is if user disables NFC and then enables it during the lifetime of the app. In this case, the first scan is handled by native android. The second tag I scan is always handled by react-native-nfc-manager

Bad-Listener avatar Nov 03 '21 10:11 Bad-Listener

Does this happen when the user is using "Quick" settings to turn of NFC not the full setting App? My guess the answer is yes to using the "Quick" settings as this does not pause and resume the App thus the package won't know about the change in status of the NfcAdaptor.

Really the package needs a Broadcaster Receiver to get notified when the state of the NfcAdaptor, gets turned on for it to re-enable it's foreground detection.

Something like below in the Android code should fix it

In startup

// Register the BroadcastReceiver
// Listen for changes NFC settings
        IntentFilter filter = new IntentFilter(NfcAdapter.ACTION_ADAPTER_STATE_CHANGED);
        this.registerReceiver(mReceiver, filter);`

Then

private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            final String action = intent.getAction();

            if (action != null && action.equals(NfcAdapter.ACTION_ADAPTER_STATE_CHANGED)) {
                final int state = intent.getIntExtra(NfcAdapter.EXTRA_ADAPTER_STATE,
                        NfcAdapter.STATE_OFF);
                switch (state) {
                    case NfcAdapter.STATE_OFF:
                        // Optionally Send a message to user about are they sure about turning NFC off
                        break;
                    case NfcAdapter.STATE_TURNING_OFF:
                    case NfcAdapter.STATE_TURNING_ON:
                        break;
                    case NfcAdapter.STATE_ON:
                        // Enable NFC as per in onResume
                        break;
                }
            }
        }
    };

Zardozz avatar Jan 12 '22 10:01 Zardozz

Same issue on Samsung S9+. I can't properly read a tag, the native NFC app keeps popping up

fabien-roy avatar Apr 07 '22 16:04 fabien-roy

Hi, any update to this issue? It happened on my several phone test like Redmi Note 8 Pro and Samsung Galaxy A32. but on Poco F3 Pro and Oneplus 3 seems normal

ramzitm91 avatar Jun 21 '22 08:06 ramzitm91

Same issue on Samsung S10. I am reading the tag and waiting for the information inside of it, then i am starting a new NfcManager.start() to write on the tag, but when the nfc comes closer the native NFC app keeps popping up. Please if anyone knows a solution i am all ears. @whitedogg13

fpiantoni avatar Jun 23 '22 15:06 fpiantoni

Hi @fpiantoni , from the description of your issue, I think you might misunderstand our API usage, can you try our demo app to see if it works on your phone? Google Play link

This app is also written in RN and use our own library, so we can further check what's going wrong. The app's repo

FYI, I'm testing this app via Samsung Note 10 and it's working properly.

whitedogg13 avatar Jun 25 '22 10:06 whitedogg13

Hi @Bad-Listener , sorry for the super late response again.

I understand you found a workaround for the original issue by putting an extra 2500 ms delay, as reported here.

But I'm also curious about whether this happened in our demo app, Can you help to test on your phone to see if it happened? Much appreciated!

whitedogg13 avatar Jun 25 '22 10:06 whitedogg13

@Bad-Listener , I forgot to mention, if the on-off switching during app lifetime is bothering you, you can consider to listen to NfcEvents.StateChanged event and disable NFC related feature in your UI accordingly.

For example, that's say we have a React state called enabled, which is used to tracking if our device's NFC has been enabled and users are allowed to use NFC related feature only when enabled is true.

Based on this, we can then listen to NfcEvents.StateChanged and change the value for enabled state:

          if (Platform.OS === 'android') {
            NfcManager.setEventListener(
              NfcEvents.StateChanged,
              ({state} = {}) => {
                // disable any pending tech requests
                NfcManager.cancelTechnologyRequest().catch(() => 0);
                if (state === 'off') {
                  setEnabled(false);
                } else if (state === 'on') {
                  setEnabled(true);
                }
              },
            );
          }

Full example can be found here

whitedogg13 avatar Jun 25 '22 12:06 whitedogg13

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] avatar Sep 24 '22 02:09 github-actions[bot]

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

github-actions[bot] avatar Oct 10 '22 02:10 github-actions[bot]