Esempio n. 1
0
        /// <summary>
        /// Event raised when NDEF messages are detected
        /// </summary>
        /// <param name="session">iOS <see cref="NFCNdefReaderSession"/></param>
        /// <param name="messages">Array of iOS <see cref="NFCNdefMessage"/></param>
        public override void DidDetect(NFCNdefReaderSession session, NFCNdefMessage[] messages)
        {
            OnTagConnected?.Invoke(null, EventArgs.Empty);

            if (messages != null && messages.Length > 0)
            {
                var first   = messages[0];
                var tagInfo = new TagInfo
                {
                    IsWritable = false,
                    Records    = GetRecords(first.Records)
                };
                OnMessageReceived?.Invoke(tagInfo);
            }

            OnTagDisconnected?.Invoke(null, EventArgs.Empty);
        }
Esempio n. 2
0
        /// <summary>
        /// Write or Clear a NDEF message
        /// </summary>
        /// <param name="tagInfo"><see cref="ITagInfo"/></param>
        /// <param name="clearMessage">Clear Message</param>
        /// <param name="makeReadOnly">Make tag read-only</param>
        internal void WriteOrClearMessage(ITagInfo tagInfo, bool clearMessage = false, bool makeReadOnly = false)
        {
            try
            {
                if (_currentTag == null)
                {
                    throw new Exception(Configuration.Messages.NFCErrorMissingTag);
                }

                if (tagInfo == null)
                {
                    throw new Exception(Configuration.Messages.NFCErrorMissingTagInfo);
                }

                var ndef = Ndef.Get(_currentTag);
                if (ndef != null)
                {
                    try
                    {
                        if (!ndef.IsWritable)
                        {
                            throw new Exception(Configuration.Messages.NFCErrorReadOnlyTag);
                        }

                        if (ndef.MaxSize < NFCUtils.GetSize(tagInfo.Records))
                        {
                            throw new Exception(Configuration.Messages.NFCErrorCapacityTag);
                        }

                        ndef.Connect();
                        OnTagConnected?.Invoke(null, EventArgs.Empty);

                        NdefMessage message = null;
                        if (clearMessage)
                        {
                            message = GetEmptyNdefMessage();
                        }
                        else
                        {
                            var records = new List <NdefRecord>();
                            for (var i = 0; i < tagInfo.Records.Length; i++)
                            {
                                var record = tagInfo.Records[i];
                                if (GetAndroidNdefRecord(record) is NdefRecord ndefRecord)
                                {
                                    records.Add(ndefRecord);
                                }
                            }

                            if (records.Any())
                            {
                                message = new NdefMessage(records.ToArray());
                            }
                        }

                        if (message != null)
                        {
                            ndef.WriteNdefMessage(message);

                            if (!clearMessage && makeReadOnly)
                            {
                                if (!MakeReadOnly(ndef))
                                {
                                    Console.WriteLine("Cannot lock tag");
                                }
                            }

                            var nTag = GetTagInfo(_currentTag, ndef.NdefMessage);
                            OnMessagePublished?.Invoke(nTag);
                        }
                        else
                        {
                            throw new Exception(Configuration.Messages.NFCErrorWrite);
                        }
                    }
                    catch (Android.Nfc.TagLostException tlex)
                    {
                        throw new Exception("Tag Lost Error: " + tlex.Message);
                    }
                    catch (Java.IO.IOException ioex)
                    {
                        throw new Exception("Tag IO Error: " + ioex.Message);
                    }
                    catch (Android.Nfc.FormatException fe)
                    {
                        throw new Exception("Tag Format Error: " + fe.Message);
                    }
                    catch (Exception ex)
                    {
                        throw new Exception("Tag Error:" + ex.Message);
                    }
                    finally
                    {
                        if (ndef.IsConnected)
                        {
                            ndef.Close();
                        }

                        _currentTag = null;
                        OnTagDisconnected?.Invoke(null, EventArgs.Empty);
                    }
                }
                else
                {
                    throw new Exception(Configuration.Messages.NFCErrorNotCompliantTag);
                }
            }
            catch (Exception ex)
            {
                StopPublishingAndThrowError(ex.Message);
            }
        }
Esempio n. 3
0
 /// <summary>
 /// Event raised when a tag is connected
 /// </summary>
 /// <param name="sender">Object <see cref="ProximityDevice"/></param>
 void OnDeviceArrived(ProximityDevice sender) => OnTagConnected?.Invoke(null, EventArgs.Empty);
Esempio n. 4
0
        /// <summary>
        /// Write or Clear a NDEF message
        /// </summary>
        /// <param name="tagInfo"><see cref="ITagInfo"/></param>
        /// <param name="clearMessage">Clear Message</param>
        internal void WriteOrClearMessage(ITagInfo tagInfo, bool clearMessage = false)
        {
            if (_currentTag == null)
            {
                throw new Exception("Tag error: No tag to write");
            }

            if (tagInfo == null)
            {
                throw new Exception("TagInfo error: No tag to write");
            }

            var ndef = Ndef.Get(_currentTag);

            if (ndef != null)
            {
                try
                {
                    if (!ndef.IsWritable)
                    {
                        throw new Exception("Tag is not writable");
                    }

                    if (ndef.MaxSize < NFCUtils.GetSize(tagInfo.Records))
                    {
                        throw new Exception("Tag is too small");
                    }

                    ndef.Connect();
                    OnTagConnected?.Invoke(null, EventArgs.Empty);

                    NdefMessage message = null;
                    if (clearMessage)
                    {
                        message = GetEmptyNdefMessage();
                    }
                    else
                    {
                        var records = new List <NdefRecord>();
                        for (var i = 0; i < tagInfo.Records.Length; i++)
                        {
                            var record = tagInfo.Records[i];
                            if (GetAndroidNdefRecord(record) is NdefRecord ndefRecord)
                            {
                                records.Add(ndefRecord);
                            }
                        }

                        if (records.Any())
                        {
                            message = new NdefMessage(records.ToArray());
                        }
                    }

                    if (message != null)
                    {
                        ndef.WriteNdefMessage(message);
                        var nTag = GetTagInfo(_currentTag, ndef.NdefMessage);
                        OnMessagePublished?.Invoke(nTag);
                    }
                    else
                    {
                        throw new Exception("nothing to write on tag");
                    }
                }
                catch (Android.Nfc.TagLostException tlex)
                {
                    throw new Exception("Tag lost error: " + tlex.Message);
                }
                catch (Java.IO.IOException ioex)
                {
                    throw new Exception("Tag IO error: " + ioex.Message);
                }
                catch (Android.Nfc.FormatException fe)
                {
                    throw new Exception("Tag format error: " + fe.Message);
                }
                catch (Exception ex)
                {
                    throw new Exception("Tag other error:" + ex.Message);
                }
                finally
                {
                    if (ndef.IsConnected)
                    {
                        ndef.Close();
                    }

                    _currentTag = null;
                    OnTagDisconnected?.Invoke(null, EventArgs.Empty);
                }
            }
            else
            {
                throw new Exception("Tag Error: NDEF is not supported");
            }
        }