Question

With the latest NFC tags, it is possible to store up to 8k of data. So I would like to know how to store a picture on a tag, like the NXP TagWriter app.

I found no information about it. Can anyone explain how to do that?

Was it helpful?

Solution

You can use MIME type records to store images on NFC tags. If, for instance, your image is a JPEG image, you would use the MIME type "image/jpeg". You NDEF record could then look like this:

+----------------------------------------+
+ MB=1, ME=1, CF=0, SR=0, IL=0, TNF=MIME +
+----------------------------------------+
+ Type Length = 10                       +
+----------------------------------------+
+ Payload Length = N                     +
+----------------------------------------+
+ image/jpeg                             +
+----------------------------------------+
+ <Your image data (N bytes)>            +
+----------------------------------------+

On Android, you could create such a record using

byte[] myImage = ...;
NdefRecord myImageRecord = NdefRecord.createMime("image/jpeg", myImage);

Or using the constructor of NdefRecord:

byte[] myImage = ...;
NdefRecord myImageRecord = new NdefRecord(
        NdefRecord.TNF_MIME_MEDIA,
        "image/jpeg".getBytes("US-ASCII"),
        null,
        myImage
);

Once you have a Tag handle of an NDEF tag (i.e. through receiving and NFC discovery intent), you could then write the NDEF record to the tag:

NdefMessage ndefMsg = new NdefMessage(new NdefRecord[] { myImageRecord });

Tag tag = ...;
Ndef ndefTag = Ndef.get(tag);
if (ndefTag != null) {
    ndefTag.connect();
    ndefTag.writeNdefMessage(ndefMsg);
    ndefTag.close();
} else {
    NdefFormatable ndefFormatable = NdefFormatable.get(tag);
    if (ndefFormatable != null) {
        ndefFormatable.connect();
        ndefFormatable.format(ndefMsg);
        ndefFormatable.close();
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top