-1

I try to send a specific string through NFC. The key of this app is that the string should be received by the TAG as hex values, not ASCII. So, this is the problem. The NdefMessage() format the string as hex, even if it is already hex.

here is the code:

byte[] lang = new byte[0];
try {
  lang = Locale.getDefault().getLanguage().getBytes("UTF-8");
} catch (UnsupportedEncodingException e1) {
  e1.printStackTrace();
}
byte[] text = new byte[0]; // Content in UTF-8
try {
  text = sendDdata.getBytes("UTF-8");
} catch (UnsupportedEncodingException e1) {
  e1.printStackTrace();
}
int langSize = lang.length;
int textLength = text.length;
ByteArrayOutputStream payload = new ByteArrayOutputStream(1 + langSize + textLength);
payload.write((byte) (langSize & 0x1F));
payload.write(lang, 0, langSize);
payload.write(text,0,textLength);
byte[] message1 = payload.toByteArray();
NdefMessage record = null;
record = new NdefMessage(new NdefRecord(NdefRecord.TNF_WELL_KNOWN, new byte[0], new byte[0], message1));

if (tag != null) {
  try {
    Ndef ndefTag = Ndef.get(tag);
    if (ndefTag == null)  {
      NdefFormatable nForm = NdefFormatable.get(tag);
      if (nForm != null) {
        nForm.connect();
        nForm.format(record);
        nForm.close();
        Toast.makeText(MainActivity.this,"format",Toast.LENGTH_SHORT).show();
      }
    }
    else {
      ndefTag.connect();
      ndefTag.writeNdefMessage(record);
      ndefTag.close();
      Toast.makeText(MainActivity.this,String.valueOf(record),Toast.LENGTH_SHORT).show();
    }
  }
  catch(Exception e) {
    e.printStackTrace();
    Toast.makeText(this,"Please attach to Tag!",Toast.LENGTH_SHORT).show();
  }

and with the debugger it returns something like: string that should be send:

"41542b5733322c340DFD020300"

payload:

"en41542b5733322c340DFD020300"

message sended:

NdefMessage [NdefRecord tnf=1 payload=02656E3431353432623537333333323263333430444644303230333030]

Is any way to send just as hex, without converting it again to hex?

And second question: is it any way to send a Ndef message without header? only the payload?

Thank you in advance.

Mihriban Minaz
  • 3,043
  • 2
  • 32
  • 52
Mihai Coman
  • 73
  • 1
  • 9

1 Answers1

1

You are creating a record with the tnf "TNF_EXTERNAL_TYPE" (NdefRecord definition). This tnf is defined for a RTD of either text, URI or SmartPoster. Binary values should not go there.

If you want to store binary values, consider using a record with the tnf "TNF_UNKNOWN". There you have the freedom to store the data in any format you want.

However, in this case the application which reads the tag, must interpret the data accordingly. If it is your app you can easily do that. If the reading app is something else, you may have to conform to the definitions that app understands.

corvairjo
  • 843
  • 1
  • 14
  • 25
  • The app will also read the tag, with the same format. I didn't read yet the tag, because I should write the command to read, then the tag will respond with the data I request. The format is the same (only hex values). Now, with the "TNF_UNKNOWN" am I able to send the message without header (e.g. "en"), just the string which I want? Thanks, Mihai – Mihai Coman Apr 01 '16 at 10:36
  • Yes, with "TNF_UNKNOWN" the formatting is up to you. But there is always a header, which is required to thell the system what kind of data is in a record. Depending on the sort of tnf found, the system acts with that data as appropriate for that sort of tnf. – corvairjo Apr 01 '16 at 16:40
  • Maybe I'm going offtopping, but I tried many solutions, but I still can not send what should I send :). As far as I understood, I should have a byte array into the message. The problem is when I format my string into byte array, it formats automatically as ASCII, or UTF-8 or anything else. Is it any chance to leave my string as is? For example, I have a string like "0D020100" and I want to send that string, not "3044303230313030" (the ASCII code for each element from string). Please, if you have any hint how to do this, be kind and tell me. Regards, Mihai – Mihai Coman Apr 06 '16 at 16:51
  • Ignore it :) The answer is here: http://stackoverflow.com/questions/140131/convert-a-string-representation-of-a-hex-dump-to-a-byte-array-using-java – Mihai Coman Apr 06 '16 at 18:22