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.
Related
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.
I need to read an NFC TAG which is fixed at the back of the phone periodically. The problem is that android will only trigger NFC intent the first time it detects the TAG. I think that any of this two possibilities could solve the problem, but don't know if android allows any of them
1- Force Android to read NFC TAG, but I don't know if it is possible, if it is, this would be the best solution, in that case, how could i do this?
2- Turn on NFC Adapter, when it turns on it will trigger NFC detection and my activity will consume that intent, but i think it is not possible to enable/disable NFC adapter programmatically without user interaction (I need it to be automatic, opening WIFI settings is not a possibility)
Thanks!!
Assuming your app is always in the foreground I would keep the initial Tag object in memory and periodically query the tag. If an exception is thrown when connect() is called then the tag has moved out of range.
Then set Tag object to null and listen for an intent being fired when the tag re-enters the phone's field.
It isn't possible to use Android's reader/writer mode when the screen is off or locked. Beside that there is an application that apparently can make this possible. Read this answer, it's an similar question.
I am new to android and i have developed an app locker.Now,i want that the application,i am locking should be unlocked with nfc tag that is without typing the password manually.How can i perform it?
As you already have the app locker part, I assume that when a user tries to launch an app, your app locker will instead show its unlock activity asking the user for a password.
On that unlock activity, you could now (instead or besides asking for the password) ask the user to scan the NFC tag. Your activity would therefore register for the NFC foreground dispatch system and wait until an NFC tag is scanned (or the user maunally types the password). As soon as your activity receives the NFC tag discovery intent through the foreground dispatch system, you could communicate with the tag and base your access control decision on the result of this communication.
As to what NFC tag/contactless smartcard you should use and what information you should store on it: That's a difficult question! This very much depends on your security requirements. In the easiest case, you could base your decision on the tag's (unique) identifier. However, you have to keep in mind that the identifier is neither unique nor unclonable. As an alternative you could use a (real) NFC tag and store an NDEF message on it. That NDEF message could contain some identifier/password that your app uses as an unlock credential. Again, NFC tags are publicly readable and therefore the NDEF message may be copied/cloned to another tag. Further security can be achived with tags that support cryptographic functionality beyond NFC Forum tag types, but discussion of that is certainly to broad for the StackOverflow format.
As I understand the technology, this isn't possible with the screen completely off. It's a security feature that the secure element only turns on when the screen turns on. So it should be a hardware limitation not a software one.
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.