chariotsolutions / phonegap-nfc

PhoneGap NFC Plugin
MIT License
712 stars 570 forks source link

NdefMessage decoding problem (smart poster) #92

Closed Ptijohn closed 11 years ago

Ptijohn commented 11 years ago

Hi.

I've been working with your plugin for a week, and it's really awesome. I don't have any problem writing or reading any kind of tags, except for decoding a smart poster. I'm trying to read the content of a smart poster composed of a text record, and an url record.

Here's what i'm doing (i should loop through the array, but i'll do that later): var decoded = ndef.decodeMessage(ndefMessage[0].payload); alert(nfc.bytesToString(decoded[0].type)); alert(nfc.bytesToString(decoded[0].payload)); alert(nfc.bytesToString(decoded[1].type)); alert(nfc.bytesToString(decoded[1].payload));

There is no problem in decoded[0], the type is "T" and my content is in the payload. However, in decoded[1], where there should be "U" in type and my Url in the payload, everything is stored in type : "Uhttp://www.something.com". And there is nothing in the payload. My guess is, there is no problem in the encoding of the smart poster (the phone is reacting well to it, it opens the browser with the url). The problem seems to be coming from the loop in decodeMessage method.

I think that after this line: if (header.me) break; // last message There should be this: bytes.shift();

I explain, when it loops after the first record, type_length and payload_length seems to have a wrong value. Moreover, their values seem to be shifted (type_length gets last value of precedent record, and type_payload get the desired value of type_length). Therefore i thought of a missing bytes.shift(); line.

I hope that you understand what i mean :) (and sorry if i made any English mistakes...) And i hope that my solution is good.

Thanks again for your work.

don commented 11 years ago

Thanks for reporting this. Nice catch. The fix is a bit different than you described, but the same principal. Before the fix the code was only reading the TNF from the first record.

JohnMcLear commented 11 years ago

Thanks @don and @Ptijohn

Ptijohn commented 11 years ago

Thanks for your reply and your fix! I'll try to report whatever I find wrong in the code. Oh, and i found something weird, but i'm not sure if it's wanted or not. In NfcPlugin.java (for Android), at line 350 to 352: if (action.equals(NfcAdapter.ACTION_NDEF_DISCOVERED)) { Ndef ndef = Ndef.get(tag); fireNdefEvent(NDEF_MIME, ndef, messages);

Why is the event fired of type "NDEF_MIME" ? Since the action discovered is ACTION_NDEF_DISCOVERED ? Shouldn't it be of type "NDEF" ? (like 10 lines below) I think i'm missing something in the behaviour of the plugin. I'd appreciate a lot if you could just explain this briefly :)

don commented 11 years ago

That behavior is intentional. I'm not sure I'd write it the same way today, but the behavior is OK.

Mime Messages are NDEF messages but they are treated separately.

The idea is that it's easy to filter for only the messages you care about by filtering by mime type https://github.com/chariotsolutions/phonegap-nfc#nfcaddmimetypelistener

You can use the same listener to process mime messages or ndef messages.

If you don't want/need this behavior, just use addNdefListener

Ptijohn commented 11 years ago

Ok, now I understand what you did. Thanks for taking time to answer.

I don't really need this behaviour, so I think I'll just stick with addNdefListener.

Thanks