简体   繁体   中英

Reading NFC card using a button(Isodep)

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.

Android not working as this.

  1. You could declare in your AndroidManifest that an Activity 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 in Intent
  • if multiple application declared that could reading NFC tags then a "popup" is shown to user to choose application

The other solution

  1. 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.

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