0

I'm trying to use the Tag Object for my Flutter Plugin. Using MethodChannel I can call the Java functions but my Tag Object remains null. How would I correctly create this Intent?

else if (call.method.equals("readTag")) {
      Intent intent = new Intent("android.intent.action.MAIN");
      Tag nfcTag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);

My guess is that the Intent can't be create in this fashion and thats why it returns a null Object for the Tag.

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
notoriousJ
  • 21
  • 3

1 Answers1

0

As you have created a new and empty Intent there is nothing to read from it. So as expected.

An Intent are really just a generic storage class used generally to pass data in a standard format between one Activity and another.

They use keys to pass specific data between Activities. One of the keys used by the the System NFC Activity to pass NFC related data to your Activity is the NfcAdapter.EXTRA_TAG key.

The Intent has nothing to do with actually reading an NFC Tag, they are just used as a communication method to ask the System NFC Activity to do stuff and how it can returns the data.

So you can create them anywhere you want, with lots of different content and pass them to any Activity to give it data or receive them to be sent data.

You seem to have a miss understanding how NFC works in Android.
The basic concept is.

Old API's
You create an Intent with basically a "message" to the System NFC App say "I would like to be sent data about specific types of NFC data when one comes in to range", you send that Intent message to the System NFC Activity using enableForegroundDispatch, when the System NFC App sees a Tag of the right type come in to range it creates an Intent to store the data about the NFC tag to send back to the activity that asked for it.
This is using a generic method for inter process communication.

Newer API's
Do something similar when you enableReaderMode you use an NFC specific method to send data again about about what Tags you are interested in and it sends back the in a more specific format when it sees a Tag of the requested type come in to range.
Example of newer API's https://stackoverflow.com/a/64921434/2373819

Manifest Filter The Manifest Filter are independent of the Old and New API's, they are about storing information that the System NFC App can use to decide if it should start your App when it sees a particular type of NFC Card. They can be used with both Old and New API's but they pass an Ìntent message using the same fields as the Old API to your App, this can be read and processed from onCreate using getIntent.

So creating a new and empty Intent and expecting to magically get some NFC data is wrong.

Andrew
  • 8,198
  • 2
  • 15
  • 35
  • So using Intents out side of the Activity Class doesn't work? – notoriousJ Jan 22 '21 at 12:11
  • Updated answer, Yes they do "work" outside the activity class but possibly not as you expected. – Andrew Jan 22 '21 at 12:32
  • Thank you Andrew, after a bit of research I noticed there is not much documentation around the new API enableReaderMode. Do you maybe know a Link you could share to read more about it. – notoriousJ Jan 23 '21 at 10:34
  • Updated Answer adding a link to a New API example, the new API has full reference documentation just it does not have a "how to" guide of how to connect it together which the example should provide. I also added how "Manifest Filters" fit in to the mix. – Andrew Jan 23 '21 at 11:26