×

Discussion Board

Results 1 to 2 of 2
  1. #1
    Registered User
    Join Date
    Aug 2010
    Posts
    15

    Reading plain text from RFID tag

    Hello!
    I created Mifare1k tag with nfc manager. i wrote some plain text on it. How should i read it?
    I tried the code below(modified one other example), bui it crashes my emulator as i run it (nokia 6212 classic). Do you have any piece of code that works?


    public class Kartar extends MIDlet implements TargetListener,
    NDEFRecordListener, CommandListener {

    private DiscoveryManager dm;

    private Form form;

    /**
    * Read command to set application to reading mode
    */
    private Command readCommand;

    /**
    * Write command to set application to writing mode
    */
    private Command writeCommand;

    /**
    * Exit command
    */
    private Command exitCommand;

    /**
    * Boolean telling if application is in read mode,
    */
    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, "text/plain"));
    } catch (IllegalStateException e) {
    form.append(e.toString());
    } catch (ContactlessException e) {
    form.append(e.toString());
    }

    // Initialize UI
    form = new Form("Reading Mode");
    readCommand = new Command("Read", Command.OK, 1);
    exitCommand = new Command("Exit", Command.EXIT, 1);
    form.addCommand(writeCommand);
    form.addCommand(exitCommand);
    form.setCommandListener(this);
    Display.getDisplay(this).setCurrent(form);
    }

    protected void pauseApp() {
    }

    protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
    }

    /**
    * Notified when 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());

    ByteArrayOutputStream baos = getImage("/com/nokia/test/simple/ndef/res/Smiley.png");

    // 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 written");

    } 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, "text/plain"));

    byte znaki []=rec[0].getPayload();
    String s=new String(znaki);
    displayAlert("Prebral sem: " + s, AlertType.INFO);

    }
    }

    public void commandAction(Command c, Displayable d) {
    if(c == exitCommand){
    notifyDestroyed();
    }
    }

    private void displayAlert(String error, AlertType type) {
    Alert err = new Alert(form.getTitle(), error, null, type);
    Display.getDisplay(this).setCurrent(err, form);
    }

    /**
    * Read image from phone to ByteArrayOutputStream
    */
    private ByteArrayOutputStream getImage(String s) throws IOException {
    InputStream is = getClass().getResourceAsStream(s);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    int c;
    while ((c = is.read()) != -1) {
    baos.write(c);
    }
    return baos;
    }
    }

    Tnx

  2. #2
    Registered User
    Join Date
    Feb 2009
    Location
    Hagenberg, Austria
    Posts
    121

    Re: Reading plain text from RFID tag

    Hallo,

    Quote Originally Posted by DixieFlatline View Post
    I created Mifare1k tag with nfc manager. i wrote some plain text on it. How should i read it?
    That depends on what type of tag-content you created:
    * If you created an NDEF tag (based on MIFARE 1K) with an NDEF record that has the MIME media type text/plain or
    * if you created an NDEF tag (based on MIFARE 1K) with an NDEF Text record:
    You can either use an NDEFRecordListener to listen for the expected type of record or you can use a TargetListener to listen for any NDEF tag and then parse its NDEF message for the expected record.
    * If you created a MIFARE 1K tag and then directly edited its contents in the hex editor window without using the NDEF format:
    In this case you need to access the MIFARE tag through Nokia's MIFARE API (see package com.nokia.nfc.nxp.mfstd).

    Quote Originally Posted by DixieFlatline View Post
    I tried the code below(modified one other example), bui it crashes my emulator as i run it (nokia 6212 classic). Do you have any piece of code that works?
    Your code looks OK, except that you don't need large portions of it. One thing I found is the line
    Code:
    form.addCommand(writeCommand);
    In this line you use the uninitialized variable writeCommand. In my opinion this should lead to a NULL pointer exception, but it should not crash the emulator.

    From your code I would guess that you have created an NDEF tag with an NDEF record of the MIME media type text/plain. So the easiest way to access (read-only) your NDEF record would be an NDEFRecordListener like this:

    Code:
      public void startApp() {
        dm = DiscoveryManager.getInstance();
        
        try {
          dm.addNDEFRecordListener(this, new NDEFRecordType(NDEFRecordType.MIME, "text/plain"));
        } catch (Exception e) { }
        
        form = new Form("Reading Mode");
        exitCommand = new Command("Exit", Command.EXIT, 1);
        form.addCommand(exitCommand);
        form.setCommandListener(this);
        Display.getDisplay(this).setCurrent(form);
      }
    
      public void recordDetected(NDEFMessage msg) {
        if (msg != null) {
          for (int i = 0; i < msg.getNumberOfRecords(); ++i) {
            NDEFRecord rec = msg.getRecord(i);
            
            String recT = rec.getRecordType().getName();
            String recP = "";
            if (rec.getPayloadLength() > 0) {
              byte[] payload = rec.getPayload();
              
              try {
                for (int ll = 0; ll < payload.length; ++ll) {
                  if (((payload[ll] >=  0) && (payload[ll] <=  8)) ||
                      ((payload[ll] >= 14) && (payload[ll] <= 31))) {
                    payload[ll] = 46;
                  }
                }
                
                recP = new String(payload, "ISO8859-1");
              } catch (Exception e) { }
            }
    
            form.append("Record " + i + ":\n"
                + (recT != null ? "TYPE=" + recT + "\n" : "")
                + "--- BEGIN DATA ---\n" + recP + "\n--- END DATA ---\n\n");
          }
        }
      }
    Of course, you should also remove any unused NDEFRecordListener as soon as you no longer need it.


    br,
    Michael

Similar Threads

  1. Reading Phone RFID Tag UID
    By giaviv in forum Mobile Java General
    Replies: 1
    Last Post: 2010-06-10, 11:58
  2. Writing to RFID tag using 6212 SDK
    By rinyogi03 in forum Near Field Communication
    Replies: 3
    Last Post: 2009-07-20, 11:07
  3. Reading text from RFID mifair tag
    By honest_success in forum Near Field Communication
    Replies: 5
    Last Post: 2009-06-19, 14:36
  4. write data to RFID tag
    By honest_success in forum Near Field Communication
    Replies: 4
    Last Post: 2009-06-18, 10:51
  5. NDEF tag filtering for RFID reading midlets
    By hajons in forum Near Field Communication
    Replies: 1
    Last Post: 2008-10-21, 12:34

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •