/// <summary>
        /// NFC読取セッションがタグを検知されたら呼び出される。
        /// </summary>
        /// <param name="session">NFC読取セッション</param>
        /// <param name="tags">検知タグ</param>
        public override void DidDetectTags(NFCTagReaderSession session, INFCTag[] tags)
        {
            Debug.WriteLine($"DidDetectTags");

            // タグに接続する。
            if (tags.Length <= 0)
            {
                return;
            }
            session.ConnectTo(tags[0], connectErr =>
            {
                // 接続エラー時はメッセージを表示してNFC読取セッションを終了する、
                if (connectErr != null)
                {
                    Debug.WriteLine($"Connect Error = [{connectErr}]");
                    session.InvalidateSession("タグ接続失敗。");
                    return;
                }

                // FeliCa準拠のタグプロトコルを取得する。
                _felicaTag = tags[0].GetNFCFeliCaTag();
                if (_felicaTag == null)
                {
                    return;
                }

                // FeliCaのRequest Serviceコマンドを実行し、
                // サービスコード0x090Fを指定し、カード種別およびカード残額情報サービスに接続する。
                // (サービスコードはリトルエンディアンで)
                _felicaTag.RequestService(ServiceCodes, OnCompletedRequestService);
            });
        }
        /// <summary>
        /// Event raised when NFC tags are detected
        /// </summary>
        /// <param name="session">iOS <see cref="NFCTagReaderSession"/></param>
        /// <param name="tags">Array of iOS <see cref="INFCTag"/></param>
        public override void DidDetectTags(NFCTagReaderSession session, INFCTag[] tags)
        {
            var _tag = tags.First();

            var connectionError = string.Empty;

            session.ConnectTo(_tag, (error) =>
            {
                if (error != null)
                {
                    connectionError = error.LocalizedDescription;
                    return;
                }
            });

            var nMifareTag = _tag.GetNFCMiFareTag();
            //nMifareTag.SendMiFareCommand();
        }
Exemple #3
0
        /// <summary>
        /// Event raised when NFC tags are detected
        /// </summary>
        /// <param name="session">iOS <see cref="NFCTagReaderSession"/></param>
        /// <param name="tags">Array of iOS <see cref="INFCTag"/></param>
        public override void DidDetectTags(NFCTagReaderSession session, INFCTag[] tags)
        {
            var _tag = tags.First();

            var connectionError = string.Empty;

            session.ConnectTo(_tag, (error) =>
            {
                if (error != null)
                {
                    connectionError = error.LocalizedDescription;
                    return;
                }
            });

            var nMifareTag = _tag.GetNFCMiFareTag();

            var t = WriteMode ? Task.Run(() => HandleWiteTag(nMifareTag)):
                    Task.Run(() => HandleReadTag(nMifareTag));
        }
Exemple #4
0
        void MakeTagReadOnly(NFCTagReaderSession session, INFCTag tag, INFCNdefTag ndefTag)
        {
            session.ConnectTo(tag, (error) =>
            {
                if (error != null)
                {
                    Console.WriteLine(error.LocalizedDescription);
                    return;
                }

                ndefTag.WriteLock((error) =>
                {
                    if (error != null)
                    {
                        Console.WriteLine("Error when locking a tag on iOS: " + error.LocalizedDescription);
                    }
                    else
                    {
                        Console.WriteLine("Locking Successful!");
                    }
                });
            });
        }
