morteza
morteza

Reputation: 33

Card ID is different when I use NFC reader on Android and RFID reader on PC

I'm trying to create an Android app to read some RFID card using the NFC reader.

When I read the RFID chip on my PC, the reader returns the ID 3853004524 for the card. When I try to read the same card using my app, I get a different ID (23646168229).

In my app, I use the intent extra NfcAdapter.EXTRA_ID to obtain the ID of the card. I convert that byte array to a string using the method:

private String byteArrayToDecimal(byte[] barray) {
    String result="";
    for (byte b : barray) {
        result = result + (b & 0xff);
    }
    return result;
}

protected void onNewIntent(Intent intent) {
    if (intent.getAction().equals(NfcAdapter.ACTION_TAG_DISCOVERED)) {
        Toast.makeText(G.context,"NFC Tag\n" +
                byteArrayToDecimal(intent.getByteArrayExtra(NfcAdapter.EXTRA_ID)),Toast.LENGTH_LONG).show();//ByteArrayToDecString
    }
}

Why do I get two different IDs for the same card? How can I get the same ID that I get on my PC through the app?

Upvotes: 1

Views: 3123

Answers (2)

Alec Mironov
Alec Mironov

Reputation: 1

Maybe it is too late. Just in case if somebody is looking this solution.

In .Net is a build-in class to do the same:

BitConverter.ToUInt32(barray, 0);

Upvotes: 0

Michael Roland
Michael Roland

Reputation: 40831

Both values (3853004524 and 23646168229) are essentially the same ID value. The reason that you get such an odd value from your app is that you are using a rather strange conversion method to convert a byte array into a string.

Let's first look at the value 3853004524 that you get from the RFID reader on your PC. This seems to be the decimal representation of the card ID. To better match that to what you get on Android, let's convert it to its hexadecimal representation: 0xE5A82EEC. This could be a valid 4-byte UID (as it is used, e.g., in NFC-A).

Let's next have a look at your rather odd conversion function:

private String byteArrayToDecimal(byte[] barray) {
    String result="";
    for (byte b : barray) {
        result = result + (b & 0xff);
    }
    return result;
}

This method takes every byte from the input byte array, casts it to an unsigned decimal integer, and appends the decimal representation to the string. If we try that with the card ID that you get on your PC, this would lead to:

  • 0xE5 gets "229"
  • 0xA8 gets "168"
  • 0x2E gets "46"
  • 0xEC gets "236"

When you look at the value that you got in your app, you will realize that all of these decimal numbers are concatenated in your result. Moreover, you will see that all numbers were concatenated in reverse order. This means that the byte array that you obtained from intent.getByteArrayExtra(NfcAdapter.EXTRA_ID) was

{ (byte)0xEC, (byte)0x2E, (byte)0xA8, (byte)0xE5 }

In order to convert that into the value that your PC application prints out, you could use something like this:

private String byteArrayToDecimal(byte[] barray) {
    if (barray == null) return "";
    long result = 0;
    for (int i = barray.length - 1; i >= 0; --i) {
        result <<= 8;
        result |= barray[i] & 0x0FF;
    }
    return Long.toString(result);
}

Upvotes: 1

Related Questions