How get Mifare Ultralight 16 digits UID reading with Nexus 5 - android

I'm very new in NFC for Android. I need very much code example or good tutorial for Java for how get Mifare Ultralight 16 digits UID reading with Nexus 5
I only know how to get 7 digits UID for MifareClassic from here
Reading the tag UID of Mifare classic card, but there is no examples for Mifare Ultralight.
This is another example to get UID for Mifare Classic. What do I need to change to make it read for Mifare Ultralight? And I don't understand what performs in ByteArrayToHexString()
byte[] nfcUID = null;
if (intent != null && (nfcUID = intent.getByteArrayExtra(NfcAdapter.EXTRA_ID)) != null) {
uid = ByteArrayToHexString(nfcUID);
private String ByteArrayToHexString(byte[] inarray) { // converts byte arrays to string
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 = inarray[j] & 0xff;
i = (in >> 4) & 0x0f;
out += hex[i];
i = in & 0x0f;
out += hex[i];
}
return out;
}
Thank you very much!

First of all, MIFARE Classic tags usually have a 4 byte UID (also called nUID). Newer MIFARE Classic tags also have 7 byte UIDs. MIFARE Ultralight tags always have a 7 byte UID.
This UID is what you will get from
byte[] uid = intent.getByteArrayExtra(NfcAdapter.EXTRA_ID);
Or from
Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
byte[] uid = tag.getId();
If you convert that UID into hexadecimal digits (as you indicated with the method in your post), you will get:
an 8 digit number if the UID has 4 bytes, or
a 14 digit number if the UID has 7 bytes.
So there is no way that you can get a 16 digit hexadecimal number for a MIFARE Ultralight tag.
As MIFARE Ultrlaight UIDs (or actually any 7 byte UIDs from NXP) have the form 0x04xxxxxxxxxxxx, converting this to a decimal number would give you at maximum a 16 digit number (0x04FFFFFFFFFFFF = 1407374883553279). So you might be trying to achieve this. However, converting a 4 byte UID to decimal would still not result in a 7 digit number.
Converting to hexadecimal representation
You could use something like this to convert the UID from byte array to a string of hexadecimal digits:
public static String convertByteArrayToHexString (byte[] b) {
if (b != null) {
StringBuilder s = new StringBuilder(2 * b.length);
for (int i = 0; i < b.length; ++i) {
final String t = Integer.toHexString(b[i]);
final int l = t.length();
if (l > 2) {
s.append(t.substring(l - 2));
} else {
if (l == 1) {
s.append("0");
}
s.append(t);
}
}
return s.toString();
} else {
return "";
}
}
This method takes each byte of the byte array, converts it to a 2-digit hexadecimal number and concatenates all these 2-digit numbers into a string.
Converting to decimal representation
public static String convertByteArrayToDecString (byte[] b) {
if (b != null) {
BigInteger n = new BigInteger(1, b);
return n.toString();
} else {
return "";
}
}

Related

Why when reading NFCtag with Android phone you get different tag ID then when reading with dedicated reader?

