I'm developing an Android application with minSdk=14 and targetSdk=17. I want to the application to use Android Beam to send a plain text string from the phone to a NFC reader. I haven't got the reader yet and have been trying to Beam plain text data from one Android device to another (I have a Nexus 4 running 4.2.2 and a LG Optimus L5 running 4.0.3).
I have found two ways to send text data, the first is to use TNF_WELL_KNOWN and RTD_TEXT as the type and the other is to use TNF_MIME_MEDIA and text/plain as the type.
After sending from the L5 to the N4, all I see is under New tag collected is "text/plain". I also tried reading the Beam data with NXP TagWriter, this is what I got when using RTD_TEXT https://i.sstatic.net/TxMzS.png and this is when using the text/plain MIME https://i.sstatic.net/vDc4m.png.
This is the code when using RTD_TEXT
NdefRecord[] records = {
new NdefRecord(NdefRecord.TNF_WELL_KNOWN,
NdefRecord.RTD_TEXT,
new byte[0],
"testing".getBytes())
};
NdefMessage msg = new NdefMessage(records);
adapter.setNdefPushMessage(msg, this);
This is the code when using text/plain MIME
NdefRecord[] records = {
new NdefRecord(NdefRecord.TNF_MIME_MEDIA,
"text/plain".getBytes(),
new byte[0],
"testing".getBytes())
};
NdefMessage msg = new NdefMessage(records);
adapter.setNdefPushMessage(msg, this);
How do I correctly create the NdefRecord? Or is it working as intended only that the payload is not displayed on the other phone? Which one of these two methods (RTD_TEXT and MIME) are preferred when sending plain text data?
RTD_TEXT
records are mapped to MIME-type "text/plain" for intent filtering. That is: an intent filter for "text/plain" will match both type of records. So for that it does not matter which type you use.
The payload of an RTD_TEXT
record is slightly more complicated than a MIME-type text/plain one. It should contain information on the encoding used and the language of the content, see the specification available at http://www.nfc-forum.org/specs/spec_list/. (So the RTD_TEXT
record you have created is incorrect.)