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);
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;
}
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 ;).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With