×

Discussion Board

Results 1 to 10 of 10
  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    21

    Writing data to a Mifare 1K tag fails

    Dear valuable members of the Nokia NFC Forum:

    I have a midlet that is able to both read and write NDEF formatted data to a Mifare 1K tag. I am also able to read data dircetly from the tags memory using

    Code:
    dataBlock = sectors[j].getBlock(k);
    dataBlockSize = dataBlock.size();
    res = new byte[dataBlockSize];
    dataSize = dataBlock.read(autKey, res, 0, 0, dataBlockSize);
    autKey is the standard 0xff-ff-ff-ff-ff-ff key.

    However, when I try to write data directly to the tags memory, I get an MFStandardException error. The SDK doc say "MFStandardException - if authentication fails.", so I guess that the problem is the authentication.. but I dont really know this for sure... I use the following code to write to the tag:

    Code:
    url = detectedTarget[i].getUrl(Class.forName(
                                     "com.nokia.nfc.nxp.mfstd.MFStandardConnection"));
                            std_conn = (MFStandardConnection) Connector.open(url);
    
                            byte[] key = new byte[] {
                                    (byte) 0xff, (byte) 0xff, (byte) 0xff,
                                    (byte) 0xff, (byte) 0xff, (byte) 0xff
                                };
                            autKey = new MFKey.KeyA(key);
                            
                            String data = "aaaaaaaaaaa";
                            std_conn.write(autKey, data.getBytes(), 0);

    Does anyone have an idea about why this code return the "MFStandardException" ?

    Thanks.

  2. #2
    Regular Contributor
    Join Date
    Jul 2007
    Location
    Lisbon - Portugal
    Posts
    96

    Re: Writing data to a Mifare 1K tag fails

    Try to write a byte[] with "dataBlockSize" bytes. Check if it works that way.

    Good luck

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    21

    Re: Writing data to a Mifare 1K tag fails

    I have tried to write a 16 bytes length byte array (as each data block is 16 bytes) as Ricardo proposed. Using the code below, returns no error at all, however when I read from the tag again there no data.... I am still investigating this problem..

    Regarding the write function, the Java doc say "Writes the given data to the data area starting at the byte offset dstOffset" (wich is 0 my case), I have no idea what this is...

    Code:
    byte[] key = new byte[] {
                                    (byte) 0xff, (byte) 0xff, (byte) 0xff,
                                    (byte) 0xff, (byte) 0xff, (byte) 0xff
                                };
                            autKey = new MFKey.KeyA(key);
                            
                            String dataValue = "Hello!";
                           byte[] dataBytes = new byte[16];
                            for (int j=0; i<dataBytes.length;i++) dataBytes[j] = (byte)0x00;
                            int dataLength = dataValue.getBytes().length;
                            if (dataLength < dataBytes.length) {
                            	System.arraycopy(dataValue.getBytes(), 0, dataBytes, 0, dataLength);
                            }
                            
                            std_conn.write(autKey, dataBytes, 0);
    Last edited by Dervis; 2007-12-13 at 13:14.

  4. #4
    Regular Contributor
    Join Date
    Jul 2007
    Location
    Lisbon - Portugal
    Posts
    96

    Re: Writing data to a Mifare 1K tag fails

    Are you testing on a real device or on the emulator?

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    21

    Re: Writing data to a Mifare 1K tag fails

    I am testing on both. The output from the phone is the same as the emulation.

    The wired thing is that, even writing 16 bytes to other than offset 0 gives a "MFStandardException". For instance this:

    std_conn.write(autKey, dataBytes, 4);

    4 will be the first data block in sector 2 (sector index 1).

    But, writing this way works:

    MFBlock block = std_conn.getBlock(12); // sector 3
    block.write(autKey, data.getBytes(), 0);

    Notice that "data" is not a 16 length array in this case..

  6. #6
    Nokia Developer Expert
    Join Date
    Dec 2003
    Posts
    38

    Re: Writing data to a Mifare 1K tag fails

    The problem is that you are tring to write to the manufacturer block
    at address 0, which is write protected. For more information see,

    MF1 IC S50 Functional Specification, Mifare Standard 1k Tag, Chapter 6.6.1 http://www.nxp.com/acrobat_download/...on/m001052.pdf

  7. #7
    Registered User
    Join Date
    Dec 2007
    Posts
    21

    Re: Writing data to a Mifare 1K tag fails

    Quote Originally Posted by tsavinen View Post
    The problem is that you are tring to write to the manufacturer block
    at address 0, which is write protected. For more information see,

    MF1 IC S50 Functional Specification, Mifare Standard 1k Tag, Chapter 6.6.1 http://www.nxp.com/acrobat_download/...on/m001052.pdf
    Then, why doesnt this line working:

    std_conn.write(autKey, dataBytes, 4);

    thanks...

  8. #8
    Nokia Developer Expert
    Join Date
    Dec 2003
    Posts
    38

    Re: Writing data to a Mifare 1K tag fails

    That's because each block is 16 bytes long. By the way, you really
    should check the memory organization of the 1K tag if you want to use
    read/write methods of the MFStandardConnection class. Alternatively use
    MFDataArea and it's subinterfaces for more structured approach.

  9. #9
    Registered User
    Join Date
    Dec 2007
    Posts
    21

    Re: Writing data to a Mifare 1K tag fails

    Alright, thank you very much.

    I have a few more questions I hope you can help me with:

    Can I use the standard key for all write operations?

    If I have a byte array that is longer than 16 bytes, do I have to manually write to several blocks and sectors or will it be done by the write function?

    If I use the MFSector class, do I need to write the sector trailers?

  10. #10
    Nokia Developer Expert
    Join Date
    Dec 2003
    Posts
    38

    Re: Writing data to a Mifare 1K tag fails

    If the tag is not protected then you can use default keys (use null as
    parameter). On the otherhand if the tag (or parts of it) are protected
    you need to know the key(s).

    You need to segment your data by your self. There is no method that would
    allow you to write for example 256 bytes to the tag directly. And no you
    don't need to write sector trailers - and probably you don't want to.

Similar Threads

  1. GPRS connection problems on 6600..
    By suckho in forum Symbian Networking & Messaging (Closed)
    Replies: 10
    Last Post: 2005-12-02, 14:00
  2. Nokia 6600 Socket Server send delay Problem
    By dicson_hu in forum Mobile Java Networking & Messaging & Security
    Replies: 1
    Last Post: 2005-10-31, 07:37
  3. Replies: 3
    Last Post: 2005-08-01, 13:39
  4. Writing data in stream
    By fcobsb in forum Symbian
    Replies: 0
    Last Post: 2004-01-15, 18:15

Posting Permissions

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