Пример #1
0
        /// <summary>
        /// Sample code to hande a couple of different cards based on the identification process
        /// </summary>
        /// <returns>None</returns>
        private async Task HandleCard(SmartCard card)
        {
            try
            {
                // Connect to the card
                using (SmartCardConnection connection = await card.ConnectAsync())
                {
                    // Try to identify what type of card it was
                    IccDetection cardIdentification = new IccDetection(card, connection);
                    await cardIdentification.DetectCardTypeAync();

                    LogMessage("Connected to card\r\nPC/SC device class: " + cardIdentification.PcscDeviceClass.ToString());
                    LogMessage("Card name: " + cardIdentification.PcscCardName.ToString());
                    LogMessage("ATR: " + BitConverter.ToString(cardIdentification.Atr));

                    if ((cardIdentification.PcscDeviceClass == Pcsc.Common.DeviceClass.StorageClass) &&
                        (cardIdentification.PcscCardName == Pcsc.CardName.MifareUltralightC ||
                         cardIdentification.PcscCardName == Pcsc.CardName.MifareUltralight ||
                         cardIdentification.PcscCardName == Pcsc.CardName.MifareUltralightEV1))
                    {
                        // Handle MIFARE Ultralight
                        MifareUltralight.AccessHandler mifareULAccess = new MifareUltralight.AccessHandler(connection);

                        await mifareULAccess.ReadCapsAsync();

                        if (!mifareULAccess.isNTag21x)
                        {
                            var ignored1 = this.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async() =>
                            {
                                var msgbox = new Windows.UI.Popups.MessageDialog("This application only works with the NXP NTAG21x line of NFC chips. Sorry.");
                                msgbox.Commands.Add(new Windows.UI.Popups.UICommand("OK"));
                                await msgbox.ShowAsync();
                            });

                            return;
                        }

                        bool authenticated = false;

                        byte[] password = await ParseField(passwordBox, 4);

                        byte[] passwordAck = await ParseField(passwordAckBox, 2);

                        if (password != null && passwordAck != null)
                        {
                            try
                            {
                                await mifareULAccess.AuthenticateWithPassword(password, passwordAck);

                                authenticated = true;
                            }
                            catch (Exception ex)
                            {
                                Debug.WriteLine("Exception sending provisioning password: "******"Exception sending provisioning password: "******"ReadCount:  " + responseAccess.ToString());

                            var ignored2 = this.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
                            {
                                accessCount.Text = responseAccess.ToString();
                            });

                            accessCountEnabled = true;
                        }
                        catch (Exception ex)
                        {
                            Debug.WriteLine("Exception sending Getting Access Count: " + ex);
                        }

                        if (!accessCountEnabled)
                        {
                            var ignored3 = this.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
                            {
                                accessCount.Text             = " ";
                                accessCountEnable.Visibility = Visibility.Visible;
                            });

                            if (autoProvision.IsChecked == true)
                            {
                                await mifareULAccess.EnableAccessCountAsync();
                            }
                        }

                        for (byte i = 0; i < mifareULAccess.Blocks; i++)
                        {
                            byte[] response = await mifareULAccess.ReadAsync((byte)(4 * i));

                            for (byte y = 0; y < 4; y++)
                            {
                                byte[] buf4 = new byte[4];
                                Array.Copy(response, y * 4, buf4, 0, 4);
                                LogMessage((i * 4 + y).ToString("x2") + ": " + BitConverter.ToString(buf4));
                            }
                        }

                        byte[] responseUid = await mifareULAccess.GetUidAsync();

                        string uidString = BitConverter.ToString(responseUid);
                        LogMessage("UID:  " + uidString);

                        var ignored4 = this.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
                        {
                            uid.Text            = uidString;
                            UidPanel.Visibility = Visibility.Visible;
                        });
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine("Exception handling card: " + ex.ToString());
            }
        }