I am using an Android Cilico F750 and the dedicated RFID reader is CF-RS103.
The RFID tag type is MIFARE Ultralight type C.
When read with a dedicated card reader the id of tag is: 2054270212(10 digit).
But when read with Android phone the id is: 36139312876727556(17digit) and reversed id is: 1316602805183616 (16digit).
Does anyone know why this happens and if its possible to convert the 10digit id to 17digit id or vice versa.
I use intents to detect tag and to resolve intent I use this:
public void resolveIntent(Intent intent){
String action = intent.getAction();
if(NfcAdapter.ACTION_TAG_DISCOVERED.equals(action)
||NfcAdapter.ACTION_TECH_DISCOVERED.equals(action)
||NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action))
{
Parcelable[] rawMsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
NdefMessage[] msgs;
if(rawMsgs!=null)
{
msgs= new NdefMessage[rawMsgs.length];
for(int i=0; i<rawMsgs.length; i++)
{
msgs[i]=(NdefMessage) rawMsgs[i];
}
}
else
{
byte[] empty = new byte[0];
byte[] id = intent.getByteArrayExtra(NfcAdapter.EXTRA_ID);
Tag tag = (Tag) intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
byte[] payload = dumpTagData(tag).getBytes();
NdefRecord record = new NdefRecord(NdefRecord.TNF_UNKNOWN,empty,id,payload);
NdefMessage msg = new NdefMessage(new NdefRecord[]{record});
msgs= new NdefMessage[] {msg};
}
displayMsgs(msgs);
}}
And this are my helper functions:
private void displayMsgs(NdefMessage[] msgs)
{
if(msgs==null || msgs.length==0) {
return;
}
StringBuilder builder = new StringBuilder();
List<ParsedNdefRecord> records= NdefMessageParser.parse(msgs[0]);
final int size = records.size();
for(int i=0;i<size;i++)
{
ParsedNdefRecord record = records.get(i);
String str = record.str();
builder.append(str).append("\n");
}
text.setText(builder.toString());
}
private String dumpTagData(Tag tag) {
StringBuilder sb = new StringBuilder();
byte[] id = tag.getId();
sb.append("ID (hex): ").append(toHex(id)).append('\n');
sb.append("ID (reversed hex):").append(toReversedHex(id)).append('\n');
sb.append("ID (dec): ").append(toDec(id)).append('\n');
sb.append("ID (reversed dec):").append(toReversedDec(id)).append('\n');
String prefix = "android.nfc.tech.";
sb.append("Technologies: ");
for (String tech: tag.getTechList()) {
sb.append(tech.substring(prefix.length()));
sb.append(", ");
}
sb.delete(sb.length() - 2, sb.length());
for (String tech: tag.getTechList()) {
if (tech.equals(MifareClassic.class.getName())) {
sb.append('\n');
String type = "Unknown";
try {
MifareClassic mifareTag = MifareClassic.get(tag);
switch (mifareTag.getType()) {
case MifareClassic.TYPE_CLASSIC:
type = "Classic";
break;
case MifareClassic.TYPE_PLUS:
type = "Plus";
break;
case MifareClassic.TYPE_PRO:
type = "Pro";
break;
}
sb.append("Mifare Classic type: ");
sb.append(type);
sb.append('\n');
sb.append("Mifare size: ");
sb.append(mifareTag.getSize() + " bytes");
sb.append('\n');
sb.append("Mifare sectors: ");
sb.append(mifareTag.getSectorCount());
sb.append('\n');
sb.append("Mifare blocks: ");
sb.append(mifareTag.getBlockCount());
} catch (Exception e) {
sb.append("Mifare classic error: " + e.getMessage());
}
}
if (tech.equals(MifareUltralight.class.getName())) {
sb.append('\n');
MifareUltralight mifareUlTag = MifareUltralight.get(tag);
String type = "Unknown";
switch (mifareUlTag.getType()) {
case MifareUltralight.TYPE_ULTRALIGHT:
type = "Ultralight";
break;
case MifareUltralight.TYPE_ULTRALIGHT_C:
type = "Ultralight C";
break;
}
sb.append("Mifare Ultralight type: ");
sb.append(type);
}
}
return sb.toString();
}
private String toHex(byte[] bytes) {
StringBuilder sb = new StringBuilder();
for (int i = bytes.length - 1; i >= 0; --i) {
int b = bytes[i] & 0xff;
if (b < 0x10)
sb.append('0');
sb.append(Integer.toHexString(b));
if (i > 0) {
sb.append(" ");
}
}
return sb.toString();
}
private String toReversedHex(byte[] bytes) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < bytes.length; ++i) {
if (i > 0) {
sb.append(" ");
}
int b = bytes[i] & 0xff;
if (b < 0x10)
sb.append('0');
sb.append(Integer.toHexString(b));
}
return sb.toString();
}
private long toDec(byte[] bytes) {
long result = 0;
long factor = 1;
for (int i = 0; i < bytes.length; ++i) {
long value = bytes[i] & 0xffl;
result += value * factor;
factor *= 256l;
}
return result;
}
private long toReversedDec(byte[] bytes) {
long result = 0;
long factor = 1;
for (int i = bytes.length - 1; i >= 0; --i) {
long value = bytes[i] & 0xffl;
result += value * factor;
factor *= 256l;
}
return result;
}`
EDIT: I managed to resolve this issue by truncating the 7-byte HEX ID to 4-bytes.
And then formating the decimal ID if its total lenght is less than 10 digits with this statement that basically adds zeroes from left side if DEC ID is smaller than 10 digits:
String strFinal=String.format("%010d", Long.parseLong(str));
This document that describes how the ID is converted from HEX8 TO DEC10 helped me alot aswell: https://www.batag.com/download/rfidreader/LF/RAD-A200-R00-125kHz.8H10D.EM.V1.1.pdf
And a huge thanks to #Andrew and #Karam for helping me resolve this!
The card reader on the PC is configured wrong, it is configured by default to display the ID as 10 digit decimal number (4 byte) when the card has a 7 byte ID.
It thus has to loose some data, it is doing this by truncating the ID to the first 4 bytes of the 7 byte ID
Use the software on the PC change the output format to something suitable for the ID size on the Mifare Ultralight C cards (8 Hex?)
or
Use Mifare Classic cards instead as these had 4 byte ID
or
truncate the 7 byte ID to 4 bytes e.g. change bytes.length to 4 (a hard coding to the first 4 bytes in the 7 byte ID) in your code and handle the fact that there is a very large number (around 16.7 million) of Mifare Ultralight C cards that will seem to have the same "ID" as you want to display it
This is because the spec's give by a seller on Amazon https://www.amazon.co.uk/Chafon-CF-RS103-Multiple-Support-Compatible-Black/dp/B017VXVZ66 (I cannot find any details on the manufacturer's site)
It says "Default output 10 digit Dec, control output format through software. "
"Support with windows,linux and android system, but can only set output format in windows pcs.No programming and software required, just plug and play. "
The only sensible answer is move everything to use a 7 byte ID.
I don't know why are you trying always to convert to decimal?
and please try to explain more about the code you use to read the UID.
about your numbers and to convert 17 digits to 10 digits; I convert both of them to Hex:
36139312876727556(17digit) in Hex : 8064837A71AD04.
2054270212(10 digit) in Hex: 7A71AD04
as you notice you can just tirm first three bytes to get the 10 digits.
and I do belive the both of them are not the UID. but the 7bytes as sayed Andrew, and you already read it in the your photo : (04:B5:71:7A:83:64:80)
So I think the answer is that because you are converting a 7 byte ID to decimal you are getting variable lengths of numbers because of the conversion to decimal.
"The byte data type is an 8-bit signed two's complement integer. It has a minimum value of -128 and a maximum value of 127 (inclusive)."
From https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
Could generate a decimal number with 1,2 or 3 characters thus as decimal the id can vary in length.
It also looks like that conversion is going wrong as in theory it should have negative numbers in there as well.
It is much better to handle it as a hex string if you want it to be human readable.
The correct method in Java to convert the 2 pages of the ID to hex is
StringBuilder Uid;
for (int i = 0; i < result.length; i++) {
// byte 4 is a check byte
if (i == 3) continue;
Uid.append(String.format("%02X ", result[i]));
}
Note as per the spec sheet of the card https://www.nxp.com/docs/en/data-sheet/MF0ICU2_SDS.pdf (Section 7.3.1)
There is check byte that is part of the ID, while this will always be the same on the same card and will still give you a unique ID it is technically not part of the ID.
Or if not reading at a low level then
https://developer.android.com/reference/android/nfc/Tag#getId()
will get you the id.
Note that the "36139312876727556(17digit) and reversed id" when converted to hex and reversed actual is 7 bytes and start with the right number.
The 10 digit just looks like the first 4 bytes of the 7 byte number also reversed.

Arduino send long string to Android

I'm trying to send long String to Android via bluetooth.
but,
It looks like the picture.
some characters are changed.
how can I get an exact full string?
arduino code :
for(int i=0;i<16;i++){
String rec = String(P[i], HEX);
if(rec.length()<2) rec = "0"+rec;
BTSerial.println(rec);
delay(50);
P is a byte array. Thanks.
Try it without String objects:
// return '0' .. 'F'
char hexnibble(byte nibble) {
nibble &= 0x0F; // just to be sure
if (nibble > 9) return 'A' + nibble - 10;
else return '0' + nibble;
}
void loop() {
byte P[16];
// ... fill P somehow ...
char rec[33];
for(int i=0;i<16;i++){
rec[2*i] = hexnibble(P[i] >> 4);
rec[2*i+1] = hexnibble(P[i] & 0x0F);
}
rec[32] = 0; // string terminator
Serial.println(rec); // just for debugging
delay(1000);
}

Getting the NFC Tag Serial Number

This is my first project working with NFC. I would like to simply get the ID for the NFC Tag. I have been following the response from this post. Here is the code:
public void onResume() {
super.onResume();
if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(getIntent().getAction())) {
Toast.makeText(this,"NFC on resume working",Toast.LENGTH_LONG).show();
byte[] tagId = getIntent().getByteArrayExtra(NfcAdapter.EXTRA_ID);
Log.i("EHEHEHEHEHE",tagId + "");
}
//process the msgs array
}
This is the response of the code:
07-06 22:07:29.804 16729-16729/za.co.bookbay.nfcplay I/EHEHEHEHEHE﹕ [B#423a1e18
07-06 22:08:08.644 16729-16729/za.co.bookbay.nfcplay I/EHEHEHEHEHE﹕ [B#423bfde0
07-06 22:08:09.574 16729-16729/za.co.bookbay.nfcplay I/EHEHEHEHEHE﹕ [B#423d6ec8
Now this number keeps changing, hence this leads me to believe that the above code is not getting the Tag's serial number or is this correct. If so, what is the reason for the number changing?
You're printing the byte[] object, not it's content. That's why you get the [B#SomeAddress in your output.
To print the id you can use something like this:
String hexdump = new String();
for (int i = 0; i < tagId.length; i++) {
String x = Integer.toHexString(((int) tagId[i] & 0xff));
if (x.length() == 1) {
x = '0' + x;
}
hexdump += x + ' ';
}
Log.i("EHEHEHEHEHE",hexdump);
(there is probably a better way to convert a byte-array to a hexdump-string, java is not my favorite programming language)
You can convert directly ByteArray to HexString with a little function published by #Roland:
fun ByteArray.toHexString() = joinToString("") { "%02x".format(it) }
Next link can you help more:
Converting a byte array into a hex string

What the value mean convert from string to Hex for BLE?

I am developing BLE in Android.
And I try to send string value to BLE device
It seem the string need to convert to byte before send to BLE device.
I found some code like the following , the code seems can convert the string value to byte.
private byte[] parseHex(String hexString) {
hexString = hexString.replaceAll("\\s", "").toUpperCase();
String filtered = new String();
for(int i = 0; i != hexString.length(); ++i) {
if (hexVal(hexString.charAt(i)) != -1)
filtered += hexString.charAt(i);
}
if (filtered.length() % 2 != 0) {
char last = filtered.charAt(filtered.length() - 1);
filtered = filtered.substring(0, filtered.length() - 1) + '0' + last;
}
return hexStringToByteArray(filtered);
}
public static byte[] hexStringToByteArray(String s) {
int len = s.length();
byte[] data = new byte[len / 2];
for (int i = 0; i < len; i += 2) {
data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
+ Character.digit(s.charAt(i+1), 16));
}
return data;
}
private int hexVal(char ch) {
return Character.digit(ch, 16);
}
I call the above function before send the string to the BLE device like the following code.
byte[] value = parseHex(text);
mCharacteristic.setValue(value);
mBluetoothGatt.writeCharacteristic(mCharacteristic);
The BLE device will show the value which I have send to it. But the value is strange and I did not under stand what it mean.
The value what I send and the value show from BLE device are like the following.
Send BLE Show
1 1
2 2
9 9
10 16
11 17
20 32
30 48
40 64
70 112
90 144
99 153
100 16
I did not understand what the value mean show on BLE device...
Does someone help me ?
You are sending the hex values, which is a base 16 system, but your BLE Show values are in decimal.
Therefore sending 10 in hex is 1*16+0*1=16 in decimal. Similarly, 99 is 9*16+9*1=153 in decimal

getBytes doesn't get the right bytes amount

I need to create a string of hex values. Now, i've got something like this.
String address = "5a f2 ff 1f";
But when getting this address into bytes:
byte[] bytes= address.getBytes();
It gets me each letter and space as a byte, instead of getting each 2 chars as a byte ang leaving the spaces. So...
How can i declare this?
private String CalcChecksum (String message) {
/**Get string's bytes*/
message = message.replaceAll("\\s","");
byte[] bytes = toByteArray(message);
byte b_checksum = 0;
for (int byte_index = 0; byte_index < byte_calc.length; byte_index++) {
b_checksum += byte_calc[byte_index];
}
int d_checksum = b_checksum; //Convert byte to int(2 byte)
int c2_checksum = 256 - d_checksum;
String hexString = Integer.toHexString(c2_checksum);
return hexString;
}
String address = "5a f2 ff 1f";
byte[] bytes = DatatypeConverter.parseHexBinary(address.replaceAll("\\s","")).getBytes();
As stated you're using hex, which you cannot use .getBytes() in the way you are trying to!
You need to specify that your string contains hex values. And in the solution below you need to remove all whitespaces from the string before converting it:
import javax.xml.bind.DatatypeConverter;
class HexStringToByteArray {
public static void main(String[] args) {
String address = "5A F2 FF 1F";
address = address.replaceAll("\\s","");
System.out.println(address);
byte[] bytes = toByteArray(address);
for( byte b: bytes ) {
System.out.println(b);
}
String string_again = toHexString(bytes);
System.out.println(string_again);
}
public static String toHexString(byte[] array) {
return DatatypeConverter.printHexBinary(array);
}
public static byte[] toByteArray(String s) {
return DatatypeConverter.parseHexBinary(s);
}
}
This will print (note that bytes as signed):
5AF2FF1F // Original address
90 // 5A
-14 // F2
-1 // FF
31 // 1F
5AF2FF1F // address retrieved from byte array

Categories

Resources