Question

I'm developing a Xamarin.iOS application using the LineaPro 5 peripheral, which is able to scan barcodes, RFID cards and swipe magnetic cards. I have the basic RFID functionality working, and the data coming from the Linea that I care about is the UID of the card (a byte array).

In our application, which interacts with a web server, the format in which we use to identify these cards is decimal, thus I have this code which translates the UID byte array to the decimal string we need:

// Handler attached to the RFID Scan event invoked by the LineaPro
void HandleRFIDScanned (DTDeviceDelegate Dispatcher, RFIDScannedEventArgs Arguments)
{
  if ( Arguments == null || Arguments.Data == null || Arguments.Data.UID == null )
    InvalidRFIDScanned ();
  else
  {
    byte[] SerialArray = new byte[Arguments.Data.UID.Length];
    System.Runtime.InteropServices.Marshal.Copy(Arguments.Data.UID.Bytes, SerialArray, 0, SerialArray.Length);
    string Hex = Util.ByteArrayToHexString (SerialArray);
    if ( string.IsNullOrWhiteSpace (Hex) )
      InvalidRFIDScanned ();
    else
    {
      string DecimalSerial = ulong.Parse (Hex, System.Globalization.NumberStyles.HexNumber).ToString ();
      ValidRFIDScanned (DecimalSerial);
    }
  }

  //Disconnecting card so another can be scanned
  NSError RFDisconnectError;
  LineaPRO.Shared.Device.RfRemoveCard (Arguments.CardIndex, out RFDisconnectError);
}

//Byte array to hexadecimal string conversion
public static string ByteArrayToHexString (byte[] Bytes)
{
  StringBuilder hex = new StringBuilder();
  foreach (byte b in Bytes)
    hex.AppendFormat("{0:x2}", b);
  return hex.ToString();
}

However, I have discovered a very concerning issue with some of the RFID cards we have been issued. We have a variety of cards, differing in style and ISO standard, that the mobile app needs to scan. One of which (I believe of the Mifare Classic standard, though I cannot confirm at this moment) is always a ten-digit number from this particular RFID card provider, though some of them begin with the number 0, as in this picture:

RFID card with leading zero

This causes a huge issue with my byte array conversion, as the hexadecimal string is parsed into an unsigned long type, and the leading zero is dropped. We use another set of USB RFID readers in a separate application in order to store these RFID card ID's into a database, though somehow those USB readers are able to pick up the leading zero.

Therefore, a conflict is reached when using the mobile application, in which the UID's leading zero is dropped, since the data passed to the API is checked against the database and is then not considered a match, because of the missing zero.

I have looked at all of the data received by the LineaPro in my event handler and that byte array is the only thing which holds the UID of the card, so as long as we are using the decimal representation of the UIDs, there is no way for the mobile app to determine whether or not a zero should be there, unless:

  1. Perhaps some of the RFID standards have a specific restriction on the number of digits in the UID's decimal representation? For example, if this type of card always has an even or specific number of decimal digits, I can pad the string with an extra zero if necessary.

  2. The LineaPro is simply not delivering sufficient data, in which case I'm probably screwed.

Was it helpful?

Solution 2

I've discovered that the specific configuration settings used with our USB RFID readers, in conjunction with the format of cards received from the vendor, are to blame. Here is a screenshot of the configuration we use with the USB readers:

The RFID reader configuration causing the issue

We have them set to force a 10 digit-long decimal UID when reading, padding shorter IDs and truncating longer ones. I have informed the other developers that the proper method of reading these IDs should be in their proper hexadecimal format with no specific length, so as to support other RFID card types without any hard-coded ID formats.

OTHER TIPS

You don't have enough information to solve your problem. If the ID numbers are always supposed to be 10 digits it is trivial to use a format string to add leading zeros as needed.

I'd say try always padding the UID to 10 digits with leading zeros and then run a large number of test values through it.

As you say, if your device is dropping valid data from the beginning of the ID then you are screwed.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top