Search code examples
androideclipsetextnfcwriter

My text writer can't be read by my NFC reader


so, i make an NFC project, but when i write a text on a tag, my reader can't read it, is there somethin wrong with my format?

this is the text writer format code:

private NdefMessage getNoteAsNdef() {

    byte[] textBytes = mNote.getText().toString().getBytes();


    NdefRecord textRecord = new NdefRecord(NdefRecord.TNF_MIME_MEDIA, "text/plain".getBytes(),
            new byte[0] , textBytes);
    return new NdefMessage(new NdefRecord[] {
        textRecord
    });
}

thanks before


Solution

  • Maybe you're running into charset encoding issues. Try the following instead to create your NdefRecord:

    byte[] textBytes = mNote.getText().toString().getBytes(Charset.forName("US-ASCII"));
    new NdefRecord(NdefRecord.TNF_MIME_MEDIA, "text/plain".getBytes(Charset.forName("US-ASCII")), new byte[0], textBytes);
    

    To create a text tag you can also use the following:

    byte[] textBytes = mNote.getText().getBytes();
    byte[] textPayload = new byte[textBytes.length + 3];
    textPayload[0] = 0x02; // 0x02 = UTF8
    textPayload[1] = 'e'; // Language = en
    textPayload[2] = 'n';
    System.arraycopy(textBytes, 0, textPayload, 3, textBytes.length);
    NdefRecord record = new NdefRecord(NdefRecord.TNF_WELL_KNOWN, NdefRecord.RTD_TEXT, new byte[0], textPayload);