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.
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
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.
Related
If a tag is already scanned, Android can start my activity, so good. Now I did a new activity started from the user, to allow the user to write the tag. When the activity is already open, I can receive the intent from the system only if the user re-scan the tag even if the phone is already on the tag, is there a way to force the rescan?
You can't "rescan" the tag, this is not possible because of how the NFC works. When you have the NFC enabled in your phone the NFC antenna is always searching for a tag. So rescan the tag doesn't make any sense.
You have two options, the first one as I have pointed in my comment is sharing the Tag object between your activities and do whatever you want with that.
The second option if you want to write the tag after the user has opened your application is using the onNewIntent. This way you can capture the Intent that will be sent from the android system after the user has pointed the NFC tag close to the NFC antenna.
I am showing my application in list of options available to get NFC tag when its trigger.
I don't want to show the options if my application is already open.
Is it possible? and how?
When you want to receive NFC intents while your app is in the foreground, you should register your activity for the foreground dispatch system. That way, your activity gets precedence over other apps that registered for the same tag type in their app manifest. Thus, while your acitivity is in the foregorund, no activity chooser will be shown even if another app also has intent filters for these events.
Ok, I have an app. This app will only complete a task when an nfc tag, any tag, is scanned. Only problem, I do not have any nfc tags. And I am trying to eliminate needing a card anyway, So What I need is a way to "Fake/Make It Look" Like an nfc tag has been scanned. I can write apps and such so all I really need is the core code to make android think a tag was scanned. I can do the rest. I just need to be able to push a button, then android think that a tag was scanned so that the app will be invoked. Thank You Guys
Write an app that broadcasts the NFC intent you'd like to emulate upon launch, and then closes. So a simple app with a single activity that does roughly this in its onCreate:
Intent intent = new Intent("android.nfc.action.NDEF_DISCOVERED");
startActivity(intent);
finish();
Then your app should volunteer to handle it as though it had been read with an NFC reader.
In the end Thomas is right, you should just buy an NFC tag and be done with it, that way you know that it's doing what you want it to for normal nfc tags.
If that doesn't quite float your boat, another option is to add a long-running notification, upon the click of which, it does the intent broadcast. This way you won't have to go back to the main menu to get it to work.
I'm writing a handful of NFC-capable apps for work.
I've got two of them on my tablet right now, and they are set to launch if an NFC tag is detected and they're not already open. So since I have two of them on the same device now I get an App Picker dialog. This is great.
What I would like is to make it so that if one of the apps is already open, that when the NFC tag is detected it doesn't show the app picker, but just uses the current activity to handle the NFC intent. How possible is this? Thanks
If the foreground activity is using enableForegroundDispatch(), it will take precedence over anything else registered in the manifest for the tag.
Here is a sample app that demonstrates the use of enableForegroundDispatch(), to write text shared from another app (e.g., URL from the Browser) to an NFC tag.
I created an NFC app. I designed it so only one of its activities can process an NFC tag. When another activity is active (same app), and the tag is scanned, another app steals focus and sends my app to the background. How can I prevent all other NFC apps from stealing focus while my app is running in foreground?
You can't really. Your only option is to have all your activities use foreground dispatch to make sure your app handles the tag if it is visible. You can probably use a base activity to share the code.