0
votes

I'm very new in NFC for Android. I need very much code example or good tutorial for Java for how get Mifare Ultralight 16 digits UID reading with Nexus 5

I only know how to get 7 digits UID for MifareClassic from here Reading the tag UID of Mifare classic card, but there is no examples for Mifare Ultralight.

This is another example to get UID for Mifare Classic. What do I need to change to make it read for Mifare Ultralight? And I don't understand what performs in ByteArrayToHexString()

byte[] nfcUID = null;
        if (intent != null && (nfcUID = intent.getByteArrayExtra(NfcAdapter.EXTRA_ID)) != null) {

            uid = ByteArrayToHexString(nfcUID);

private String ByteArrayToHexString(byte[] inarray) { // converts byte arrays to string
        int i, j, in;
        String[] hex = {
                "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"
        };
    String out = "";

    for (j = 0; j < inarray.length; ++j) {
        in = inarray[j] & 0xff;
        i = (in >> 4) & 0x0f;
        out += hex[i];
        i = in & 0x0f;
        out += hex[i];
    }
    return out;
}

Thank you very much!

1

1 Answers

4
votes

First of all, MIFARE Classic tags usually have a 4 byte UID (also called nUID). Newer MIFARE Classic tags also have 7 byte UIDs. MIFARE Ultralight tags always have a 7 byte UID.

This UID is what you will get from

byte[] uid = intent.getByteArrayExtra(NfcAdapter.EXTRA_ID);

Or from

Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
byte[] uid = tag.getId();

If you convert that UID into hexadecimal digits (as you indicated with the method in your post), you will get:

  • an 8 digit number if the UID has 4 bytes, or
  • a 14 digit number if the UID has 7 bytes.

So there is no way that you can get a 16 digit hexadecimal number for a MIFARE Ultralight tag.

As MIFARE Ultrlaight UIDs (or actually any 7 byte UIDs from NXP) have the form 0x04xxxxxxxxxxxx, converting this to a decimal number would give you at maximum a 16 digit number (0x04FFFFFFFFFFFF = 1407374883553279). So you might be trying to achieve this. However, converting a 4 byte UID to decimal would still not result in a 7 digit number.


Converting to hexadecimal representation

You could use something like this to convert the UID from byte array to a string of hexadecimal digits:

public static String convertByteArrayToHexString (byte[] b) {
    if (b != null) {
        StringBuilder s = new StringBuilder(2 * b.length);
        for (int i = 0; i < b.length; ++i) {
            final String t = Integer.toHexString(b[i]);
            final int l = t.length();
            if (l > 2) {
                s.append(t.substring(l - 2));
            } else {
                if (l == 1) {
                    s.append("0");
                }
                s.append(t);
            }
        }
        return s.toString();
    } else {
        return "";
    }
}

This method takes each byte of the byte array, converts it to a 2-digit hexadecimal number and concatenates all these 2-digit numbers into a string.


Converting to decimal representation

public static String convertByteArrayToDecString (byte[] b) {
    if (b != null) {
        BigInteger n = new BigInteger(1, b);
        return n.toString();
    } else {
        return "";
    }
}