Ejemplo n.º 1
0
        /// <inheritdoc/>
        public void Callback(object obj)
        {
            Debug.WriteLine("Callback: called - XNFC");
            if (obj is Intent intent)
            {
                var tag      = intent.GetParcelableExtra(NfcAdapter.ExtraTag) as Tag;
                var messages = intent.GetParcelableArrayExtra(NfcAdapter.ExtraNdefMessages)?.Cast <NdefMessage>().ToList();

                Debug.WriteLine($"Callback: {messages?.Count} messages - XNFC");
                if (messages == null)
                {
                    return;
                }

                foreach (var mess in messages)
                {
                    var message = mess.GetRecords();
                    var atag    = new AndroidTag(tag, mess);
                    foreach (var msg in message)
                    {
                        Debug.WriteLine("Callback: " + Encoding.UTF8.GetString(msg.GetPayload()));
                    }
                    TagDetected?.Invoke(this,
                                        new TagDetectedEventArgs(atag));
                }
            }
            else
            {
                Console.WriteLine($"obj is not Intent, can't do callback - XNFC");
            }
        }
Ejemplo n.º 2
0
        public void OnTagDiscovered(Tag tag)
        {
            try
            {
                var techs = tag.GetTechList();

                if (!techs.Contains(Java.Lang.Class.FromType(typeof(Ndef)).Name))
                {
                    return;
                }

                var ndef = Ndef.Get(tag);

                ndef.Connect();

                var ndefMessage = ndef.NdefMessage;
                var records     = ndefMessage.GetRecords();

                ndef.Close();

                var nfcTag = new NfcDefTag(ndef, records);

                TagDetected?.Invoke(nfcTag);
            }
            catch (Exception ex)
            {
                // handle errors
            }
        }
Ejemplo n.º 3
0
        public override async void DidDetectTags(NFCNdefReaderSession session, INFCNdefTag[] tags)
        {
            System.Diagnostics.Debug.WriteLine("========> DidDetectTags");

            _tag = tags[0];
            await session.ConnectToTagAsync(_tag);

            TagDetected?.Invoke(this, new NfcTagDetectedEventArgs(
                                    tagId: string.Empty, //// TODO: CHECK HOW TO GET ID
                                    ndefMessage: await ReadNdefAsync()));
        }
Ejemplo n.º 4
0
 private void OnNewIntentReceived(object sender, Intent e)
 {
     if (e.Action == Android.Nfc.NfcAdapter.ActionNdefDiscovered)
     {
         _tag = e.GetParcelableExtra(Android.Nfc.NfcAdapter.ExtraTag) as Android.Nfc.Tag;
         var ndefMessage = NdefMessage.FromByteArray(Android.Nfc.Tech.Ndef.Get(_tag).CachedNdefMessage
                                                     .ToByteArray());
         TagDetected?.Invoke(this, new NfcTagDetectedEventArgs(
                                 BitConverter.ToString(_tag.GetId()).Replace("-", ":"),
                                 ndefMessage));
     }
 }
Ejemplo n.º 5
0
        public async Task StartListeningAsync()
        {
            _reader = new NfcReader();
            var messages = await _reader.ScanAsync();

            foreach (var message in messages)
            {
                TagDetected?.Invoke(new NfcDefTag(message));
            }

            await StopListeningAsync();
        }
Ejemplo n.º 6
0
        public Task StartReadingAsync()
        {
            Task.Run(() =>
            {
                System.Threading.Thread.Sleep(_delay);

                if (_tag == null)
                {
                    TagDetected?.Invoke(NfcTagFake.RandomTextRecord());
                }
                else
                {
                    TagDetected?.Invoke(_tag);
                }
            });


            return(Task.CompletedTask);
        }
Ejemplo n.º 7
0
 /// <summary>
 /// Raise tag detected event
 /// </summary>
 /// <param name="nfcTagType">NFC tag type</param>
 /// <param name="conn">Connection instance to NFC tag</param>
 private void OnTagDetected(NfcTagType nfcTagType, NfcTagConnection conn)
 {
     TagDetected?.Invoke(this, new NfcTagEventArgs(nfcTagType, conn));
 }
Ejemplo n.º 8
0
        public async Task StartReadingAsync()
        {
            var message = await ScanAsync();

            TagDetected?.Invoke(message);
        }
Ejemplo n.º 9
0
        private void StartMonitoring()
        {
            // Start readers monitoring.
            _deviceMonitorFactorySubscription = DeviceMonitorFactory.Instance
                                                .CreateObservable(SCardScope.System)
                                                .Subscribe(
                onNext: deviceMonitorEvent =>
            {
                var readers = deviceMonitorEvent.Readers;
                Debug.WriteLine($"---> {deviceMonitorEvent.GetType().Name} " +
                                $"- readers: {string.Join(", ", readers)}");

                switch (deviceMonitorEvent)
                {
                case DeviceMonitorInitialized deviceMonitorInitialized:
                    break;

                case ReadersAttached readersAttached:
                    _monitorFactorySubscription?.Dispose();
                    _monitorFactorySubscription = null;
                    break;

                case ReadersDetached readersDetached:
                    if (_reader != null && !readers.Contains(_reader))
                    {
                        // Active reader has been removed. Abort anything ongoing.
                        _reader = null;
                    }
                    _monitorFactorySubscription?.Dispose();
                    _monitorFactorySubscription = null;
                    break;

                default:
                    return;
                }

                // Start card monitoring if there are readers.
                if (readers.Count() != 0)
                {
                    _monitorFactorySubscription = MonitorFactory.Instance
                                                  .CreateObservable(SCardScope.System, readers)
                                                  .Subscribe(
                        onNext: monitorEvent =>
                    {
                        var reader = monitorEvent.ReaderName;
                        Debug.WriteLine($"---> {monitorEvent.GetType().Name} " +
                                        $"- reader: {reader}");

                        switch (monitorEvent)
                        {
                        case CardInserted inserted:
                            if (_isSessionEnabled)
                            {
                                _reader = reader;
                                TagDetected?.Invoke(this, new NfcTagDetectedEventArgs(
                                                        tagId: GetTagId(),
                                                        ndefMessage: ReadNdef()));
                            }

                            break;

                        case CardRemoved removed:
                            _reader = null;
                            break;

                        case MonitorInitialized initialized:
                        case MonitorCardInfoEvent infoEvent:
                        case CardStatusChanged statusChanged:
                        default:
                            return;
                        }
                    },
                        onError: ex =>
                    {
                        Debug.WriteLine($"Card Error: {ex.Message} _reader: {_reader}");
                        _reader = null;
                    });
                }
            },
                onError: ex =>
            {
                Debug.WriteLine($"Reader Error: {ex.Message} _reader: {_reader}");
                _reader = null;
                Task.Run(() => StartMonitoring());
            });
        }