NDEF使用NFC Tools java格式化Mifare classik卡

时间:2014-02-02 16:39:05

标签: nfc mifare ndef

有谁知道如何使用java NFC工具库直接格式化Mifare经典1k卡作为NDEF?我使用的是ACR 122U读写器

1 个答案:

答案 0 :(得分:0)

我使用此代码将Mifare Classik卡格式化为NDEF格式

package name.leiqin.test.nfc;

import java.io.IOException;

import org.nfctools.NfcAdapter;
import org.nfctools.api.TagInfo;
import org.nfctools.mf.classic.MfClassicNfcTagListener;
import org.nfctools.mf.ul.Type2NfcTagListener;
import org.nfctools.ndef.NdefOperations;
import org.nfctools.ndef.NdefOperationsListener;
import org.nfctools.scio.Terminal;
import org.nfctools.scio.TerminalHandler;
import org.nfctools.scio.TerminalMode;
import org.nfctools.spi.acs.AcsTerminal;
import org.nfctools.spi.scm.SclTerminal;
import org.nfctools.utils.LoggingUnknownTagListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class FormatCard implements NdefOperationsListener {
    private static final Logger logger = LoggerFactory
            .getLogger(FormatCard.class);

    public static void main(String[] args) throws IOException {
        TerminalHandler terminalHandler = new TerminalHandler();
        terminalHandler.addTerminal(new AcsTerminal());
        terminalHandler.addTerminal(new SclTerminal());

        Terminal terminal = terminalHandler.getAvailableTerminal(null);
        logger.info("terminal : {}", terminal);

        NfcAdapter adapter = new NfcAdapter(terminal, TerminalMode.INITIATOR);

        FormatCard formatListener = new FormatCard();
        adapter.registerTagListener(new Type2NfcTagListener(formatListener));
        adapter.registerTagListener(new MfClassicNfcTagListener(formatListener));

        adapter.registerUnknownTagListerner(new LoggingUnknownTagListener());
        logger.info("nfc adapter : {}", adapter);

        adapter.startListening();

        System.out.println("Waiting for tags, press ENTER to exit");
        System.in.read();
    }

    @Override
    public void onNdefOperations(NdefOperations ndefOperations) {
        TagInfo tagInfo = ndefOperations.getTagInfo();
        logger.info("tag id : {}", toHex(tagInfo.getId(), ""));
        logger.info("tag type : {}", tagInfo.getTagType());

        if (ndefOperations.isWritable()) {
            if (ndefOperations.isFormatted()) {
                logger.info("tag is already formatred");
            } else {
                logger.info("format start");
                ndefOperations.format();
                logger.info("format end");
            }
        } else {
            logger.info("Tag not writable");
        }
    }

    public static String toHex(byte[] bs, String separator) {
        if (separator == null)
            separator = " ";
        StringBuffer sb = new StringBuffer();
        for (byte b : bs) {
            int i = b & 0xFF; 
            String hex = Integer.toHexString(i).toUpperCase();
            if (hex.length() == 1)
                hex = "0" + hex;
            sb.append(hex);
            sb.append(separator);
        }
        sb.setLength(sb.length() - separator.length());
        return sb.toString();
    }

}