I have a persistent problem when my application delivers multiple intentions in a return action after checking the NFC tag.
My application essentially scans the NFC tag, which has some data and sends it to the server. All in all, it works great. I understand that NFC works within a few cms, and maybe my problem is related to a user error, for example, a user waves his phone around a tag that can scan it twice in milliseconds.
I put a timer in the Application object. When the user scans the tag, I start the timer and set the flag to false, this flag is checked in the Activity, which processes the NFC intent, and the intent is deleted if the flag is false. This (should) stop several tag scans within 10 seconds.
My application is used to work with HomeCare, so the user scans the tag in the customers house. When they scan a tag the first time they log on to this client with data and a timestamp. A second tag scan will log out.
In general, it works, but sometimes the guardian will log in and log out on the first client, and then on the second client, and sometimes after a few hours, the guardian will be credited back to the previous client.
What, in my opinion, is happening is taking care of the wrong tag scan, which runs the NFC applet twice. The first intention is delivered immediately, and the second in a few minutes.
How are things. scan tag -> intent delivered to Activity -> start AppObj timer. write data to sqlite DB → run IntentService to send the transaction to the server (canceled after 10 seconds).
Can someone see the problem that I missed, can explain why the Intent duplicate comes in a few hours?
Can anyone suggest a solution that would stop this?
Is there a problem with how intent is handled in this activity? for example, the wrong code in the wrong activity lifecycle method?
Is there a way to cancel / intercept the second intention from the second incorrect scan?
Basically, I want nfcAdapter to be run 10 times in one second, but only the first scan gets delivered to the action. How can I achieve this?
I will send the code.
[edit2]
Toast.makeText(this, "about to test flags", Toast.LENGTH_LONG).show(); if ((intent.getFlags() & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) != 0) { Toast.makeText(this, "intent is from history", Toast.LENGTH_LONG).show(); }else{ Toast.makeText(this, "intent is not from history", Toast.LENGTH_LONG).show(); }
.
[Edit3]
<activity android:name=".NfcscannerActivity" android:screenOrientation="portrait" > <intent-filter> <action android:name="android.nfc.action.NDEF_DISCOVERED" /> <category android:name="android.intent.category.DEFAULT" /> <data android:mimeType="text/plain" /> </intent-filter> <intent-filter> <action android:name="com.carefreegroup.rr3.QRCODE_ACTION" /> <category android:name="android.intent.category.DEFAULT" /> <data android:mimeType="text/plain" /> </intent-filter> <intent-filter> <action android:name="android.nfc.action.TECH_DISCOVERED" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> <meta-data android:name="android.nfc.action.TECH_DISCOVERED" android:resource="@xml/nfc_tech_filter" /> </activity> <activity android:name=".EntryActivity" android:label="@string/app_name" android:screenOrientation="portrait" android:launchMode="singleTask" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> <intent-filter> <action android:name="com.carefreegroup.rr3.INVALID_CARER_TAG_SCANNED" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity>
. [Edit4]
Tag tagTest = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG); Ndef ndefTag = Ndef.get(tagTest); try { Log.e(TAG, "about to test io operations on Ndef Tag"); ndefTag.connect(); // this should already perform an IO operation and should therefore fail if there is no tag NdefMessage ndefMsg = ndefTag.getNdefMessage(); // this reads the current NDEF message from the tag and consequently causes an IO operation Log.e(TAG, "tested io operations on Ndef Tag, must be a real Tag!!!!!******!!!!!!"); } catch (Exception e) { // there is no tag or communication with tag dropped Log.e(TAG, "tested io operations on Ndef Tag, No Tag there, must be a re-delivery of the old Tag via an intent!!!!!******!!!!!!!!!!!!!!!!!!!!!!"); onResume(); } finally { try { ndefTag.close(); } catch (Exception e) { } }