Exemple #5
0
        /// <summary>
        /// Event raised when NFC tags are detected
        /// </summary>
        /// <param name="session">iOS <see cref="NFCTagReaderSession"/></param>
        /// <param name="tags">Array of iOS <see cref="INFCTag"/></param>
        public override void DidDetectTags(NFCTagReaderSession session, INFCTag[] tags)
        {
            _customInvalidation = false;
            _tag = tags.First();

            var connectionError = string.Empty;

            session.ConnectTo(_tag, (error) =>
            {
                if (error != null)
                {
                    connectionError = error.LocalizedDescription;
                    Invalidate(session, connectionError);
                    return;
                }

                var ndefTag = GetNdefTag(_tag);

                if (ndefTag == null)
                {
                    Invalidate(session, UIMessages.NFCErrorNotCompliantTag);
                    return;
                }

                ndefTag.QueryNdefStatus((status, capacity, error) =>
                {
                    if (error != null)
                    {
                        Invalidate(session, error.LocalizedDescription);
                        return;
                    }

                    if (status == NFCNdefStatus.NotSupported)
                    {
                        Invalidate(session, UIMessages.NFCErrorNotSupportedTag);
                        return;
                    }

                    var identifier = GetTagIdentifier(ndefTag);
                    var nTag       = new TagInfo(identifier)
                    {
                        IsWritable = status == NFCNdefStatus.ReadWrite
                    };

                    if (_isWriting)
                    {
                        // Write mode
                        OnTagDiscovered?.Invoke(nTag, _isFormatting);
                    }
                    else
                    {
                        // Read mode
                        ndefTag.ReadNdef((message, error) =>
                        {
                            if (error != null)
                            {
                                Invalidate(session, UIMessages.NFCErrorRead);
                                return;
                            }

                            if (message == null)
                            {
                                Invalidate(session, UIMessages.NFCErrorEmptyTag);
                                return;
                            }

                            session.AlertMessage = UIMessages.NFCSuccessRead;

                            nTag.Records = GetRecords(message.Records);
                            OnMessageReceived?.Invoke(nTag);
                            Invalidate(session);
                        });
                    }
                });
            });
        }
Exemple #6
0
        /// <summary>
        /// Event raised when NFC tags are detected
        /// </summary>
        /// <param name="session">iOS <see cref="NFCTagReaderSession"/></param>
        /// <param name="tags">Array of iOS <see cref="INFCTag"/></param>
        public override void DidDetectTags(NFCTagReaderSession session, INFCTag[] tags)
        {
            _customInvalidation = false;
            _tag = tags.First();

            var connectionError = string.Empty;

            session.ConnectTo(_tag, (error) =>
            {
                if (error != null)
                {
                    connectionError = error.LocalizedDescription;
                    Invalidate(session, connectionError);
                    return;
                }

                var ndefTag = GetNdefTag(_tag);

                if (ndefTag == null)
                {
                    Invalidate(session, Configuration.Messages.NFCErrorNotCompliantTag);
                    return;
                }

                ndefTag.QueryNdefStatus((status, capacity, error) =>
                {
                    if (error != null)
                    {
                        Invalidate(session, error.LocalizedDescription);
                        return;
                    }

                    var isNdefSupported = status != NFCNdefStatus.NotSupported;

                    var identifier = GetTagIdentifier(ndefTag);
                    var nTag       = new TagInfo(identifier, isNdefSupported)
                    {
                        IsWritable = status == NFCNdefStatus.ReadWrite,
                        Capacity   = Convert.ToInt32(capacity)
                    };

                    if (!isNdefSupported)
                    {
                        session.AlertMessage = Configuration.Messages.NFCErrorNotSupportedTag;

                        OnMessageReceived?.Invoke(nTag);
                        Invalidate(session);
                        return;
                    }

                    if (_isWriting)
                    {
                        // Write mode
                        OnTagDiscovered?.Invoke(nTag, _isFormatting);
                    }
                    else
                    {
                        // Read mode
                        ndefTag.ReadNdef((message, error) =>
                        {
                            // iOS Error: NFCReaderError.NdefReaderSessionErrorZeroLengthMessage (NDEF tag does not contain any NDEF message)
                            // NFCReaderError.NdefReaderSessionErrorZeroLengthMessage constant should be equals to 403 instead of 304
                            // see https://developer.apple.com/documentation/corenfc/nfcreadererror/code/ndefreadersessionerrorzerolengthmessage
                            if (error != null && error.Code != 403)
                            {
                                Invalidate(session, Configuration.Messages.NFCErrorRead);
                                return;
                            }

                            session.AlertMessage = Configuration.Messages.NFCSuccessRead;

                            nTag.Records = GetRecords(message?.Records);
                            OnMessageReceived?.Invoke(nTag);
                            Invalidate(session);
                        });
                    }
                });
            });
        }