0

I'm doing an experiment with NFC and I need to write to a MIFARE Classic tag, but its giving me an Exception because it needs 16 bytes. This would sound kind of noobish but I have tried everything and still it gives me the same exception:

 Caused by: java.lang.IllegalArgumentException: must write 16-bytes
        at android.nfc.tech.MifareClassic.writeBlock(MifareClassic.java:446)
        at com.kaissersoft.cartracker.MainActivity$1$1.doInBackground(MainActivity.java:80)
        at com.kaissersoft.cartracker.MainActivity$1$1.doInBackground(MainActivity.java:72)
        at android.os.AsyncTask$2.call(AsyncTask.java:297)
        at java.util.concurrent.FutureTask.run(FutureTask.java:237)

One of the pieces of code I used:

 mifare.connect();
 //DO STUFF
 mifare.writeBlock(4, "CAR 100000000000".getBytes(Charset.forName("UTF-16LE")));
Michael Roland
  • 39,663
  • 10
  • 99
  • 206
Eefret
  • 4,724
  • 4
  • 30
  • 46

1 Answers1

1

Well if you print out the length of this byte array it returns 32. That means that your choosen encoding uses more bytes per char than you might want.

You could try to use it with UTF8 that would at least give you the needed amount of bytes but be aware of what characters you try to send. See the link for an explanation on this.

"CAR 100000000000".getBytes(Charset.forName("UTF8"))

Community
  • 1
  • 1
simonides
  • 3,040
  • 3
  • 19
  • 34