简体   繁体   中英

Android | NFC - NFC Discovery bugs out on activity switch and opens new tag collected over our activity

We are currently experiencing a weird Problem with new Android Devices. We finally updated from our Android 6. Handhelds to Android 10 and greater. Which helps us a lot but at the same time opened a real serious Problem.

We use our Handhelds for a lot different things but on of the main reasons is to identify and execute things on the discovery of a NFC Tag.

And we are experiencing that if the NFC discovery starts an activity switch it happens that instead of the new activity the screen ("New Tag Collected") pops up. The new Activity Starts in the Background. On Android 6 and before this worked fine. And we don't find anything related to it.

We think it has something to do with NFC Tag scanned -> Start Activity Switch -> NFC Adapter disabled -> Device Reads -> NFC Tag again -> New Activity started

Video to the Issue It opens new tag collected, and in the background the activity which should start

Android Device

Device: Zebra TC26BK
Android Version: 10 
Kernel: 4.4.205-perf
MinSdk: 21

NFC Tag

Manufacturer: NXP
Model: NTAG213
Type: NFC Forum Type 2 TAG
Technology: NfcA, MifareUltralight,Ndef
Size: 144 bytes
Writeable: Yes (not used)

And the a test Repository with the code. Repo

Thank you in advance if someone knows why this happens.

So my thoughts are because you are doing something you logically are not supposed to be doing you are confusing the system and now with the new OS and faster CPU's your unpredictable outcome is different than before.

So it looks like from the video the card is triggering the in built system NFC App to display the "Empty Tag" message.

Because you are enabling reader mode and foreground dispatch at the same type you are basically asking the system to do 2 things every time it sees a Tag

So again my thoughts on what is happening now in the System NFC service.

The Tag is detected.

The enableReaderMode callback starts the Second activity and this now becomes the foreground activity.

The NFC system service say "I now need to deliver an Intent to Main activity when it is in the foreground"

Oh Main activity is not in the foreground and the current foreground activity (The second activity) does not want to be delivered an Intent (as it does nothing with NFC enableForegroundDispatch).

Lets look for another App that wants to be started when this type of Tag is detected (Checks for Manifest entries)

There is no App that wants to be started, therefore the system NFC service defaults to staring the in built NFC App (which displays the empty Tag)

The fact in the video that when you press the back button the "SecondActivity" is there show that the enableReaderMode part has worked.

Previously

Due to a different timing because of a different CPU speed and more likely less CPU cores.

When the system NFC service handled the second NFC request to process the foreground dispatch, the "SecondActivity" was not running yet therefore it managed to deliver the Intent to "MainActivity" and generate 2 requests to the system to start the same activity (SecondActivity), the second of these requests would just bring SecondActivity to the foreground but would actually do nothing because the SecondActivity was already in the foreground.

Solution

Don't use enableForegroundDispatch and enableReaderMode at the same time in the same App, they logically do the same thing. I would recommend just using enableReaderMode as it is the better of the 2 API's

I also note that your code creates a bad IntentFilter which it does not use.

An ACTION_TECH_DISCOVERED IntentFilter does not take mimeType options.

eg the addDataType("*/*"); has no affect only ACTION_NDEF_DISCOVERED IntentFilters take mimeTypes

Also note that as you don't seem to be doing anything with NDEF data you probably want to add the FLAG_READER_SKIP_NDEF_CHECK flag to enableReaderMode , it just speeds things up.

Summary

The code is doing exactly what you asked it to do, but a different speed of code execution on a new device is leading to a different result.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM