Android Question Basic question to NFC functionality

Nickelgrass

Active Member
Licensed User
Longtime User
Hello,
I would like to make an app with a service that can "detect" NFC tags. So the question is if it is possible to detect an NFC tag from a service while the screen is off and the app is not running. I know there is the possibility of the intent filter and get a broadcast. But a basic test of this https://www.b4x.com/android/forum/threads/reading-ndef-data-from-nfc-tags.14931/ I did did not work.
I use these cards: RFID-Card für Modul RC522, 13,56 MHz (https://www.reichelt.de/entwicklerb...bo-rfid-card-p201860.html?&trstct=pol_4&nbc=1). The cards work with the NFC Tools app.
I just would like to know if this is basically possible or if the app has to be running all the time with screen on.
Thanks
 

drgottjr

Expert
Licensed User
Longtime User
if your manifest is set up correctly, your app can be
launched when an nfc tag is discovered. you don't
need something running in the background, even if
it were possible. and, therefore, your app doesn't
need to be running all the time when the tag is
discovered. it just needs to be launched, which
the system will do if your manifest is correct.

nfc discovery intent filters are activity filters. that
is, they launch an activity. and nfc handling must
be done in the foreground. with the applicable
intent filter, your app might be called to read the tag.
without the applicable intent filter, you app will not
be launched. but you might still be able to read the
tag if your app is already in the foreground.

if multiple apps have the same intent filter(s), the
user has to decide which gets launched (unless your
app is already in the foreground). note: just because
your app is in the foreground when the tag is detected
does not mean you will be able to read it. the tag may
support a technology not recognized by your app.

the tag you mention is mifare classic 1k compatible,
android can deal with it.
 
Upvote 0

Nickelgrass

Active Member
Licensed User
Longtime User
Ok thanks for the detailed replie. Would this also work for a foregroundservice? Is there an example somewhere for such a manifest?
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
i do not believe there is anything passed from the system to a service (back- or foreground) relating to nfc. the system reacts when a tag is disovered. it then creates an intent and launches an app to handle the discovery (unless your app happens to be running and foreground dispatch has been set, in which case you don't need to be launched). there is no part in this for a service. nfc can only be handled in the foreground. what is it you think your service is going to do? bring your app to life? the system already does that. do you follow what i'm saying? if all you want is for your app to handle nfc discovery, you do that through the manifest. if you have something else in mind, you'd have to describe it.

there is a broadcast receiver relating to nfc, but you'll have to dig to find out what it does. there are only 2 actions, but i'm not sure either one is triggered under normal circumstances. but the question still remains: what do you think your receiver is going to do when triggered? launch your app?

there may be some examples here regarding nfc and the manifest. i'll leave it to you to type "nfc" in the little search box and do your research. the best documentation is android's. its nfc page refers to basic, advanced and hce. the nfc examples here in the forum cover basic and some advanced. android's advance documentation is what you want. it shows exactly what you need to do in the manifest. it works. just follow it. the setup is really just like any other intent filter that wants to handle an intent. since i don't know what you want to do with your app, it's hard to say exactly what your manifest should look like.
 
Upvote 0

Nickelgrass

Active Member
Licensed User
Longtime User
Thanks for the input. What I want to do is play an MP3 according to what NFC tag is held to the phone. I want that in a service because I do not want the app to be launched. I just want the phone to detect the NFC tag and then play an MP3. And this also when the screen is off.
 
Upvote 0
Top