Hoe to write an image to a NDEF tag

From Nokia Developer Wiki
Jump to: navigation, search
Article Metadata
Series 40
Created: samuuh (27 Jun 2007)
Last edited: lpvalente (25 Jan 2014)

Simple example MIDlet that can write an image to a RFID tag and read the image back from it. Writing is done using TargetListener and NDEFTagConnection. Reading using NDEFRecordListener. Image used in this example: Media:smiley.png

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.microedition.contactless.ContactlessException;
import javax.microedition.contactless.DiscoveryManager;
import javax.microedition.contactless.TargetListener;
import javax.microedition.contactless.TargetProperties;
import javax.microedition.contactless.TargetType;
import javax.microedition.contactless.ndef.NDEFMessage;
import javax.microedition.contactless.ndef.NDEFRecord;
import javax.microedition.contactless.ndef.NDEFRecordListener;
import javax.microedition.contactless.ndef.NDEFRecordType;
import javax.microedition.contactless.ndef.NDEFTagConnection;
import javax.microedition.io.Connector;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Image;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
* Simple example that can write image to RFID tag and read it from it.
* Writing is done using TargetListener and NDEFTagConnection.
* Reading using NDEFRecordListener.

public class ImageWrite extends MIDlet implements TargetListener,
NDEFRecordListener, CommandListener {
private DiscoveryManager dm;
private Form form;
// Read and write commands to set mode of the application
private Command readCommand;
private Command writeCommand;
// Boolean telling in which mode the application is
private boolean read = true;
protected void startApp() throws MIDletStateChangeException {
dm = DiscoveryManager.getInstance();
try {
// Register NDEF tag to target listener..
dm.addTargetListener(this, TargetType.NDEF_TAG);
// ..and png image mime type to NDEF record listener.
dm.addNDEFRecordListener(this, new NDEFRecordType(
NDEFRecordType.MIME, "image/png"));
} catch (IllegalStateException e) {
} catch (ContactlessException e) {
form = new Form("Form");
readCommand = new Command("Read", Command.OK, 1);
writeCommand = new Command("Write", Command.OK, 1);
protected void pauseApp() {
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
// NDEF tag detected
public void targetDetected(TargetProperties[] properties) {
// If application is not in read mode - write image to tag
if (!read) {
NDEFTagConnection conn = null;
try {
// Open connection to NDEF tag
conn = (NDEFTagConnection) Connector.open(properties[0]
// Read image from phone to ByteArrayOutputStream
InputStream is = getClass().getResourceAsStream("smiley.png");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int c;
while ((c = is.read()) != -1) {
// Create NDEF Record to be added to NDEF Message
NDEFRecord recordToWrite = new NDEFRecord(new NDEFRecordType(
NDEFRecordType.MIME, "image/png"), null, baos
// Create NDEF Message and add created record in it.
NDEFMessage write_container = new NDEFMessage();
// Write NDEF Message to tag
form.append("Image witten");
} catch (IOException e) {
} catch (Exception e) {
} finally {
try {
if (conn != null) {
} catch (IOException e) {
// proper NDEF message detected
public void recordDetected(NDEFMessage mes) {
// If application is in read mode - show image from tag
if (read) {
// Get png image record from NDEF message
NDEFRecord rec[] = mes.getRecord(new NDEFRecordType(
NDEFRecordType.MIME, "image/png"));
// Create Image from payload data from record - which is the image
// as byte array.
Image img = Image.createImage(rec[0].getPayload(), 0, (int) rec[0]
// Change application mode from reading to writing and vice versa
public void commandAction(Command c, Displayable d) {
if (read) {
read = false;
} else {
read = true;
This page was last modified on 25 January 2014, at 18:22.
74 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.


Thank you!

We appreciate your feedback.