Android Nfc Sample Demo - reads only fake information from the Tag

ubiquitous picture ubiquitous · Feb 22, 2011 · Viewed 20.2k times · Source

I just installed the Nfc Demo from google, but it doesn´t read the information from the Tag.-> It just provides some fakeTag information. Has anybody an idea, where I can change the sample to read from the nfc Tag? Or has somebody a working nfc demo for the nexus?

If we could bring a nfc demo to work, many people would have the possibility to develop a nfc demo on their own.

Best regards Alexander

Answer

Vili T picture Vili T · Mar 31, 2011

I had the same problem getting tag id. I got some B@2346323143 style data to screen. I got it to work like this:

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

You need to convert byte[] to hex string. For example using following method.

private static final byte[] HEX_CHAR_TABLE = { (byte) '0', (byte) '1',
        (byte) '2', (byte) '3', (byte) '4', (byte) '5', (byte) '6',
        (byte) '7', (byte) '8', (byte) '9', (byte) 'A', (byte) 'B',
        (byte) 'C', (byte) 'D', (byte) 'E', (byte) 'F' };

public static String getHexString(byte[] raw, int len) {
    byte[] hex = new byte[2 * len];
    int index = 0;
    int pos = 0;

    for (byte b : raw) {
        if (pos >= len)
            break;

        pos++;
        int v = b & 0xFF;
        hex[index++] = HEX_CHAR_TABLE[v >>> 4];
        hex[index++] = HEX_CHAR_TABLE[v & 0xF];
    }

    return new String(hex);
}