Beispiel #1
0
        protected async override void OnNavigatedTo(NavigationEventArgs e)
        {
            // First try to find a reader that advertises as being NFC
            var deviceInfo = await SmartCardReaderUtils.GetFirstSmartCardReaderInfo(SmartCardReaderKind.Nfc);

            if (deviceInfo == null)
            {
                // If we didn't find an NFC reader, let's see if there's a "generic" reader meaning we're not sure what type it is
                deviceInfo = await SmartCardReaderUtils.GetFirstSmartCardReaderInfo(SmartCardReaderKind.Any);
            }

            if (deviceInfo == null)
            {
                LogMessage("NFC card reader mode not supported on this device");
                return;
            }

            if (m_cardReader == null)
            {
                m_cardReader = await SmartCardReader.FromIdAsync(deviceInfo.Id);

                m_cardReader.CardAdded   += cardReader_CardAdded;
                m_cardReader.CardRemoved += cardReader_CardRemoved;
            }
        }
Beispiel #2
0
        public async Task <bool> AttachReader(OnCardData onCardData)
        {
            isDetached = false;

            // check if we are already attached
            if (reader != null)
            {
                return(true);
            }

            // check to see if we can get the reader
            var deviceInfo = await SmartCardReaderUtils.GetFirstSmartCardReaderInfo(SmartCardReaderKind.Nfc);

            if (deviceInfo == null || isDetached)
            {
                return(false);
            }

            // instantiate reader
            reader = await SmartCardReader.FromIdAsync(deviceInfo.Id);

            if (!isDetached)
            {
                reader.CardAdded   += OnCardAdded;
                reader.CardRemoved += OnCardRemoved;

                customEvent    = onCardData;
                CardDataEvent += customEvent;
            }

            return(!isDetached);
        }
Beispiel #3
0
        private async void InitNFC()
        {
            var deviceInfo = await SmartCardReaderUtils.GetFirstSmartCardReaderInfo(SmartCardReaderKind.Nfc);

            if (m_cardReader == null)
            {
                m_cardReader = await SmartCardReader.FromIdAsync(deviceInfo.Id);

                m_cardReader.CardAdded   += CardReader_CardAdded;
                m_cardReader.CardRemoved += CardReader_CardRemoved;
            }
        }
Beispiel #4
0
        private async void setupCard()
        {
            var deviceInfo = await SmartCardReaderUtils.GetFirstSmartCardReaderInfo(SmartCardReaderKind.Nfc);

            if (deviceInfo == null)
            {
                System.Diagnostics.Debug.WriteLine("Failed: No readers!");
                // Bah, device doesn't support NFC.
                CardStatus = ScanningStatus.Other;
                return;
            }


            if (!deviceInfo.IsEnabled)
            {
                System.Diagnostics.Debug.WriteLine("Failed: NFC is off!");
                CardStatus = ScanningStatus.NFCOff;
                return;
            }

            // At this point, deviceInfo should be legit.

            if (m_reader == null)
            {
                System.Diagnostics.Debug.WriteLine("Setting up card for id " + deviceInfo.Id);
                m_reader = await SmartCardReader.FromIdAsync(deviceInfo.Id);

                var status = await m_reader.GetStatusAsync();

                System.Diagnostics.Debug.WriteLine("CardStatus is " + status);
                switch (status)
                {
                case SmartCardReaderStatus.Disconnected:
                    CardStatus = ScanningStatus.NFCOff;
                    return;

                case SmartCardReaderStatus.Exclusive:
                    CardStatus = ScanningStatus.Other;
                    return;

                case SmartCardReaderStatus.Ready:
                    CardStatus          = ScanningStatus.NoCard;
                    m_reader.CardAdded += cardEV_Tap;
                    break;

                default:
                    break;
                }
            }
        }
        private async void setupCard()
        {
            var deviceInfo = await SmartCardReaderUtils.GetFirstSmartCardReaderInfo(SmartCardReaderKind.Nfc);


            if (deviceInfo == null || (deviceInfo != null && !deviceInfo.IsEnabled))
            {
                // the Lumia 950 apparently has a problem where the NFC being off in some builds *disconnects* the device.
                // since we can hope that the device itself has an NFC reader (thanks to the store) we can, with relative
                // safety assume that the device is turned off.
                System.Diagnostics.Debug.WriteLine("Failed: NFC is off!");
                CardStatus = ScanningStatus.NFCOff;
                return;
            }

            // At this point, deviceInfo should be legit.

            if (m_reader == null)
            {
                System.Diagnostics.Debug.WriteLine("Setting up card for id " + deviceInfo.Id);
                m_reader = await SmartCardReader.FromIdAsync(deviceInfo.Id);

                var status = await m_reader.GetStatusAsync();

                System.Diagnostics.Debug.WriteLine("CardStatus is " + status);
                switch (status)
                {
                case SmartCardReaderStatus.Disconnected:
                    CardStatus = ScanningStatus.NFCOff;
                    return;

                case SmartCardReaderStatus.Exclusive:
                    CardStatus = ScanningStatus.Other;
                    return;

                case SmartCardReaderStatus.Ready:
                    CardStatus          = ScanningStatus.NoCard;
                    m_reader.CardAdded += cardEV_Tap;
                    break;

                default:
                    break;
                }
            }
        }
Beispiel #6
0
        protected async override void OnNavigatedTo(NavigationEventArgs e)
        {
            // Clear the messages
            //MainPage.Current.NotifyUser(String.Empty, NotifyType.StatusMessage, true);


            // First try to find a reader that advertises as being NFC
            var deviceInfo = await SmartCardReaderUtils.GetFirstSmartCardReaderInfo(SmartCardReaderKind.Generic);

            if (deviceInfo == null)
            {
                // If we didn't find an NFC reader, let's see if there's a "generic" reader meaning we're not sure what type it is
                deviceInfo = await SmartCardReaderUtils.GetFirstSmartCardReaderInfo(SmartCardReaderKind.Any);
            }

            if (deviceInfo == null)
            {
                LogMessage("NFC card reader mode not supported on this device", NotifyType.ErrorMessage);
                return;
            }

            if (!deviceInfo.IsEnabled)
            {
                var msgbox = new Windows.UI.Popups.MessageDialog("Your NFC proximity setting is turned off, you will be taken to the NFC proximity control panel to turn it on");
                msgbox.Commands.Add(new Windows.UI.Popups.UICommand("OK"));
                await msgbox.ShowAsync();

                // This URI will navigate the user to the NFC proximity control panel
                NfcUtils.LaunchNfcProximitySettingsPage();
                return;
            }

            if (m_cardReader == null)
            {
                m_cardReader = await SmartCardReader.FromIdAsync(deviceInfo.Id);

                m_cardReader.CardAdded   += cardReader_CardAdded;
                m_cardReader.CardRemoved += cardReader_CardRemoved;
            }
            //RegisterDevice("test");
        }
        public async Task InitializeAsync()
        {
            // First try to find a reader that advertises as being NFC
            var deviceInfo = await SmartCardReaderUtils.GetFirstSmartCardReaderInfo(SmartCardReaderKind.Nfc);

            if (deviceInfo == null)
            {
                // If we didn't find an NFC reader, let's see if there's a "generic" reader meaning we're not sure what type it is
                deviceInfo = await SmartCardReaderUtils.GetFirstSmartCardReaderInfo(SmartCardReaderKind.Any);
            }

            if (deviceInfo == null)
            {
                LogMessage("NFC card reader mode not supported on this device", NotifyType.ErrorMessage);
                return;
            }

            if (!deviceInfo.IsEnabled)
            {
                var msgbox = new Windows.UI.Popups.MessageDialog("Your NFC proximity setting is turned off, you will be taken to the NFC proximity control panel to turn it on");
                msgbox.Commands.Add(new Windows.UI.Popups.UICommand("OK"));
                await msgbox.ShowAsync();

                // This URI will navigate the user to the NFC proximity control panel
                NfcUtils.LaunchNfcProximitySettingsPage();
                return;
            }

            if (m_cardReader == null)
            {
                m_cardReader = await SmartCardReader.FromIdAsync(deviceInfo.Id);

                m_cardReader.CardAdded   += cardReader_CardAdded;
                m_cardReader.CardRemoved += cardReader_CardRemoved;
            }

            var path = Windows.ApplicationModel.Package.Current.InstalledLocation.Path + @"\Data\StationCode.csv";

            m_stationCodeList = await Core.Models.StationCodeData.LoadAsync(path);
        }
