×
Namespaces

Variants
Actions
Revision as of 13:59, 18 July 2012 by hamishwillee (Talk | contribs)

Hoe to write an image to a NDEF tag

From Nokia Developer Wiki
Jump to: navigation, search
Article Metadata
Compatibility
Platform(s):
Series 40
Article
Created: samuuh (27 Jun 2007)
Last edited: hamishwillee (18 Jul 2012)

Simple example MIDlet that can write image to RFID tag and read it from it. Writing is done using TargetListener and NDEFTagConnection. Reading using NDEFRecordListener. Image ised 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) {
form.append(e.toString());
} catch (ContactlessException e) {
form.append(e.toString());
}
form = new Form("Form");
readCommand = new Command("Read", Command.OK, 1);
writeCommand = new Command("Write", Command.OK, 1);
form.addCommand(writeCommand);
form.setCommandListener(this);
Display.getDisplay(this).setCurrent(form);
}
 
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]
.getUrl());
 
// Read image from phone to ByteArrayOutputStream
InputStream is = getClass().getResourceAsStream("smiley.png");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int c;
while ((c = is.read()) != -1) {
baos.write(c);
}
 
// Create NDEF Record to be added to NDEF Message
NDEFRecord recordToWrite = new NDEFRecord(new NDEFRecordType(
NDEFRecordType.MIME, "image/png"), null, baos
.toByteArray());
 
// Create NDEF Message and add created record in it.
NDEFMessage write_container = new NDEFMessage();
write_container.appendRecord(recordToWrite);
 
// Write NDEF Message to tag
conn.writeNDEF(write_container);
 
form.append("Image witten");
 
} catch (IOException e) {
form.append(e.toString());
} catch (Exception e) {
form.append(e.toString());
} finally {
try {
if (conn != null) {
conn.close();
}
} catch (IOException e) {
form.append(e.toString());
}
}
}
}
 
// 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]
.getPayloadLength());
form.append(img);
}
}
 
// Change application mode from reading to writing and vice versa
public void commandAction(Command c, Displayable d) {
if (read) {
read = false;
form.removeCommand(writeCommand);
form.addCommand(readCommand);
} else {
read = true;
form.removeCommand(readCommand);
form.addCommand(writeCommand);
}
}
}
82 page views in the last 30 days.