public void Dispose()
        {
            this.cardDetectionTimer.Change(Timeout.Infinite, Timeout.Infinite);

            this.cardDetectionTimer.Dispose();

            LibMifareApi.ftrMFCloseDevice(this.handle);
        }
        public CardReader AccessCardReader()
        {
            var handle = LibMifareApi.ftrMFOpenDevice();

            if (handle != IntPtr.Zero)
            {
                return(new CardReader(handle));
            }

            throw new Exception("Cannot open device");
        }
        private void CardDetectionCallback(object state)
        {
            LibMifareApi.ftrMFStartSequence(handle);

            var cardTypeBytes     = new byte[1];
            var serialNumberBytes = new byte[8];

            LibMifareApi.ftrMFActivateIdle8bSN(this.handle, cardTypeBytes, serialNumberBytes);

            LibMifareApi.ftrMFEndSequence(this.handle);

            var cardType = (CardType)cardTypeBytes[0];

            var isCardPresentNow = cardType != CardType.Invalid;

            if (isCardPresentNow != IsCardPresent)
            {
                if (isCardPresentNow)
                {
                    var serialNumber = BitConverter.ToUInt64(serialNumberBytes, 0);

                    this.CardSerialNumber = serialNumber;
                    this.CardType         = cardType;
                    this.IsCardPresent    = true;

                    this.OnCardDetected(new CardDetectedEventArgs {
                        SerialNumber = serialNumber, Type = cardType
                    });
                }
                else
                {
                    this.IsCardPresent    = false;
                    this.CardType         = CardType.Invalid;
                    this.CardSerialNumber = 0;

                    this.OnCardRemoved();
                }
            }
        }