10
votes

I am attempting to do what I would have guessed would be pretty simple, but as it turns out is not. I have an ACR122 NFC reader and a bunch of Mifare Classic and Mifare Ultralight tags, and all I want to do is read and write a mime-type and a short text string to each card from a Java application. Here's what I've got working so far:

  • I can connect to my reader and listen for tags
  • I can detect which type of tag is on the reader
  • On the Mifare Classic tags I can loop through all of the data on the tag (after programming the tag from my phone) and build an ascii string, but most of the data is "junk" data
  • I can determine whether or not there is an Application directory on the tag.

Here's my code for doing that:

Main:

public static void main(String[] args){
        TerminalFactory factory = TerminalFactory.getDefault();
        List<CardTerminal> terminals;
        try{
            TerminalHandler handler = new TerminalHandler();
            terminals = factory.terminals().list();
            CardTerminal cardTerminal = terminals.get(0);
            AcsTerminal terminal = new AcsTerminal();
            terminal.setCardTerminal(cardTerminal);
            handler.addTerminal(terminal);
            NfcAdapter adapter = new NfcAdapter(handler.getAvailableTerminal(), TerminalMode.INITIATOR);
            adapter.registerTagListener(new CustomNDEFListener());
            adapter.startListening();
            System.in.read();
            adapter.stopListening();
        }
        catch(IOException e){
        }
        catch(CardException e){
            System.out.println("CardException: " + e.getMessage());
        }

}

CustomNDEFListener:

public class CustomNDEFListener extends AbstractCardTool
{
    @Override
    public void doWithReaderWriter(MfClassicReaderWriter readerWriter)
            throws IOException{
        NdefMessageDecoder decoder = NdefContext.getNdefMessageDecoder();
        MadKeyConfig config = MfConstants.NDEF_KEY_CONFIG;
        if(readerWriter.hasApplicationDirectory()){
            System.out.println("Application Directory Found!");
            ApplicationDirectory directory = readerWriter.getApplicationDirectory();
        }
        else{
            System.out.println("No Application Directory Found, creating one.");
            readerWriter.createApplicationDirectory(config);

        }
    }
}

From here, I seem to be at a loss as for how to actually create and interact with an application. Once I can create the application and write Record objects to it, I should be able to write the data I need using the TextMimeRecord type, I just don't know how to get there. Any thoughts?

::Addendum:: Apparently there is no nfc-tools tag, and there probably should be. Would someone with enough rep be kind enough to create one and retag my question to include it?

::Second Addendum:: Also, I am willing to ditch NFC-Tools if someone can point me in the direction of a library that works for what I need, is well documented, and will run in a Windows environment.

1

1 Answers

-3
votes

Did you checked this library ? It is well written, how ever has poor documentation. Actually no more than JavaDoc.