/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Home
*/
import com.nokia.nfc.nxp.mfstd.*;
import javax.microedition.contactless.*;
import javax.microedition.io.*;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
// Simple MIDlet to read the serial number of NDEF tag
public class ReadBlock extends MIDlet implements TargetListener {
private Form form;
private TextField textField;
protected void startApp() throws MIDletStateChangeException {
// Get instance of NFC Discovery Manager
DiscoveryManager dm = DiscoveryManager.getInstance();
// Register NDEF_TAG target to discovery
try {
//dm.addTargetListener(this, TargetType.NDEF_TAG);
dm.addTargetListener(this, TargetType.NDEF_TAG);
} catch (IllegalStateException e) {
// Catch IllegalStateException
} catch (ContactlessException e) {
// Catch ContactlessException
}
// Initialize and show user interface elements
form = new Form("ReadSerial");
Display.getDisplay(this).setCurrent(form);
}
protected void pauseApp() {
}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
}
public void targetDetected(TargetProperties[] prop) {
TargetType[] t = prop[0].getTargetTypes();
String extUrl = "nfc:rf;type=mf4k;uid=" + prop[0].getUid();
this.form.append(extUrl);
byte[] data = this.readData(extUrl, this.form);
// process data here
}
byte[] readData(String url, Form frm)
{
byte[] buf = new byte[16];
byte[] bKeyA = {(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,
(byte)0xff};
MFKey.KeyA keyA = new MFKey.KeyA(bKeyA);
MFStandardConnection tconn = null;
// Make connection
try {
tconn = (MFStandardConnection)Connector.open(url);
// read meta_data sector
MFSector sec = tconn.getSector(1);
frm.append("Sector 1 Blocks:"+ sec.getBlockCount());
MFBlock mblk = sec.getBlock(0);
frm.append("Got block 0");
mblk.read(keyA, buf, 0, 0, 16);
mblk.write(keyA, buf, 0);
tconn.close();
} catch (Exception ex) {
buf = null;
}
return buf;
}
}