private static async Task HandleCard(SmartCard args) { try { var newConnection = args.CreateMiFareCard(); lock (cardConnectionLock) { if (currentConnection != null) { currentConnection.Dispose(); currentCardId = null; currentConnection = null; } currentConnection = newConnection; } var cardId = await currentConnection.GetCardInfo(); Debug.WriteLine("Connected to card\r\nPC/SC device class: {0}\r\nCard name: {1}", cardId.PcscDeviceClass, cardId.PcscCardName); if (cardId.PcscDeviceClass == DeviceClass.StorageClass && (cardId.PcscCardName == CardName.MifareStandard1K || cardId.PcscCardName == CardName.MifareStandard4K)) { Debug.WriteLine("MiFare Classic card detected"); var uid = await currentConnection.GetUid(); currentCardId = uid.ByteArrayToString(); Debug.WriteLine("UID: " + currentCardId); } else { throw new NotImplementedException("Card type is not implemented"); } } catch (Exception ex) { Debug.WriteLine(ex); } }
/// <summary> /// Sample code to hande a couple of different cards based on the identification process /// </summary> /// <returns>None</returns> private async Task HandleCard(CardAddedEventArgs args) { try { card?.Dispose(); card = args.SmartCard.CreateMiFareCard(); var cardIdentification = await card.GetCardInfo(); DisplayText("Connected to card\r\nPC/SC device class: " + cardIdentification.PcscDeviceClass.ToString() + "\r\nCard name: " + cardIdentification.PcscCardName.ToString()); if (cardIdentification.PcscDeviceClass == MiFare.PcSc.DeviceClass.StorageClass && (cardIdentification.PcscCardName == CardName.MifareStandard1K || cardIdentification.PcscCardName == CardName.MifareStandard4K)) { // Handle MIFARE Standard/Classic DisplayText("MIFARE Standard/Classic card detected"); var uid = await card.GetUid(); DisplayText("UID: " + BitConverter.ToString(uid)); // 16 sectors, print out each one for (var sector = 0; sector < 16; sector++) { try { var data = await card.GetData(sector, 0, 48); string hexString = ""; for (int i = 0; i < data.Length; i++) { hexString += data[i].ToString("X2") + " "; } DisplayText(string.Format("Sector '{0}':{1}", sector, hexString)); } catch (Exception) { DisplayText("Failed to load sector: " + sector); } } } } catch (Exception e) { PopupMessage("HandleCard Exception: " + e.Message); } }