chariotsolutions / phonegap-nfc

PhoneGap NFC Plugin
MIT License
706 stars 557 forks source link

An example or link to a bulletproof method of reading a tag and it's payload (if any) #253

Closed mm108 closed 7 years ago

mm108 commented 7 years ago

hi,

I was wondering if there is a real-world example on reading NFC cards (the tag, and the payload if any). What I mean my "real-world" is that it should be able to employ multiple methods (listeners or something?), with a decent error handler.

I remember having seen Don mention about "if-not-this-then-that-method" (to increase the chances of reading a nfc card) somewhere but I just can't find the post / link no matter how hard I try :) I am attaching details of the NFC cards that I usually be reading with my app. And also thanks a ton for the excellent plugin.

screenshot_2016-02-05-12-20-30 screenshot_2016-08-24-13-03-11

Cheers, M.M

don commented 7 years ago

There's really no simple answer.

Reading NDEF tags is pretty straight forward with addNdefListener, but you need to write code to read the TNF and Type and decoding the payload.

If you want to handle non-NDEF tags you can add a addTagDiscoveredListener

I use addMimeTypeListener to handle tag reads that launch the app from an intent filter.

I use addNdefFormatableListener to handle unformatted tags that can have NDEF messages written to them.

It's fine to all all 4 listeners added in one app. Android will call the most specific listener for any tag. It's also find (and often preferable) to share event handlers across listeners so you don't have to duplicate logic.

HTH