This website uses cookies for visitor traffic analysis. By using the website, you agree with storing the cookies on your computer.More information

New project: embedded NFC

The source code for NFC reading/writing (mifare cards, chip PN532) is available.

See http://sourceforge.net/projects/enfc.

If you use Eclipse, simply check out and open the included project. It should work without problems. A build.xml for ant is included and creates an enfc.jar under <project>/build/install/.

How to use the library?

First, test if your hardware works. Start the JUnit test cases:

<TestNFC>.testDiagnose
<TestNFC>.testCommunication

Both should be green.

If you have problems, check if your authentication Key is 0xFF_FF_FF_FF_FF_FF and change the port for the serial interface (default: COM6). Add the system property "-Dserialport=xxx" to your launch configuration or change AbstractNFCTest.

The library has one very important class and one important class :)

  • The very important class is NFC. It handles the communication with your hardware (supports read and write operations).
  • The important class is NFCReader

The NFCReader reads two sectors (á 3 Blocks á 16bytes). The first sector contains a name (usually your first and last name). The second sector contains an authentication key (whatever). If you manage different data or want save read operations, check the source of NFCReader and create your own reader - it's easy.

Before you can read data, you have to write data to your card. Use following test case to write data:

<TestNFC>.testWrite

but don't forget to change the username and auth key. The defaults are René Jahn and 010203 ;-)

A simple test application:

public static void main(String[] pArgs)
{
  nfcr = new NFCReader();
  nfcr.setPort(System.getProperty("serialport"));
  nfcr.setAuthenticationKey(new byte[] {(byte)0xFF, (byte)0xFF, (byte)0xFF, (byte)0xFF, (byte)0xFF, (byte)0xFF});
  nfcr.addListener(new INFCListener()
  {
    public void nearFieldCommunication(final NFCEvent pEvent)
    {
      switch (pEvent.getType())
      {
        case BlockRead:
                byte[] byData = new byte[48];
                                                       
                List<?> liBytes = (List<?>)pEvent.getObject();
                                                       
                for (int i = 0; i < 3; i++)
                {
                  System.arraycopy((byte[])liBytes.get(i), 0, byData, i * 16, 16);     
                }
                                                       
                String sName = NFC.toString(byData);

                byData = new byte[48];

                for (int i = 3, j = 0; i < 6; i++, j++)
                {
                  System.arraycopy((byte[])liBytes.get(i), 0, byData, j * 16, 16);     
                }
                                                       
                String sKey = NFC.toString(byData);
                                                       
                System.out.println("Welcome: " + sName + " [" + sKey + "]");
                break;
        case Error:
                ((Exception)pEvent.getObject()).printStackTrace();
                break;
      }
    }
  });
       
  nfcr.searchCards(false);
}

Leave a Reply

Spam protection by WP Captcha-Free