1

I'm trying to write to a tag but I don't want to send a text, I want to send an array of bytes because that way I'll have a better sending control and I'll be able to establish fixed data according to the position of the array when I read it, I was investigating and I didn't find anything in particular .

The tag I have has NfcV and Ndef as techlist. I tried MifareClassic but it doesn't show up as null. Any other ideas please.

I want to emphasize that the byte array must be exclusively the data that I send, because I have seen other scripts like NdefRecord but they respect parameters at the beginning of the frame by writing data that I do not want but that function needs it to write.

private void write(String text, Tag tag) throws IOException, FormatException {
    //byte[] data= Const.ResponseDataDeviceWrite;
    //NdefRecord records = new NdefRecord(data);
   // NdefRecord[] records = { createRecord(text) };
    byte[] data = {66,104,111,108,97,32,32,32,32,32,32,32,32};
    //records=data;
   // NdefMessage  message = new NdefMessage(records);
    //NdefMessage message = createRecord(text);
    // Get an instance of Ndef for the tag.
    Ndef ndef = Ndef.get(tag);
  
    // If Ndef.get is null then try formatting it and adding message
    if (ndef != null) {
        // Enable I/O
        ndef.connect();
        // Write the message
       /* NdefRecord[] records = {
                NdefRecord.createMime("text/plain",  data)
        };*/
        //NdefMessage  message = new NdefMessage(data);
        ndef.writeNdefMessage(new NdefMessage(new NdefRecord(NdefRecord.TNF_UNKNOWN, null, null, data)));
        //ndef.writeNdefMessage(message);
        // Close the connection
        ndef.close();
    } else {
        NdefFormatable ndefFormatable = NdefFormatable.get(tag);
        // Really should do a null test on ndefFormatable here but as the code is looking for an exception don't test for null
        ndefFormatable.connect();
        // Format at write message at the same time
       // ndefFormatable.format(message);
        ndefFormatable.close();
    }

}

Example what came out in writing and what should be, previously it was possible to write but in an application in c#

enter image description here

Mauricio
  • 11
  • 2
  • Does this answer your question? [ISO15693 (NfcV) / Tag-it HF-I commands throw tag lost exception](https://stackoverflow.com/questions/29903127/iso15693-nfcv-tag-it-hf-i-commands-throw-tag-lost-exception) – Andrew Sep 12 '22 at 18:36
  • I saw that it only focuses on reading and not on writing, which is what I want – Mauricio Sep 12 '22 at 19:09
  • The answer has a section on "WRITE_SINGLE_BLOCK" so covers what you want. – Andrew Sep 12 '22 at 19:16
  • I did it as is and I get an error android.nfc.TagLostException: Tag was lost. – Mauricio Sep 12 '22 at 19:43
  • I also understand that transceive is not to write in the tag or is it? :c – Mauricio Sep 12 '22 at 19:44
  • `transceive` is a generic low level method, to read,write or do any other operation, usually the beginning of the byte array specifies if the command is to read or write or other operation. NfcV the main method is `transceive` the class does not provide higher level wrapper methods like `writeNdefMessage` or Mifare `writeBlock` that just `transceive` with the right command. – Andrew Sep 12 '22 at 19:50
  • I found my exact case, I did it as it is and I get that it was saved correctly but when I read the byte array it didn't change anything :C, I've been on this for a week and I still don't get help please https://stackoverflow.com/questions/55856674/writing-single-block-command-fails-over-nfcv – Mauricio Sep 12 '22 at 22:12
  • It would help if you mentioned the exact make and model of the Tag as the datasheet for the Tag might help people answer. – Andrew Sep 12 '22 at 22:21
  • haaaaaaaa This is the guide, I've been reading it but I can't quite understand the solution :/, thanks for the help anyway https://www.ti.com/cn/lit/ug/scbu003a/scbu003a.pdf?ts=1616817192509 – Mauricio Sep 12 '22 at 23:01

1 Answers1

0

There is a Mifare Classic-specific solution as they have a java library. You just need to identify the tag as MifareClassic and then you can use the writeBlock() method.

https://developer.android.com/reference/android/nfc/tech/MifareClassic#writeBlock(int,%20byte[])

You can initalize a tag using for example MifareClassic mifareTag = MifareClassic.get(tag);

Make sure to import android.nfc.tech.MifareClassic;

oskhen
  • 11
  • 2