Beispiel #8
0
        protected async override void OnNavigatedTo(NavigationEventArgs e)
        {
            await LogMessageAsync(String.Empty, true);

            this.UpdateTargetCardFlag();

            var deviceInfo = await SmartCardReaderUtils.GetFirstSmartCardReaderInfo(SmartCardReaderKind.Nfc);

            if (deviceInfo == null)
            {
                deviceInfo = await SmartCardReaderUtils.GetFirstSmartCardReaderInfo(SmartCardReaderKind.Any);
            }

            if (deviceInfo == null)
            {
                await LogMessageAsync("NFCカードリーダがサポートされていません");

                return;
            }

            if (!deviceInfo.IsEnabled)
            {
                var msgbox = new Windows.UI.Popups.MessageDialog("設定画面でNFCをONにしてください");
                msgbox.Commands.Add(new Windows.UI.Popups.UICommand("OK"));
                await msgbox.ShowAsync();

                LaunchNfcProximitySettingsPage();
                return;
            }

            if (cardReader == null)
            {
                cardReader = await SmartCardReader.FromIdAsync(deviceInfo.Id);

                cardReader.CardAdded   += cardReader_CardAdded;
                cardReader.CardRemoved += cardReader_CardRemoved;
            }
        }
Beispiel #9
0
        private async void WaitForCard()
        {
            // First try to find a reader that advertises as being NFC
            var deviceInfo = await SmartCardReaderUtils.GetFirstSmartCardReaderInfo(SmartCardReaderKind.Generic);

            /*if (deviceInfo == null)
             * {
             *  // If we didn't find an NFC reader, let's see if there's a "generic" reader meaning we're not sure what type it is
             *  deviceInfo = await SmartCardReaderUtils.GetFirstSmartCardReaderInfo(SmartCardReaderKind.Any);
             * }*/

            if (deviceInfo == null)
            {
                //LogMessage("NFC card reader mode not supported on this device", NotifyType.ErrorMessage);
                return;
            }

            //Should show message async
            if (!deviceInfo.IsEnabled)
            {
                var msgbox = new Windows.UI.Popups.MessageDialog("Your NFC proximity setting is turned off, you will be taken to the NFC proximity control panel to turn it on");
                msgbox.Commands.Add(new Windows.UI.Popups.UICommand("OK"));
                await msgbox.ShowAsync();

                // This URI will navigate the user to the NFC proximity control panel
                //NfcUtils.LaunchNfcProximitySettingsPage();
                return;
            }

            if (m_cardReader == null)
            {
                m_cardReader = await SmartCardReader.FromIdAsync(deviceInfo.Id);

                m_cardReader.CardAdded   += CardReader_CardAdded;
                m_cardReader.CardRemoved += CardReader_CardRemoved;
            }
        }
Beispiel #10
0
        public void FindCard(int timeoutInSecs = 10,
                             CancellationTokenSource tokenSource = null)
        {
            Task findCardTask = Task.Factory.StartNew(async() =>
            {
                // SmartCardReaderを取得
                var deviceInfo = await SmartCardReaderUtils.GetFirstSmartCardReaderInfo(SmartCardReaderKind.Nfc);

                this.IsSupported = deviceInfo != null;
                this.IsEnabled   = (deviceInfo != null) && deviceInfo.IsEnabled;

                if (!this.IsSupported)
                {
                    throw new FelicaReaderNotSupportedException("NFC is not supported");
                }
                else if (!this.IsEnabled)
                {
                    throw new FelicaReaderNotEnabledException("NFC is not enabled");
                }

                this.reader = await SmartCardReader.FromIdAsync(deviceInfo.Id);

                // Felicaカードを探索
                var cts = tokenSource ?? new CancellationTokenSource();
                try
                {
                    var task = this.FindSmartCard(this.reader, cts.Token);
                    task.Wait(timeoutInSecs * 1000);

                    if (task.Status == TaskStatus.RanToCompletion)
                    {
                        var smartCard = task.Result;
                        SmartCardConnection connection = await smartCard.ConnectAsync();

                        IccDetection cardIdentification = new IccDetection(smartCard, connection);
                        await cardIdentification.DetectCardTypeAync();

                        if (cardIdentification.PcscDeviceClass == Pcsc.Common.DeviceClass.StorageClass &&
                            cardIdentification.PcscCardName == Pcsc.CardName.FeliCa)
                        {
                            this.felicaCardSubject.OnNext(new FelicaCardMediaImplementation(connection));
                        }
                        else
                        {
                            throw new UnknownCardException(
                                String.Format("Unknown device: PcscDeviceClass={0}, PcscCardName={1}",
                                              cardIdentification.PcscDeviceClass,
                                              cardIdentification.PcscCardName));
                        }
                    }
                    else if (task.Status == TaskStatus.Canceled)
                    {
                        throw new FelicaReaderTimeoutException("Read Timeout");
                    }
                    else
                    {
                        // Timeoutが発生した場合
                        throw new FelicaReaderTimeoutException("Read Timeout");
                    }
                }
                catch (AggregateException)
                {
                    throw new FelicaReaderException();
                }
            });

            return;
        }