×
Namespaces

Variants
Actions
Revision as of 08:07, 25 March 2013 by hamishwillee (Talk | contribs)

Reading UID from embedded Mifare tag

From Nokia Developer Wiki
Jump to: navigation, search
Article Metadata
Compatibility
Platform(s):
Series 40
Article
Created: tsavinen (03 Jan 2008)
Last edited: hamishwillee (25 Mar 2013)
import java.io.IOException;
 
import javax.microedition.io.Connector;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Form;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
 
import com.nokia.nfc.nxp.mfstd.MFStandardConnection;
 
 
public class GetMyUID extends MIDlet {
Form form = new Form("GetMyUID");
 
public GetMyUID() {
}
 
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
}
 
protected void pauseApp() {
}
 
protected void startApp() throws MIDletStateChangeException {
Display.getDisplay(this).setCurrent(form);
 
MFStandardConnection myTag = null;
try {
myTag = (MFStandardConnection) Connector.open(System.getProperty("internal.mf.url"));
form.append("My UID: " + decodeUID(myTag.getManufacturerBlock().getUID()));
} catch (Exception e) {
form.append(e.toString());
} finally {
if (myTag != null) {
try {
myTag.close();
} catch (IOException e) {
form.append(e.toString());
}
}
}
}
 
private String decodeUID(byte[] uid) {
byte[] result = new byte[uid.length*2];
for (int i = 0; i < uid.length; i++) {
result[i*2] = (hexChars[(uid[i] & 0xF0) >>> 4]);
result[i*2+1] = (hexChars[uid[i] & 0x0F]);
}
 
return new String(result);
}
 
private final static byte[] hexChars = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
}
107 page views in the last 30 days.
×