×

Discussion Board

Page 1 of 2 12 LastLast
Results 1 to 15 of 28
  1. #1
    Nokia Developer Champion
    Join Date
    Sep 2007
    Posts
    915

    is this code correct ?

    Hello,

    I have written the following code. But when i touch external card to my Nokia 6212C there is no invokation of targetDetected(). Can some one check my code and tel me what is wrong in it ?


    /*
    * ReadCard.java
    *
    * Created on Jun 19, 2009, 9:39:46 AM
    *
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    */

    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.midlet.*;
    import javax.microedition.lcdui.*;

    /**
    * @author Administrator
    */
    public class ReadCard extends MIDlet implements TargetListener , NDEFRecordListener
    {
    private DiscoveryManager dm;
    Form form;

    public void startApp()
    {
    dm = DiscoveryManager.getInstance();
    form=new Form("");
    try {

    dm.addTargetListener(this, TargetType.NDEF_TAG);
    /*
    dm.addNDEFRecordListener(this, new NDEFRecordType(
    NDEFRecordType.UNKNOWN, "text"));
    //dm.addNDEFRecordListener(this, new NDEFRecordType(NDEFRecordType.UNKNOWN, ""));
    */
    Display.getDisplay(this).setCurrent(form);
    form.append("after display");

    } catch (IllegalStateException e) {
    form.append(e.toString());
    } catch (ContactlessException e) {
    form.append(e.toString());
    //Writing_image_to_NDEF_tag
    }
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
    }

    public void targetDetected(TargetProperties[] prop) {
    try
    {
    TargetProperties target = prop[0];
    NDEFTagConnection conn;
    // throw new UnsupportedOperationException("Not supported yet.");
    form.append("target detected");
    String url = target.getUrl();
    // Open NDEFTagConnection to the target
    conn = (NDEFTagConnection)Connector.open(url);
    // Read data from the target
    NDEFMessage message = conn.readNDEF();
    NDEFRecord[] records = message.getRecords();
    for(int i=0;i<records.length;i++)
    {
    byte data[]=records[i].getPayload();
    String sdata=new String(data);
    form.append(("record number 1="+sdata));
    }
    }
    catch(Exception e)
    {
    form.append(e.toString());
    }
    }

    public void recordDetected(NDEFMessage mes) {
    //throw new UnsupportedOperationException("Not supported yet.");
    //NDEFRecord rec[] = mes.getRecord(new NDEFRecordType(NDEFRecordType.UNKNOWN, "text"));
    NDEFRecord rec[] = mes.getRecord(new NDEFRecordType(NDEFRecordType.UNKNOWN,"text"));
    byte data[]= rec[0].getPayload();
    String sdata=new String(data);
    if(form==null)
    form=new Form("");
    form.append(sdata);

    }
    }

  2. #2
    Registered User
    Join Date
    Sep 2008
    Posts
    45

    Re: is this code correct ?

    I tried your code on my 6212, it detects my ndef tag.

    BTW, dont expect it to automatically launch the midlet on bringing the tag near

    the phone. You will first have to start the midlet then take the tag near the phone.

  3. #3
    Nokia Developer Champion
    Join Date
    Sep 2007
    Posts
    915

    Re: is this code correct ?

    Quote Originally Posted by praghuna View Post
    I tried your code on my 6212, it detects my ndef tag.

    BTW, dont expect it to automatically launch the midlet on bringing the tag near

    the phone. You will first have to start the midlet then take the tag near the phone.
    It seems strange to me. Can you tell me how can i write data in tag ? and how can i do sector vise operation in J2ME ?

  4. #4
    Registered User
    Join Date
    Sep 2008
    Posts
    45

    Re: is this code correct ?

    You need to be familiar with the mifare tag storage . Also you need to know the keys of the sectors to access them.

    The following midlet code is a sample for writing into block-0 of sector-1.

    /*
    * 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;
    }
    }

  5. #5
    Nokia Developer Champion
    Join Date
    Sep 2007
    Posts
    915

    Re: is this code correct ?

    Thanks for your reply and code. But my application requirement is that i need to read the data which is being written by other c#.net application. So can you give me any example or guide me to read that data. I wrote the code and i was able to read data which was shared with mobile but it does not seems working to read the data which is written by other application on card. So please let me know or give me any other example.

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    182

    Re: is this code correct ?

    But my application requirement is that i need to read the data which is being written by other c#.net application.
    Most probably the C# application used to create that tags (mifare1k with data) will be using any other keys than default ones (6 FF's). So, you should know that new key values prior to read the data from tag.

    So can you give me any example or guide me to read that data.
    You got a working code and now also asking for others to complete your task ?

  7. #7
    Nokia Developer Champion
    Join Date
    Sep 2007
    Posts
    915

    Re: is this code correct ?

    Quote Originally Posted by esunilkumare View Post
    Most probably the C# application used to create that tags (mifare1k with data) will be using any other keys than default ones (6 FF's). So, you should know that new key values prior to read the data from tag.



    You got a working code and now also asking for others to complete your task ?
    I did not mean it i tried that code but it is giving me target not found exception so i asked. I do not know what could be wrong in it.

  8. #8
    Registered User
    Join Date
    Oct 2006
    Posts
    182

    Re: is this code correct ?

    do you have access to the C# code which is used to create the tag that you are trying to read ? going thru the code will be helpful to find the logic and keys. in-order to read data from a third-party card, it is necessary to know the keys used write that data. without knowing keys, it will be very difficult to get the data.

  9. #9
    Nokia Developer Champion
    Join Date
    Sep 2007
    Posts
    915

    Re: is this code correct ?

    Quote Originally Posted by esunilkumare View Post
    do you have access to the C# code which is used to create the tag that you are trying to read ? going thru the code will be helpful to find the logic and keys. in-order to read data from a third-party card, it is necessary to know the keys used write that data. without knowing keys, it will be very difficult to get the data.
    My team mate has made that code so that is not issue and he told me he has used keys FFFFFF but when i tried to use the below code it is giving me excetion in targetDetected() that Connection error : target not found so can you tell me what could be wrong in it. The following code i have used.


    /*
    * 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);
    String sdata=new String(data);
    form.append("data="+sdata);

    // 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;
    frm.append(ex.toString());
    }
    return buf;
    }
    }

  10. #10
    Registered User
    Join Date
    Oct 2006
    Posts
    182

    Re: is this code correct ?

    your card is being detected by the phone since you are getting an exception in the targetDetected method. so problem will be in the connection url.

    are you sure that you are using a mifare 4k external tag (you are using a connection string like this "nfc:rf;type=mf4k;uid=" ). if you are using a mifare 1k tag, use this connection string "nfc:ndef;type=mf1k;uid=". or please try to use the connection string you got from

    Code:
    TargetProperties target = prop[0];
    String url = target.getUrl();
    NDEFTagConnection conn = (NDEFTagConnection)Connector.open(url);

    or try to use the following code to find out the type of tag you have:

    Code:
    /* Copyright (c) Nokia, 2007 */
    
    
    package com.nokia.nfc.app;
    
    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.TransactionListener;
    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.StringItem;
    import javax.microedition.midlet.MIDlet;
    import javax.microedition.midlet.MIDletStateChangeException;
    
    /**
     * Simple MIDlet, which detects NFC targets and prints their connection
     * data on the screen. Data detail level can be chosen by the user.
     *
     *
     */
    public class TargetDetectorMidlet extends MIDlet implements CommandListener,
        TargetListener, TransactionListener {
        DiscoveryManager dm;
        Form screen;
        Display disp;
        Command cmdClear;
        Command cmdExit;
        Command cmdBasic;
        Command cmdDetailed;
        StringItem siScreenText;
        boolean basic = true;
    
        public TargetDetectorMidlet() {
            dm = DiscoveryManager.getInstance();
    
            try {
                dm.addTargetListener(this, TargetType.ISO14443_CARD);
                dm.addTargetListener(this, TargetType.RFID_TAG);
                dm.addTargetListener(this, TargetType.NDEF_TAG);
                dm.addTransactionListener(this);
            } catch (IllegalStateException e) {
                e.printStackTrace();
            } catch (ContactlessException e) {
                e.printStackTrace();
            }
        }
    
        protected void startApp() throws MIDletStateChangeException {
            screen = new Form("TargetDetector");
            siScreenText = new StringItem(null,
                    "Application started. Please touch target...");
            cmdClear = new Command("Clear", Command.OK, 0);
            cmdExit = new Command("Exit", Command.EXIT, 0);
            cmdBasic = new Command("Switch to Basic", Command.OK, 1);
            cmdDetailed = new Command("Switch to Detailed", Command.OK, 1);
    
            screen.append(siScreenText);
            screen.addCommand(cmdClear);
            screen.addCommand(cmdExit);
            screen.addCommand(cmdDetailed);
            screen.setCommandListener(this);
            disp = Display.getDisplay(this);
            disp.setCurrent(screen);
        }
    
        public void commandAction(Command c, Displayable d) {
            if (c == cmdExit) {
                destroyApp(true);
            }
    
            if (c == cmdClear) {
                clearScreen();
                siScreenText.setText("Cleared");
            }
    
            if (c == cmdBasic) {
                basic = true;
                clearScreen();
                screen.removeCommand(cmdBasic);
                screen.addCommand(cmdDetailed);
                siScreenText.setText("Basic data mode selected");
            }
    
            if (c == cmdDetailed) {
                basic = false;
                clearScreen();
                screen.removeCommand(cmdDetailed);
                screen.addCommand(cmdBasic);
                siScreenText.setText("Detailed data mode selected");
            }
        }
    
        private void clearScreen() {
            screen.deleteAll();
            siScreenText.setText("");
            screen.append(siScreenText);
        }
    
        public void targetDetected(TargetProperties[] prop) {
            clearScreen();
            siScreenText.setText("Target detected, target contains " + prop.length +
                " contactless targets");
    
            AnalyzeTargetThread at = new AnalyzeTargetThread(prop);
            new Thread(at).start();
        }
    
        public void externalReaderDetected(byte arg0) {
            clearScreen();
            siScreenText.setText("External reader detected");
        }
    
        protected void pauseApp() {
        }
    
        protected void destroyApp(boolean arg0) {
            notifyDestroyed();
        }
    
        protected class AnalyzeTargetThread extends Thread {
            TargetProperties[] prop;
    
            public AnalyzeTargetThread(TargetProperties[] prop) {
                this.prop = prop;
            }
    
            public void run() {
                for (int i = 0; i < prop.length; i++) {
    	            	if (!basic){
    	                screen.append("\n\nTARGET " + (i + 1));
    	                screen.append("\n" + (i + 1) + ".1 UID: " + prop[i].getUid());
    	                screen.append("\n" + (i + 1) + ".2 Connection Variables:");
    
    	                Class[] classes = prop[i].getConnectionNames();
    
    	                for (int j = 0; j < classes.length; j++) {
    	                    screen.append("\n" + (i + 1) + ".2." + (j + 1) +
    	                        " Name =\n" + classes[j].getName());
    	                    screen.append("\n" + (i + 1) + ".2." + (j + 1) +
    	                        " URL =\n" + prop[i].getUrl(classes[j]));
    	                }
    
    	                screen.append("\n" + (i + 1) + ".3 TargetTypes:");
    
    	                TargetType[] types = prop[i].getTargetTypes();
    
    	                for (int k = 0; k < types.length; k++) {
    	                    screen.append("\n- " + (i + 1) + ".3." + (k + 1) +
    	                        " Type = " + types[k].toString());
    	                }
                	}
                    else{
                        screen.append("\n\nTARGET " + (i + 1));
                        screen.append("\n" + (i + 1) + ".1 UID: " + prop[i].getUid());
    
                        Class[] classes = prop[i].getConnectionNames();
                        String url = prop[i].getUrl(classes[0]);
                        screen.append("\n" + (i + 1) + ".2 Type: " +
                            url.substring(url.indexOf("=") + 1, url.lastIndexOf(';')));
                    }
                }
            }
        }
    }
    Regards,
    Sunil

  11. #11
    Nokia Developer Champion
    Join Date
    Sep 2007
    Posts
    915

    Re: is this code correct ?

    Dear Sunil

    Thanks for your continuous help. I changed the code and did suggested by you but it is giving ClassCastException. What could be wrong in it ? The code is as below.


    /*
    * 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();
    String extUrl="nfc:ndef;type=mf1k;uid="+prop[0].getUid();
    */

    //this.form.append(extUrl);


    TargetProperties target = prop[0];
    String url = target.getUrl();
    form.append(url);
    //NDEFTagConnection conn = (NDEFTagConnection)Connector.open(url);

    byte[] data = this.readData(url, this.form);
    String sdata=new String(data);
    form.append("data="+sdata);

    // 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(1);

    frm.append("Got block 0");
    mblk.read(keyA, buf, 0, 0, 16);
    //mblk.write(keyA, buf, 0);

    tconn.close();

    } catch (Exception ex) {
    buf = null;
    frm.append(ex.toString());
    }
    return buf;
    }
    }

  12. #12
    Registered User
    Join Date
    Oct 2006
    Posts
    182

    Re: is this code correct ?

    Have you tried the TargetDetector example ?

    which is your tag-type ? mifare 1k or 4k ?

    try "nfc:rf;type=mf1k;uid=" instead of "nfc:ndef;type=mf1k;uid="

    regards
    Sunil

  13. #13
    Nokia Developer Champion
    Join Date
    Sep 2007
    Posts
    915

    Re: is this code correct ?

    Quote Originally Posted by esunilkumare View Post
    Have you tried the TargetDetector example ?

    which is your tag-type ? mifare 1k or 4k ?

    try "nfc:rf;type=mf1k;uid=" instead of "nfc:ndef;type=mf1k;uid="

    regards
    Sunil
    Ya i tried TargetDetector it gave me following result. Type :1.2 type :mf1k

  14. #14
    Nokia Developer Champion
    Join Date
    Sep 2007
    Posts
    915

    Re: is this code correct ?

    Quote Originally Posted by esunilkumare View Post
    Have you tried the TargetDetector example ?

    which is your tag-type ? mifare 1k or 4k ?

    try "nfc:rf;type=mf1k;uid=" instead of "nfc:ndef;type=mf1k;uid="

    regards
    Sunil
    I am so thankful to you for your continuous support. After puting "nfc:rf;type=mf1k;uid=" it is not giving any exception but yet nothing comes in String parameter which should contains data. It did not display any thing so it seems it should be blank.

    Please take a look at code if there is some thing wrong in reading particualr sector or conversition to String . This should be the last issue.

  15. #15
    Registered User
    Join Date
    Oct 2006
    Posts
    182

    Re: is this code correct ?

    Hi Kirti,


    Your code worked for me with some minor changes. Changes are :

    1. Change the TargetType from NDEF_TAG to RFID_TAG in addTargetListener method. (while adding listeners to DiscoveryManager)
    2. Change the way you getting connection URL to String url = "nfc:rf;type=mf1k;uid=" + prop[0].getUid(); (in targetDetected method)

    Thats all. It will work now (and of-course let me know the result)

    Regards,
    Sunil

Similar Threads

  1. Replies: 106
    Last Post: 2009-05-11, 05:53
  2. C++ 學習程序2
    By hanwen813 in forum [Archived] Other Programming Discussion 关于其他编程技术的讨论
    Replies: 32
    Last Post: 2009-04-02, 09:10
  3. Replies: 7
    Last Post: 2008-02-26, 15:43
  4. Testing HTTP client for Nokia 3650; Looking to share code
    By nawkboy in forum Symbian Networking & Messaging (Closed)
    Replies: 2
    Last Post: 2003-09-10, 15:30

Posting Permissions

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