Closed halindrome closed 6 years ago
I don't think you can erase a tag that isn't formatted. Try removing the erase and just writing the message. IIRC it will format and write the message.
BTW I tried this and the write failed. Can you imagine a path that will get us from "formatted in some unknown format" to "formatted as NDEF and written with an NDEF record" ?
It looks like nfc.erase
is writing an empty NDEF record to the tag. If your tag is blank, this will format and write the empty message. If you tag has an NDEF message erase will overwrite it. https://github.com/chariotsolutions/phonegap-nfc/blob/master/src/android/src/com/chariotsolutions/nfc/plugin/NfcPlugin.java#L227-L234
Calling nfc.write will format the tag if necessary before writing the NDEF message. I'd expect that's what's failing.
If there is non-NDEF content on the tag, it'll probably fail. Can you see what happens with NXP Tag Writer when you use "Erase to Factory Default"? That should blank the tag and allow you to write NDEF messages to it.
Not all NFC tags can be erased. NXP Tag Write fails on Mifare Classic tags when some sectors have different keys. You can write custom code to fix this, but generally I just get a new tag. If it won't format, I'd look at the bytes on the tag using NXP Tag Info or NFC TagInfo to determine what's wrong.
I'm having the same issues as @halindrome using Mifare Ultralight cards. Both nfc.erase
and nfc.write
fail with blank cards. I used NXP Tag Writer to format the cards using the "Erase to Factory Default" option, only then both methods worked as expected.
I don't have a good fix for this. In the past my devices would format before writing. For now I think you just need more error handling for unformatted tags.
I have a bunch of blank tags that we are using for a project. I have set up a handler to get called when there is an NDEF Formatable tag discovered. That handler is getting called. Within the handler the logic is:
This handler gets called, and when inspecting I can see that the formatting failed clause is being called... but there is no parameter so I don't know why. When the event handler is called, the "tag" property contains:
I did run an adb logcat while trying this. Here is the relevant output:
Any ideas why it might not want to format? Or any additional information I can capture?