Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the NFC Hardware ID In Android

Tags:

android

nfc

I want to do something fairly straightforward, but I can't quite work out if the method in the Gingerbread API is for the ID of the token being scanned or the hardware on-board the Nexus S. What I want to be able to do is get the unique identifier of the NFC chip of the device, so I can register it (eg. when the device is waived over an RFID reader, I can associate the device being waived with an account). Is this possible with the current API methods available?

The piece of code that looks most promising (but I can't test because I don't have a device) is

byte[] tagId = intent.getByteArrayExtra(NfcAdapter.EXTRA_ID);
like image 881
Matt Avatar asked Dec 17 '22 18:12

Matt


2 Answers

tagId is set to an array of bytes. You need to parse that array to a hex string. There's lots of ways to do that, but this code will do it without resorting to external libraries and it's easy to see what's going on:

String ByteArrayToHexString(byte [] inarray) 
    {
    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 = (int) inarray[j] & 0xff;
        i = (in >> 4) & 0x0f;
        out += hex[i];
        i = in & 0x0f;
        out += hex[i];
        }
    return out;
}
like image 180
Adam Laurie Avatar answered Dec 28 '22 06:12

Adam Laurie


In version 2.3.3 you have class Tag and if you'll get that object fron intent you can use method getId(),

Tag myTag = (Tag) intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);

and if you need tag id from byte[] as "String" you have to parse it from byte to hex ;).

like image 20
Robert Avatar answered Dec 28 '22 06:12

Robert