trying to use external ACS NFC reader on windows using Android library and NFC tools

46 views
Skip to first unread message

Gurpreet Singh

unread,
Jul 26, 2018, 11:26:14 AM7/26/18
to nfc-developers
HI
as I tried to read and write Mifare ultralight using NFC tools -core It started giving me as unknown card error although it coiuld read all known tags and throw an error after them. I tried using Android library but I dont know what parameter to pass in Tag field


import android.nfc.Tag;
import android.nfc.tech.MifareUltralight;
import android.util.Log;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;

/**
 *
 */
public class MifareUltralightTagTester {

    private static int blockOffset = 4;
    private static int blockNumber = 16;
    private static int blockSize = 4;


    private static final String TAG = MifareUltralightTagTester.class.getSimpleName();

    /**
     * Write text to NFC Tag! Text size should be less than size of NFC tag memory! 46 bytes
     * @param tag
     * @param tagText
     * @return
     */
    public static boolean writeTag(Tag tag, String tagText) {
        boolean result = true;
        MifareUltralight ultralight = MifareUltralight.get(tag);
        if (tagText.length() < (blockNumber-blockOffset)*blockSize) {
            ArrayList<String> list = new ArrayList<String>();
            int start = 0;
            int end = blockSize;
            while (end < tagText.length()) {
                String text = tagText.substring(start, end);
                Log.d(TAG," - [" + text + "]" );
                list.add(text); 
                start +=blockSize;
                end += blockSize;
            }
            try {
                ultralight.connect();

                int page = blockOffset;
                for (String text:list) {
                    Log.d(TAG,"page=" + page + " [" + text + "]" );
                    ultralight.writePage(page, text.getBytes(Charset.forName("US-ASCII")));
                    page++;
                }
            } catch (IOException e) {
                Log.e(TAG, "IOException while closing MifareUltralight...", e);
                result = false;
            } finally {
                try {
                    ultralight.close();
                } catch (IOException e) {
                    Log.e(TAG, "IOException while closing MifareUltralight...", e);
                    result = false;
                }
            }
        } else {
            Log.e(TAG, "Text size bigger than MilfareUltralight size (46 byte)");
            result = false;
        }
        return result;
    }

    public static String readTag(Tag tag) {
        MifareUltralight mifare = MifareUltralight.get(tag);
        try {
            mifare.connect();
            byte[] payload1 = mifare.readPages(4);
            String part1 = new String(payload1, Charset.forName("US-ASCII"));
            byte[] payload2 = mifare.readPages(8);
            String part2 = new String(payload2, Charset.forName("US-ASCII"));
            return part1 + part2;
        } catch (IOException e) {
            Log.e(TAG, "IOException while writing MifareUltralight message...", e);
        } finally {
            if (mifare != null) {
                try {
                    mifare.close();
                }
                catch (IOException e) {
                    Log.e(TAG, "Error closing tag...", e);
                }
            }
        }
        return null;
    }
}
the NFCtagAdaptor which I use does not give me an option to getTag and also I dont know the tag listner
Reply all
Reply to author
Forward
0 new messages