chariotsolutions / phonegap-nfc

PhoneGap NFC Plugin
MIT License
706 stars 556 forks source link

How to read the scanned NFC that opened the app ? #350

Closed bluetiger30 closed 4 years ago

bluetiger30 commented 5 years ago

Hello,

Thanks for providing this great plugin.

I have added the following to open the app once an NFC card is detected and it works like a charm :

<intent-filter>
  <action android:name="android.nfc.action.NDEF_DISCOVERED" />
  <data android:mimeType="text/pg" />
  <category android:name="android.intent.category.DEFAULT" />
</intent-filter>

However, that doesn't provide me with the data of the scanned card, the only way I'm getting the data is by scanning it again after the app is opened. so how can I make the open once it discovers an NFC card and gets its data as well without the need to scan the card again

I have a look at android developer guide and found that there is an intent that enables this but I don't know where to put it and how to use it with phone gap: EXTRA_NDEF_MESSAGES.

I'm using the following code which is an example of a NFC card reader: https://github.com/don/phonegap-nfc-reader

simonGleamorb commented 5 years ago

Maybe cause your function to read the event inside your app is too far from the instant when you first scan your tag. Your app is maybe heavy.

For my app, i was facing the same issue. Interact with the event only 50% of the time. So i moved the get event function close to the top of my code. Set this event tag inside LocalStorage variable to use it when the app is completely loaded.

It worked for me.

don commented 5 years ago

You need to use nfc.addMimeTypeListener() to get the NFC data when launching the app. You can add both a MimeTypeListener and the NDEF listener in your code. Android will call the most specific listener. Typically for a case like this I register the same function for both listeners.