Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how do you read the unique ID of an NFC tag on android?

Tags:

android

nfc

Tag myTag = (Tag) intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
Log.i("tag ID", myTag.getId().toString());

This gives me an ID like "[B@40521c40" but this ID changes every read.

Any help would be greatly appreciated.

Thanks.

like image 731
user635028 Avatar asked May 19 '11 14:05

user635028


People also ask

Can you read any NFC tag?

9. Can NFC tags work with any NFC reader or smartphone? NFC tags can be read or encoded by any NFC reader or NFC compatible Smartphone (see updated list) which includes Android phones.

Can you change UID of NFC tag?

No, the UID of genuine Type 1 tags (from Broadcom or, formerly, Innovision) cannot be changed. That UID is a serial number that is permanently burned-in into read-only memory during the manufacturing process.


2 Answers

you still need to convert the byte to string:

private String bytesToHexString(byte[] src) {
    StringBuilder stringBuilder = new StringBuilder("0x");
    if (src == null || src.length <= 0) {
        return null;
    }

    char[] buffer = new char[2];
    for (int i = 0; i < src.length; i++) {
        buffer[0] = Character.forDigit((src[i] >>> 4) & 0x0F, 16);  
        buffer[1] = Character.forDigit(src[i] & 0x0F, 16);  
        System.out.println(buffer);
        stringBuilder.append(buffer);
    }

    return stringBuilder.toString();
}
like image 156
fordiy Avatar answered Oct 24 '22 11:10

fordiy


I ran in to similar issue and could resolve it. The issue is the conversion to string.

myTag.getId() returns byte array. You should convert these bytes to hex string. I used the following function that I found here in stackoverflow.com

    final protected static char[] hexArray = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
public static String bytesToHex(byte[] bytes) {
    char[] hexChars = new char[bytes.length * 2];
    int v;
    for ( int j = 0; j < bytes.length; j++ ) {
        v = bytes[j] & 0xFF;
        hexChars[j * 2] = hexArray[v >>> 4];
        hexChars[j * 2 + 1] = hexArray[v & 0x0F];
    }
    return new String(hexChars);
}
like image 31
user2240755 Avatar answered Oct 24 '22 13:10

user2240755