Read NFC tag periodically - android

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.

Related

How to prevent my device reading NFC Tags in every Fragment when using NavigationComponent and a single Activity?

Background
I included Navigation Component in my app which basically means that I have a single Navigation Activity and multiple Fragments that display my Views and Data. The app has the ability to read NFC Tags from ID Cards.
I followed the implementation guide of Android Developers to include the capability of NFC Tag reading and in their example they edit the Activity in the AndroidManifest and enable the ForegroundDispatcher in their Activity. Since I only have a single Activity basically anywhere in my App the NFC Capability is available.
I'm using setIntent(intent) to delegate the incoming Intents to my Fragments. Only the Fragment that is supposed to work with NFC handles the Intent while the others just ignore it.
The Problem
The problem is that when I hold a NFC card close to the phone the phone starts to vibrate since it recognized a NFC device. This happens in every Fragment and is unwanted behavior.
Question
What can i do about this or am I stuck with this behavior? I read a bit about enabling the foregroundDispatcher in a Fragment but the answer was almost always to handle it in my Activity and the function also demands an Activity.
Don't use ForegroundDispatcher (unless you really need to do NFC before API 19) use the newer and better enableReaderMode method instead.
The Sound is generated by the system NFC App and with enableForegroundDispatch you have no control of the sound plus enableForegroundDispatch has a bad side effect of pausing your App to read the NFC Tag and can be unreliable at times especially if writing to the NFC Tag.
With enableReaderMode you get to control whether the system NFC App makes a sound or not with the FLAG_READER_NO_PLATFORM_SOUNDS flag.
So your design will have to change a bit as the onTagDiscovered method is called in another Thread in your application when a NFC Tag is discovered, that thread has to then communicate the data to the Fragment (as it cannot interact with the UI thread directly). One possible method to do this is the onTagDiscovered method in the Activity sets a value in a shared view model and then the Fragment that is interested in it "Observes" this value for change and reacts according including making a sound and vibrate itself.
Full example of enableReaderMode is at https://stackoverflow.com/a/64921434/2373819
Note only enabling ForeGroundDispatch NFC in a Fragment is a bad idea, as it is likely the System NFC App will still make a sound anyway when a NFC Tag is presented and because your App might not be handling the event because the Fragment is not running then system NFC App might also show it's own screen over your App as well.

Disable NFC functionality from within the app

The issue I'm trying to solve is as follows:
An NFC tag will trigger my app, from which I'm launching another app. What sometimes will happen is that the NFC tag will move and will be "re discovered" by the phone and launch my app once again. The moving of the NFC tag is something I can't control.
This really ruins the experience.
What I would ideally like to do is once my app is launched, NFC triggering will be disabled or paused, and once app is closed (OnDestroy()) I will enable / un-pause NFC functionality.
I couldn't find how this could be done and your help would be really appreciated. If you have another (code related) approach on how this can be solved, this is just as good.
I noticed this post from a couple years back : NFC disable adapter
But I hope there is another solution, being a system application is not an option and prompting the user the disable NFC is just as bad...
Thanks in advance.
You can create an activity-alias that handles the intent, disable it while your activity is running and re-enable it when it is destroyed.
You're assuming that you can saftely turn off an important functionality of the phone, and disable any other service and/or app that is dependant on it.
This is of course wrong. You should only make assumptions regarding your own app.
So what you're really trying to do is to disable the re-launch. This can be done in many ways:
Control your main Activity's launch mode, and thus your entire app's.
Handle the NFC intent correctly, via a Service that monitors the NFC state as well as your app's. As per #Michael's remark - since you can only associate an NFC intent with an Activity - you would have to create a non-ui Activity (one that does not call setContentView) to act as a BroadcastListener for this Service.
Tag your app, as suggested by #Tristan.

NFC forced rescan

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.

Detect Incoming Call Before Default Phone App

Is it possible to detect incoming call before default phone application, and prevent to active default phone application in Android?
Looks like it may not exactly be possible according to this -> Create a custom call handling Application.
Anyway more research may be required on your side to check if it can be. Multiple posts on Phone broadcast receiver can be used as reference.

Need To Fake an NFC Tag Being Scanned In Android

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.

Categories

Resources