iTranslated by AI
The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
👋
Reading NFC Card UID with Unity on Mac using I-O DATA USB-NFC3
1. Overview
A continuation of the following article. Since I've managed to establish a connection, I will now read the card information.
2. Environment
- USB-NFC3 (Pi-Touch) | IC Card Reader | IODATA
- MacBook Pro (16-inch, 2019) / macOS Big Sur
- Unity 2021.3.16.f1
3. Method
3.1 Determining if a touch occurred
I referred to the following:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using PCSC;
using PCSC.Monitoring;
public class SimpleRead : MonoBehaviour
{
string readerName;
ISCardMonitor monitor;
void Start()
{
var contextFactory = ContextFactory.Instance;
using (var context = contextFactory.Establish(SCardScope.System)) {
Debug.Log("Currently connected readers: ");
var readerNames = context.GetReaders();
foreach (var readerName in readerNames) {
Debug.Log("\t" + readerName);
}
readerName = readerNames[0];
}
var monitorFactory = MonitorFactory.Instance;
monitor = monitorFactory.Create(SCardScope.System);
// connect events here..
monitor.StatusChanged += (sender, args) =>
Debug.Log($"New state: {args.NewState}");
monitor.Start(readerName);
}
void OnApplicationQuit()
{
monitor.Cancel();
monitor.Dispose();
}
It looks like this when executed. New state: Present is displayed when an NFC card is detected, and New state: Empty is displayed when the card is moved away.

I feel like it might freeze after quitting if monitor.Cancel(); and monitor.Dispose(); are not present.
3.2 Getting the information of the touched card
I referred to the following article.
Since the above article also referred to the PCSC-Sharp sample, I basically based the behavior after detecting the card on this sample.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using PCSC;
using PCSC.Monitoring;
using PCSC.Iso7816;
using System; // Use BitConverter
public class ReadCardInfo : MonoBehaviour
{
string readerName;
ISCardMonitor monitor;
void Start()
{
var contextFactory = ContextFactory.Instance;
// using (var context = contextFactory.Establish(SCardScope.System)) {
using (var context = contextFactory.Establish(SCardScope.System)) {
Debug.Log("Currently connected readers: ");
var readerNames = context.GetReaders();
foreach (var readerName in readerNames) {
Debug.Log("\t" + readerName);
}
readerName = readerNames[0];
}
var monitorFactory = MonitorFactory.Instance;
// var monitor = monitorFactory.Create(SCardScope.System);
monitor = monitorFactory.Create(SCardScope.System);
// connect events here..
monitor.StatusChanged += (sender, args) => {
Debug.Log($"New state: {args.NewState}");
if( args.NewState == SCRState.Present)
{
ReadData();
}
};
monitor.Start(readerName);
}
// read data
private void ReadData() {
using (var context = ContextFactory.Instance.Establish(SCardScope.System)) {
using (var rfidReader = context.ConnectReader(readerName, SCardShareMode.Shared, SCardProtocol.Any)) {
var apdu = new CommandApdu(IsoCase.Case2Short, rfidReader.Protocol) {
CLA = 0xFF,
Instruction = InstructionCode.GetData,
P1 = 0x00,
P2 = 0x00,
Le = 0 // We don't know the ID tag size
};
using (rfidReader.Transaction(SCardReaderDisposition.Leave)) {
Console.WriteLine("Retrieving the UID .... ");
var sendPci = SCardPCI.GetPci(rfidReader.Protocol);
var receivePci = new SCardPCI(); // IO returned protocol control information.
var receiveBuffer = new byte[256];
var command = apdu.ToArray();
var bytesReceived = rfidReader.Transmit(
sendPci, // Protocol Control Information (T0, T1 or Raw)
command, // command APDU
command.Length,
receivePci, // returning Protocol Control Information
receiveBuffer,
receiveBuffer.Length); // data buffer
var responseApdu =
new ResponseApdu(receiveBuffer, bytesReceived, IsoCase.Case2Short, rfidReader.Protocol);
Debug.Log(string.Format("SW1: {0:X2}, SW2: {1:X2}\nUid: {2}",
responseApdu.SW1,
responseApdu.SW2,
responseApdu.HasData ? BitConverter.ToString(responseApdu.GetData()) : "No uid received"));
}
}
}
}
void OnApplicationQuit()
{
monitor.Cancel();
monitor.Dispose();
}
}
I don't fully understand it, but for now, I was able to read the card's UID.

Discussion