somq / nfccard-tool

💳 🛠️ The missing toolbox for reading and writing NFC cards
23 stars 5 forks source link

Error writing short text #2

Closed Spooky12 closed 5 years ago

Spooky12 commented 5 years ago

Hello,

When I write a short text message (<9 characters) to a NFC tag, I get this error when I read the tag and try to parse it:

Uncaught (in promise) NdefExceptionMessages.ExMessageBeginMissing

I think the error come from the method prepareBytesToWrite, because I don't have any problem when I try to parse short messages encoded in a NDEF format by another program.

somq commented 5 years ago

This error comes from ndef-lib and is pretty self explanatory MB flag (message begin) is missing.

Can try to encode the same message using nfccard-tool and compare raw tags byte per byte?

Also, you can try to log or breakpoint directly in the ndef-lib where I set the line stop in the link above.

Pretty hard to tell you more without a repro or any code...

Spooky12 commented 5 years ago

I am using an ACR122 reader along with nfc-pcsc to read and write tags. And for the code to encode and write NDEF messages I'm using the same as your example, the only change I made is:

const message = [
  { type: 'text', text: myText, language: 'en' },
]

I tried to encode the text 'test' with both nfccard-tool and another program and this the buffer I get:

I also tried to encode the text 'testtest1' and I get the same buffer from both nfccard-tool and the other program: Buffer(18) [3, 16, 209, 1, 12, 84, 2, 101, 110, 116, 101, 115, 116, 116, 101, 115, 116, 49]

It looks like the message length is not added to the buffer when the text is <9 characters so the whole buffer is shifted by one. I don't know why it happens but I'll look at the code of the prepareBytesToWrite method to try to find out why.

Spooky12 commented 5 years ago

I found out why it happens, it's because of this:

let NDEFMessageLength = NDEFMessage.toByteArray().length; // second and final write
let NDEF_MESSAGE_TLV_L_LENGTH = new Buffer(NDEFMessageLength.toString(16), 'hex'); // 0x00 - first write

When I want to encode a short message (<9 characters), NDEFMessageLength < 16 so NDEFMessageLength.toString(16) length is 1 so there is an error when the buffer is created.

But if I add a 0 before then it works fine.

E.g. if I have NDEFMessageLength=15 then NDEFMessageLength.toString(16) is 'f' which will create an error at the creation of the buffer but if I add 0 before to get '0f' then it works fine, the NDEF message is correct and I don't get NdefExceptionMessages.ExMessageBeginMissing when I try to read my tag.

somq commented 5 years ago

Ok, I dug this one a bit and you are right. This error comes from a wrong conversion of decimal to hex. Anyway Buffer constructor usage is deprecated and these tricky converts are not ideal. Si I'm replacing by a standard Buffer.from and everything should be OK.

I will commit a package update as well as a fix asap, afterthen I will publish it to npm but it the meantime you should be able to test and work with master.

Let me know if everything is OK on your side...

Spooky12 commented 5 years ago

Great, thank you!

somq commented 5 years ago

Your welcome, published to npm aswell. Let me know if it works properly whenever you can.

Spooky12 commented 5 years ago

It works properly now, I can create NFC tags with messages that contain less than 9 characters, thanks.