×

Discussion Board

Page 1 of 2 12 LastLast
Results 1 to 15 of 25
  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    16

    Question NFC read mifare 1k with 6216 classic

    Hello;
    I got that:
    public class MidletGestion extends MIDlet implements TargetListener, CommandListener {

    protected void startApp() throws MIDletStateChangeException {
    try {

    DiscoveryManager dm = DiscoveryManager.getInstance();

    dm.addTargetListener(this, TargetType.ISO14443_CARD);
    dm.addTargetListener(this, TargetType.NDEF_TAG);
    dm.addTargetListener(this, TargetType.RFID_TAG);
    dm.addTargetListener(this, TargetType.VISUAL_TAG);
    } catch (IllegalStateException e) {
    // Catch IllegalStateException
    } catch (ContactlessException e) {
    // Catch ContactlessException
    }
    }

    public void targetDetected(TargetProperties[] properties) {
    SimpleTagConnection conn = null;
    try {
    TargetProperties target = properties[0]; // always at least one target

    //open the connection

    conn = (SimpleTagConnection) Connector.open(target.getUrl(SimpleTagConnection.class));

    //read from 4 page, next 8 pages
    byte[] res = conn.read(4, 8);

    //with the bytes, make a string
    codigo = "";
    for (int i = 0; i < res.length; i++) {
    codigo = codigo + (char) res[i];
    }

    //we read 32 characters, but just need the first 9
    codigo = codigo.substring(0, 8);
    //show the text read
    textField.setString(codigo);

    } catch (Exception e) {
    textField.setString("Scan the tag again");
    } finally {
    closeQuietly(conn); //Importante: close the connection
    }
    }
    }
    Now i want read mifare 1k tags

    Thank you.


    Edit to dont make a longer question.
    I got too an application that i copy from examples on visual.net. Read with a ACR122 reader (usb to pc). if any want it i could paste here.

    Thanks
    Last edited by Jorge Gil; 2010-05-17 at 09:02. Reason: cut the question

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

    Re: NFC read mifare 1k with 6216 classic

    Hallo,

    Quote Originally Posted by Jorge Gil View Post
    Code:
    dm.addTargetListener(this, TargetType.ISO14443_CARD);
    dm.addTargetListener(this, TargetType.NDEF_TAG);
    dm.addTargetListener(this, TargetType.RFID_TAG);
    dm.addTargetListener(this, TargetType.VISUAL_TAG);
    I would suggest that you only register listeners for the target types that you really need (i.e. RFID_TAG for Mifare,..., ISO14443_CARD if you also want to listen for ISO 14443 Type 4 tags/smartcards)


    Quote Originally Posted by Jorge Gil View Post
    Code:
    conn = (SimpleTagConnection) Connector.open(target.getUrl(SimpleTagConnection.class));
    The SimpleTagConnection is for NFC Forum type 2 tags (e.g. Mifare Ultralight) Have a look at the MFStandardConnection (in package com.nokia.nfc.nxp.mfstd). This can be used to read Mifare Classic 1k/4k.

    br,
    Michael

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    16

    Re: NFC read mifare 1k with 6216 classic

    Quote Originally Posted by mroland View Post
    Hallo,


    The SimpleTagConnection is for NFC Forum type 2 tags (e.g. Mifare Ultralight) Have a look at the MFStandardConnection (in package com.nokia.nfc.nxp.mfstd). This can be used to read Mifare Classic 1k/4k.

    br,
    Michael
    Thanks, i will take your suggest and just take the listener needed.

    About the connection, i know that for Mifare 1k, would use MFS or NDEFTagConnection.

    In the visual.net aplication, i charge keys, then authenticate, and then write on the Mifare 1k tag, thats not need on the nokia?

    I really wanna know how i can write for example on the block 4(name), 5(e-mail), 6(phone number) for example, but dont know the way to do that and what must do with the target keys.

    Thanks and sorry for my english and if im asking some obvious.

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

    Re: NFC read mifare 1k with 6216 classic

    Hallo,

    Quote Originally Posted by Jorge Gil View Post
    About the connection, i know that for Mifare 1k, would use MFS or NDEFTagConnection.
    [...]
    I really wanna know how i can write for example on the block 4(name), 5(e-mail), 6(phone number) for example, but dont know the way to do that and what must do with the target keys.
    You can't do this with the NDEFTagConnection but only with the MFStandardConnection. The NDEFTagConnection is an abstraction to the Forum tag types (and in case of the Nokia API also for Mifare Classic).

    Quote Originally Posted by Jorge Gil View Post
    In the visual.net aplication, i charge keys, then authenticate, and then write on the Mifare 1k tag, thats not need on the nokia?
    That's pretty much the same as you would do on the Nokia:
    1. You open the connection:
    Code:
    MFStandardConnection conn = (MFStandardConnection) Connector.open(targetUrl);
    2. Then your application can either use the Mifare application directory (MAD) to find its allocated secors or can access the sector immediatly without the MAD:
    Code:
    MFSector mfsector = conn.getSector(sectorIndex);
    3. Within the secor you can access each block with:
    Code:
    MFBlock mfblock = mfsector.getBlock(blockIndex);
    4. Then you can read the block's data with something like this (NOW you authenticate with the sector specific key):
    Code:
    byte[] KEY_SECTOR = {(byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF};
    byte[] blockdata = new byte[mfblock.size()];
    readcnt = mfblock.read(new MFKey.KeyA(KEY_SECTOR), blockdata, 0, 0, mfblock.size());
    br,
    Michael

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

    Re: NFC read mifare 1k with 6216 classic

    Of course, you could also use the NDEFTagConnection and take advantage of the abstraction level of the NFC Data Exchange Format (NDEF). This way you could use the same code to write to any NFC tag (i.e. Topaz, Mifare Ultralight, ...) and even Mifare Classic. But as you would then work independent of the actual tag format, you could no longer force the location of your data (i.e. name is in sector 4, e-mail in sector 5). Instead you would have one NDEF message that is composed of one or more NDEF records, where each record holds one or more of your data elements.

    br,
    Michael

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    16

    Thumbs up Re: NFC read mifare 1k with 6216 classic

    uoooh

    I see, really thanks for your explanation, help me too much.

    I will work on that and when take any score i will come back here to told.

    Thanks for the info .

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    16

    Re: NFC read mifare 1k with 6216 classic

    Here is a part of the code developed on visual.net,

    Private Sub cargarClave()
    Dim indx As Integer
    Dim tmpStr As String

    Call ClearBuffers()
    'Load Authentication Keys command
    SendBuff(0) = &HFF 'Class
    SendBuff(1) = &H82 'INS
    SendBuff(2) = &H0 'P1 : Key Structure
    SendBuff(3) = CLng("&H" + "01") 'P2 : Key Number
    SendBuff(4) = &H6 'P3 : Lc
    SendBuff(5) = CLng("&H" + "FF") 'Key 1
    SendBuff(6) = CLng("&H" + "FF") 'Key 2
    SendBuff(7) = CLng("&H" + "FF") 'Key 3
    SendBuff(8) = CLng("&H" + "FF") 'Key 4
    SendBuff(9) = CLng("&H" + "FF") 'Key 5
    SendBuff(10) = CLng("&H" + "FF") 'Key 6
    'SendBuff(10) = CLng("&H" + tKey6.Text) 'Key 6

    SendLen = 11
    RecvLen = 2

    retCode = SendAPDU()
    If retCode <> ModWinsCard.SCARD_S_SUCCESS Then
    Exit Sub
    Else
    For indx = RecvLen - 2 To RecvLen - 1

    tmpStr = tmpStr + Microsoft.VisualBasic.Right("00" & Hex(RecvBuff(indx)), 2) + " "

    Next indx
    'Check for response
    If tmpStr.Trim <> "90 00" Then

    Call displayOut(4, 0, "Load authentication keys error!")

    End If
    End If

    End Sub


    Private Sub autentificar()

    Dim tmpLong As Byte
    Dim tempInt, indx As Integer
    Dim tmpStr As String

    Call ClearBuffers()
    'Authentication command
    SendBuff(0) = &HFF 'Class
    SendBuff(1) = &H86 'INS
    SendBuff(2) = &H0 'P1
    SendBuff(3) = &H0 'P2
    SendBuff(4) = &H5 'Lc
    SendBuff(5) = &H1 'Byte 1 : Version number
    SendBuff(6) = &H0 'Byte 2
    If bloque = "01" Then
    SendBuff(7) = CInt(bloque) 'Byte 3 : Block number
    Else
    SendBuff(7) = bloque 'CInt(bloque) 'Byte 3 : Block number
    End If

    SendBuff(8) = &H60 'Byte 4 : Key Type A

    SendBuff(9) = CInt("&H" & "01") 'Byte 5 : Key number

    SendLen = 10
    RecvLen = 2

    retCode = SendAPDU()
    If retCode <> ModWinsCard.SCARD_S_SUCCESS Then
    Exit Sub
    Else
    For indx = 0 To RecvLen - 1

    tmpStr = tmpStr + Microsoft.VisualBasic.Right("00" & Hex(RecvBuff(indx)), 2) + " "

    Next indx

    'Checking for response
    If tmpStr.Trim = "90 00" Then

    Call displayOut(0, 0, "Authentication success")
    Else
    Call displayOut(4, 0, "Authentication failed")
    End If
    End If


    End Sub

    I still work with your help, thanks

    PD: this code in case anyone was left with the curiosity to know.

  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    16

    Re: NFC read mifare 1k with 6216 classic

    #
    Code:
    TextField textField;   //cuadro de texto
        Ticker ticker;      //marquesina
        boolean intr = false;   //booleano de comprobacion
        String nombre="";
        private int sectorIndex=2;
        private int blockIndex=1;
        private int readcnt;
        String internalUrl = System.getProperty("internal.mf.url");
    #


    #
    Code:
    public void targetDetected(TargetProperties[] properties) {
            try {
                TargetProperties target = properties[0]; // always at least one target
    
                 MFStandardConnection conn = (MFStandardConnection) Connector.open(internalUrl);
    
                 MFSector mfsector = conn.getSector(sectorIndex);
    
                MFBlock mfblock = mfsector.getBlock(blockIndex);
    
                byte[] KEY_SECTOR = {(byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF};
                byte[] blockdata = new byte[mfblock.size()];
                readcnt = mfblock.read(new MFKey.KeyA(KEY_SECTOR), blockdata, 0, 0, mfblock.size());
    
                textField.setString(nombre);
    
            } catch (Exception e) {
                textField.setString("Vuelva a escanear la etiqueta");
            } finally {
                closeQuietly(conn);   //<---- Here fail Why?
            }
        }
    
        private void closeQuietly(javax.microedition.io.Connection conn) {
            try {
                conn.close();
            } catch (Exception ex) {
                // Nada
            }
        }
    #

    How can i do the text read from the tag, put into screen on my textField?
    And when i try to read always do "Vuelva a escanear la etiqueta" its the same like Scan the tag again.

    Thanks

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

    Re: NFC read mifare 1k with 6216 classic

    Hallo Jorge Gil,

    Quote Originally Posted by Jorge Gil (as private message)
    I dont understand some like:

    Code:
    readcnt = mfblock.read(new MFKey.KeyA(KEY_SECTOR), blockdata, 0, 0, mfblock.size());
    i think that KeyA is the key (obviuos maybe),
    blockdata means you are trying to read data from a block, but dstOffSet, srcOffSet what is? the bit where you start (0) to read and where finish(0)? then, what is length?
    Have a look at the JavaDoc for Nokia's JSR-257 extensions. This documentation comes with both SDKs (i.e. the one for the Nokia 6131 NFC and the one for the Nokia 6221). It's usually located somewhere in "<NOKIA_SDKS_ROOT>\Nokia\Devices\Nokia_6131_NFC_SDK_1_1\docs\nfc_ext" and "<NOKIA_SDKS_ROOT>\Nokia\Devices\S40_Nokia_6212_NFC_SDK\doc\WebHelp\WebHelp\jsr257nokiaextensions" respectively.

    Regarding the read function the documentation gives something like this:
    read

    public int read(MFKey key,
    byte[] dst,
    int dstOffset,
    int srcOffset,
    int length)
    throws MFStandardException,
    java.io.IOException


    Reads a region of bytes from this data area and stores the read result in the dst array starting at offset dstOffset.

    If the reading is only partially succesful then no exception will be thrown. For example if the first 16 bytes is read successfully but then access conditions deny further reading, the reading stops. If reading fails right from the start then an MFStandardException is thrown. The amount of data read successfully is returned as the return value.

    Parameters:
    key - the authentication key. If null then the device's default keys are used.
    dst - the destination array.
    dstOffset - start position in the destination array.
    srcOffset - the byte offset within this data area.
    length - the amount of bytes to read.
    Returns:
    amount of data read in bytes.
    Throws:
    MFStandardException - if authentication fails.
    java.io.IOException - if a connection error occurs.
    java.lang.NullPointerException - if dst is null.
    java.lang.IllegalArgumentException - if either the source or destination byte range is out of bounds.
    This means that key can either be a KeyA or a KeyB object that specifies the authentication key (Mifare Auth Key A or B respectively).

    dst is an array, that will be filled with the read data bytes.

    dstOffset is the index of the array dst, where the first data byte of the read data will be. This, if dstOffset is 0, then dst[0] will hold the first data byte, dst[1] will hold the second data byte, ... If dstOffset is 3, then the first data byte will be in dst[3], the second in dst[4], ...

    srcOffset is the position (in byte) within the block, where the read procedure should begin. I.e. if this is 2, the first two bytes of the block will be skipped.

    Therefore a read will look like this:
    blockdata[dstOffset + 0] := data in block[srcOffset + 0]
    blockdata[dstOffset + 1] := data in block[srcOffset + 1]
    ...
    blockdata[dstOffset + length - 2] := data in block[srcOffset + length - 2]
    blockdata[dstOffset + length - 1] := data in block[srcOffset + length - 1]


    br,
    Michael

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

    Re: NFC read mifare 1k with 6216 classic

    Hallo,

    Quote Originally Posted by Jorge Gil View Post
    Code:
    String internalUrl = System.getProperty("internal.mf.url");
    
    [...]
    
    public void targetDetected(TargetProperties[] properties) {
      try {
        TargetProperties target = properties[0]; // always at least one target
    
        MFStandardConnection conn = (MFStandardConnection) Connector.open(internalUrl);
    
        MFSector mfsector = conn.getSector(sectorIndex);
    
      } catch (Exception e) {
        textField.setString("Vuelva a escanear la etiqueta");
      } finally {
        closeQuietly(conn);   //<---- Here fail Why?
      }
    }
    How can i do the text read from the tag, put into screen on my textField?
    And when i try to read always do "Vuelva a escanear la etiqueta" its the same like Scan the tag again.
    I'm not sure what you are trying to accomplish here. On the one hand you are reacting on the targetDetected event, but on the other hand you try to connect to the internal Mifare section of the mobile phone (internalUrl = System.getProperty("internal.mf.url")).

    So, to connect to the external Mifare card that triggered the event, you should change the line
    Code:
    MFStandardConnection conn = (MFStandardConnection) Connector.open(internalUrl);
    to something like this
    Code:
    MFStandardConnection conn = (MFStandardConnection) Connector.open(target.getUrl(MFStandardConnection.class));
    br,
    Michael

  11. #11
    Registered User
    Join Date
    May 2010
    Posts
    16

    Re: NFC read mifare 1k with 6216 classic

    Quote Originally Posted by mroland View Post
    Hallo,

    I'm not sure what you are trying to accomplish here. On the one hand you are reacting on the targetDetected event, but on the other hand you try to connect to the internal Mifare section of the mobile phone (internalUrl = System.getProperty("internal.mf.url")).

    br,
    Michael
    Hello Michael:

    Im trying to read a tag and put the information of 3 blocks into 3 textfields to show on the screen. internalUrl was to try if the phone do something (watched on internet, jejeje).

    Then, connection is open, sector and block selected
    Code:
    public void targetDetected(TargetProperties[] properties) {
                 try {
                TargetProperties target = properties[0]; // always at least one target
                MFStandardConnection conn = (MFStandardConnection) Connector.open(target.getUrl(MFStandardConnection.class));
                MFSector mfsector = conn.getSector(sectorIndex);
                MFBlock mfblock = mfsector.getBlock(blockIndex);
    And then charge keys, and read the byte array
    Code:
      byte[] KEY_SECTOR = {(byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF};
                byte[] blockdata = new byte[mfblock.size()];
                readcnt = mfblock.read(new MFKey.KeyA(KEY_SECTOR), blockdata, 0, 0, mfblock.size());
    For finish wanna show the text on the tag in the screen, how to do it.
    Shomethink like: string nombre = readcnt.toString();??

    Code:
    textField.setString(nombre);
    Maybe im asking too much but, im doing my studentship and they just give me a pc, a Nokia 6212, few tags and told me do that (but they dont know how to do it) and i wanna learn because are good to me.

    Thank you again.

  12. #12
    Registered User
    Join Date
    May 2010
    Posts
    16

    Re: NFC read mifare 1k with 6216 classic

    It WORKS
    Code:
    textField.setString(new String(blockdata));
    and put into screen *^_^*
    Thanks.

    Now next step its read another 2 blocks and put the 3 dates on the same screen.

    When finish it, then must write on the tag, same application could read and write at same time?
    I have one to read and one to write. But dont know if on the same targetDetected could do at same time.

    Thanks very much again, hope i will understand lot of things of NFC to help people too.

    PD: sorry for my english again :P

    PD2: i will come back ^_^

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

    Re: NFC read mifare 1k with 6216 classic

    Hallo,

    Quote Originally Posted by Jorge Gil View Post
    It WORKS
    Code:
    textField.setString(new String(blockdata));
    Fine. Here you might also want to take a look into specifying the right character encoding in the String() constructor. I.e. with new String(blockdata, "ISO8859-1") you apply the ISO 8859-1 character encoding to the bytes from blockdata. Similarly, you could use new String(blockdata, "UTF-8") if the encoding of blockdata was UTF-8.

    Quote Originally Posted by Jorge Gil View Post
    When finish it, then must write on the tag, same application could read and write at same time?
    Of course, the same application can read and write data from/to a tag. Even in the same instance of the targetDetected event.

    br,
    Michael

  14. #14
    Registered User
    Join Date
    May 2010
    Posts
    16

    Re: NFC read mifare 1k with 6216 classic

    Fine. Here you might also want to take a look into specifying the right character encoding in the String() constructor. I.e. with new String(blockdata, "ISO8859-1") you apply the ISO 8859-1 character encoding to the bytes from blockdata. Similarly, you could use new String(blockdata, "UTF-8") if the encoding of blockdata was UTF-8.
    At the moment its enought read/write jejeje but i will remember all that you said.


    Of course, the same application can read and write data from/to a tag. Even in the same instance of the targetDetected event.

    br,
    Michael
    This will be next step, maybe tomorrow ask for more information, today wanna finish the read section :P

    Im very happy today, it woooorks

  15. #15
    Registered User
    Join Date
    May 2010
    Posts
    16

    Re: NFC read mifare 1k with 6216 classic

    Hello Michael:

    Its time to write :P

    I will put on the same targetDetected the part of read and the part of writ but...it works?

    How can i do? using a switch? or must make with if?, or just put after the read section?

    and for write i supose must be same, but changing the word read for the word write, ehehehe.

    Jorge

Similar Threads

  1. [moved, again] I get error messages when I try to write to Mifare 1k
    By andersljung in forum Near Field Communication
    Replies: 3
    Last Post: 2008-06-18, 12:10
  2. JSR 257 Read Mifare Ultra Light
    By Alex_Polt in forum Near Field Communication
    Replies: 9
    Last Post: 2007-08-17, 12:09
  3. Nokia 6131 NFC SDK 1.0 has been released
    By Nokia Ron in forum Near Field Communication
    Replies: 0
    Last Post: 2007-03-26, 22:28
  4. Can't read RFID Tag with NFC Shell
    By begermic in forum Mobile Java Tools & SDKs
    Replies: 5
    Last Post: 2007-01-31, 23:02
  5. socket read hangs until socket is closed
    By camroe in forum Mobile Java Networking & Messaging & Security
    Replies: 1
    Last Post: 2005-07-21, 09:44

Posting Permissions

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