Author Topic: NFC reader with mifare tags  (Read 1851 times)

wbackup83

  • Newbie
  • *
  • Posts: 11
    • View Profile
NFC reader with mifare tags
« on: August 07, 2018, 13:04:29 »
I don't understand why using a mifare tag using the provided code i can read the tag page without authenticating ...moreover the state page ready is never accessed... for this reason the page if i change the tag still remains the same...
can you take a look at my code to understand if i m missing something or what?
Tags are found, i can print the page content without authenticating it...(commented)

public class ExampleScanForTags {
    private static final String HOST = "localhost";
    private static final int PORT = 4223;

    // Change XYZ to the UID of your NFC/RFID Bricklet
    private static final String UID = "uqB";

    private static short currentTagType = 0;

    // Note: To make the example code cleaner we do not handle exceptions. Exceptions
    //       you might normally want to catch are described in the documentation
    public static void main(String args[]) throws Exception {
        IPConnection ipcon = new IPConnection(); // Create IP connection
        // Note: Declare nr as final, so the listener can access it
        final BrickletNFCRFID nr = new BrickletNFCRFID(UID, ipcon); // Create device object

        ipcon.connect(HOST, PORT); // Connect to brickd
        // Don't use device before ipcon is connected

        // Add state changed listener
        nr.addStateChangedListener(new BrickletNFCRFID.StateChangedListener() {
            public void stateChanged(short state, boolean idle) {
               //System.out.println(state);
                try {
                    if(idle) {
                        currentTagType = 0;
                              //(short)((currentTagType + 1) % 3);
                        nr.requestTagID(currentTagType);
                       
                    }

                    if(state == BrickletNFCRFID.STATE_REQUEST_TAG_ID_READY) {
                        BrickletNFCRFID.TagID tagID = nr.getTagID();
                        String s = "Found tag of type " + tagID.tagType +
                                   " with ID [" + Integer.toHexString(tagID.tid[0]);

                        for(int i = 1; i < tagID.tidLength; i++) {
                            s += " " + Integer.toHexString(tagID.tid);
                        }

                        s += "]";
                        System.out.println(s);
                        //added code
                        short[] keys= {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
                  
                  //nr.authenticateMifareClassicPage(5, (short) 0,keys);
                  short[] data=nr.getPage();
                  System.out.println(Arrays.toString(data));
                    } 
                    if(state == BrickletNFCRFID.STATE_AUTHENTICATING_MIFARE_CLASSIC_PAGE_READY) {
                       System.out.println("Ready");
                       
                    }
                    if(state == BrickletNFCRFID.STATE_AUTHENTICATING_MIFARE_CLASSIC_PAGE_ERROR) {
                       System.out.println("error");
                       
                    }

                } catch(Exception e) {
                    System.out.println(e);
                }
            }
        });

        // Start scan loop
        nr.requestTagID(BrickletNFCRFID.TAG_TYPE_MIFARE_CLASSIC);

        System.out.println("Press key to exit"); System.in.read();
        ipcon.disconnect();
    }
}

borg

  • Tinkerforge Staff
  • Administrator
  • Hero Member
  • *****
  • Posts: 3.142
    • View Profile
Re: NFC reader with mifare tags
« Reply #1 on: August 07, 2018, 21:53:16 »
I think if you call getPage you will get the page from the last successful read.

So if you call getPage in the REQUEST_TAG_ID_READY state, you will not actually read a new page.

You have to first authenticate, then request the page and then you can call getPage.
Wir sind die Borg, Widerstand ist Spannung durch Stromstärke!

wbackup83

  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: NFC reader with mifare tags
« Reply #2 on: August 07, 2018, 22:14:57 »
Thanks for the reply...but it is the same: it always returns the same page with different tags and the other states are never accessed.

