I am quite new to android development and trying to read an nfc card and was wondering if it's possible to read it when the user presses a button. I know how to read a card once the card is near the reader or when it is tapped to the nfc reader (onTagDiscovered). I would like to know if it's possible when a user presses a button then the device will read the card.
Asked
Active
Viewed 391 times
0
-
do you mean physical device button , or a button in software UI ? – harveyslash Jun 08 '16 at 06:18
-
what I mean is a software UI – Jujumancer Jun 08 '16 at 06:20
1 Answers
0
Android not working as this.
- You could declare in your
AndroidManifest
that anActivity
in your application could read NFC tag.
When a tag is detected by system,
- if only your application declared that could reading NFC tags then your
Activity
is launched with discovered tag inIntent
- if multiple application declared that could reading NFC tags then a "popup" is shown to user to choose application
The other solution
- You use
enableForgroundDispatch
: https://developer.android.com/guide/topics/connectivity/nfc/advanced-nfc.html#foreground-dispatch
The foreground dispatch system allows an activity to intercept an intent and claim priority over other activities that handle the same intent. Using this system involves constructing a few data structures for the Android system to be able to send the appropriate intents to your application. To enable the foreground dispatch system
If multiple applications "listen" to NFC tag and your Activity
is in foreground and enableForgroundDispatch
then only your application, receive NFC discovered Tag .
With previous mechanism, you could catch all tags in your Activity
and when user press a button do required action, when user not press button, do nothing.

LaurentY
- 7,495
- 3
- 37
- 55