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.