 nr.addStateChangedListener(new BrickletNFCRFID.StateChangedListener() {
            public void stateChanged(short state, boolean idle) {
               //System.out.println(state);
                try {
                    if(idle) {
                        currentTagType = 0;
                              //(short)((currentTagType + 1) % 3);
                        nr.requestTagID(currentTagType);
                       
                    }

                    if(state == BrickletNFCRFID.STATE_REQUEST_TAG_ID_READY) {
                        BrickletNFCRFID.TagID tagID = nr.getTagID();
                        String s = "Found tag of type " + tagID.tagType +
                                   " with ID [" + Integer.toHexString(tagID.tid[0]);

                        for(int i = 1; i < tagID.tidLength; i++) {
                            s += " " + Integer.toHexString(tagID.tid);
                        }

                        s += "]";
                        System.out.println(s);
                        //added code
                        short[] keys= {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
                  
                  nr.authenticateMifareClassicPage(5, (short) 0,keys);
                  nr.requestPage(5);
                  short[] data=nr.getPage();
                  System.out.println(Arrays.toString(data));
                    } 
                    if(state == BrickletNFCRFID.STATE_AUTHENTICATING_MIFARE_CLASSIC_PAGE_READY) {
                       System.out.println("Ready");
                       
                    }
                    if(state == BrickletNFCRFID.STATE_AUTHENTICATING_MIFARE_CLASSIC_PAGE_ERROR) {
                       System.out.println("error");
                       
                    }

                } catch(Exception e) {
                    System.out.println(e);
                }
            }
        });

        // Start scan loop
        nr.requestTagID(BrickletNFCRFID.TAG_TYPE_MIFARE_CLASSIC);

        System.out.println("Press key to exit"); System.in.read();
        ipcon.disconnect();

wbackup83

  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: NFC reader with mifare tags
« Reply #3 on: August 07, 2018, 22:17:20 »
What i would expect it would be to access the STATE_AUTHENTICATING_MIFARE_CLASSIC_PAGE_READY state after calling authenticateMifareClassicPage(). But this does not happen...

borg

  • Tinkerforge Staff
  • Administrator
  • Hero Member
  • *****
  • Posts: 3.142
    • View Profile
Re: NFC reader with mifare tags
« Reply #4 on: August 07, 2018, 22:26:23 »
If you comment in the "//System.out.println(state);", do you get any new state after you call authenticateMifareClassicPage()? Or no new state at all?
Wir sind die Borg, Widerstand ist Spannung durch Stromstärke!

wbackup83

  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: NFC reader with mifare tags
« Reply #5 on: August 08, 2018, 07:56:32 »
If you intend as it is reported there with the print of the state commented, NO, I do not get any state at all...

borg

  • Tinkerforge Staff
  • Administrator
  • Hero Member
  • *****
  • Posts: 3.142
    • View Profile
Re: NFC reader with mifare tags
« Reply #6 on: August 08, 2018, 09:17:03 »
Strange, do you now what kind of mifare classic tag you have? Is it something special? Do you have a second one you could test with?
Wir sind die Borg, Widerstand ist Spannung durch Stromstärke!

wbackup83

  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: NFC reader with mifare tags
« Reply #7 on: August 08, 2018, 09:26:30 »
https://www.amazon.it/gp/product/B01FLOQNMI/ref=oh_aui_detailpage_o02_s00?ie=UTF8&psc=1

I have these... I tried with another tag of the same brand and it's the same...

borg

  • Tinkerforge Staff
  • Administrator
  • Hero Member
  • *****
  • Posts: 3.142
    • View Profile
Re: NFC reader with mifare tags
« Reply #8 on: August 08, 2018, 10:19:51 »
Looking at your code, you do:

Code: [Select]
nr.authenticateMifareClassicPage(5, (short) 0,keys);
nr.requestPage(5);
short[] data=nr.getPage();
System.out.println(Arrays.toString(data));

I think you should first call

Code: [Select]
nr.authenticateMifareClassicPage(5, (short) 0,keys);

then in the state STATE_AUTHENTICATING_MIFARE_CLASSIC_PAGE_READY call
Code: [Select]
nr.requestPage(5);

and then in the state STATE_REQUEST_PAGE_READY call
Code: [Select]
short[] data=nr.getPage();
System.out.println(Arrays.toString(data));

Does it work when you do that?
Wir sind die Borg, Widerstand ist Spannung durch Stromstärke!

wbackup83

  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: NFC reader with mifare tags
« Reply #9 on: August 08, 2018, 12:09:56 »
Nope...it does not enter in the state ...never..
I attach the java file so you can try it if you want

thanks again for the support

borg

  • Tinkerforge Staff
  • Administrator
  • Hero Member
  • *****
  • Posts: 3.142
    • View Profile
Re: NFC reader with mifare tags
« Reply #10 on: August 08, 2018, 14:02:45 »
Hi, it works after a few tweaks:

Code: [Select]
olaf@pc2:~/build20/java$ javac -cp Tinkerforge.jar:. ExampleScanForTags.java
olaf@pc2:~/build20/java$ java -cp Tinkerforge.jar:. ExampleScanForTags
Press key to exit
Found tag of type 0 with ID [6d b8 97 f7]
Ready
page ready
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

The Problem with your code is that you call "nr.requestTagID(currentTagType);" every time that the Bricklet is in idle state. But it goes into idle state after it reaches any of the new states. So you call requestTagID in a loop. For me it started to work immediately after i commented out the if(idle) {...} block.
Wir sind die Borg, Widerstand ist Spannung durch Stromstärke!

wbackup83

  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: NFC reader with mifare tags
« Reply #11 on: August 08, 2018, 14:51:47 »
can you please attach the code or paste it?

borg

  • Tinkerforge Staff
  • Administrator
  • Hero Member
  • *****
  • Posts: 3.142
    • View Profile
Re: NFC reader with mifare tags
« Reply #12 on: August 08, 2018, 15:22:51 »
See attachment.

Edit: Dont forget to change the UID back to yours.
Wir sind die Borg, Widerstand ist Spannung durch Stromstärke!

wbackup83

  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: NFC reader with mifare tags
« Reply #13 on: August 08, 2018, 15:35:01 »
This works if I put the tag on the reader when i run the main... What i need is to scan and be active for other tags ... for this reason I don't understand where to put the loop ...

borg

  • Tinkerforge Staff
  • Administrator
  • Hero Member
  • *****
  • Posts: 3.142
    • View Profile
Re: NFC reader with mifare tags
« Reply #14 on: August 08, 2018, 15:48:11 »
Well, if no tag is there you will get into the state STATE_REQUEST_TAG_ID_ERROR and you have to run requestTagID from there again.

I attached a working program that runs in a loop.

Please note: All of the steps can create an error. For example: What happens if someone removes the card after the authentication but before the page was read? In this case you probably want to scan for a tag again.

You have to handle all of these states if you want it to be robust.
Wir sind die Borg, Widerstand ist Spannung durch Stromstärke!