Beispiel #1
0
        public void WriteTag(NdefLibrary.Ndef.NdefMessage message)
        {
            int messageSize = 0;

            foreach (NdefRecord record in message)
            {
                messageSize += record.Payload.Length;
                if (!record.CheckIfValid())
                {
                    throw new Exception("A record on NDEFMessage is not valid");
                }
            }

            if (!isTagPresent)
            {
                throw new Exception("No Tag present or Tag is incompatible ");
            }

            if (!nfcTag.IsWriteable)
            {
                throw new Exception("Tag is write locked ");
            }

            if (nfcTag.MaxSize < messageSize)
            {
                throw new Exception("Tag is too small for this message");
            }

            RaiseTagConnected(nfcTag);

            nfcDevice.PublishBinaryMessage("NDEF:WriteTag", message.ToByteArray().AsBuffer(), writerHandler);
        }
Beispiel #2
0
        /// <summary>
        /// Create the payload based on the data stored in the properties. Usually called
        /// automatically when changing properties, but you might need to call this manually
        /// if you modify the list of alternative carrier records directly without using
        /// accessor method provided by this class.
        /// </summary>
        /// <exception cref="NdefException">Thrown if unable to assemble the payload.
        /// The exception message contains further details about the issue.</exception>
        public void AssemblePayload()
        {
            if (_handoverVersion == null)
            {
                throw new NdefException(NdefExceptionMessages.ExHandoverInvalidVersion);
            }

            // Convert child records to message
            var childMsg = new NdefMessage();

            if (_handoverAlternativeCarrierRecords != null)
            {
                childMsg.AddRange(_handoverAlternativeCarrierRecords);
            }
            if (_handoverErrorRecord != null)
            {
                childMsg.Add(_handoverErrorRecord);
            }

            var childBytes = childMsg.ToByteArray();

            var newPayload = new byte[childBytes.Length + 1];

            // Frist byte: handover version
            newPayload[0] = _handoverVersion.Version;

            // Rest of the payload: child message containing Alternative Carrier records + Error record
            Array.Copy(childBytes, 0, newPayload, 1, childBytes.Length);

            _payload = newPayload;
        }
Beispiel #3
0
        /// <summary>
        /// Reverse function to parseRecords() - this one takes
        /// the information stored in the individual record instances and assembles
        /// it into the payload of the base class.
        /// </summary>
        /// <remarks>
        /// As the URI is mandatory, the payload will not be assembled
        /// if no URI is defined.
        /// </remarks>
        /// <returns>Whether assembling the payload was successful.</returns>
        private bool AssemblePayload()
        {
            // Uri is mandatory - don't assemble the payload if it's not set
            if (RecordUri == null)
            {
                return(false);
            }

            // URI (mandatory)
            var message = new NdefMessage {
                RecordUri
            };

            // Title(s) (optional)
            if (Titles != null && Titles.Count > 0)
            {
                message.AddRange(Titles);
            }

            // Action (optional)
            if (ActionInUse())
            {
                message.Add(_recordAction);
            }

            // Size (optional)
            if (SizeInUse())
            {
                message.Add(_recordSize);
            }

            // Mime Type (optional)
            if (MimeTypeInUse())
            {
                message.Add(_recordMimeType);
            }

            // Image (optional)
            if (ImageInUse())
            {
                message.Add(_recordImage);
            }

            SetPayloadAndParse(message.ToByteArray(), false);

            return(true);
        }
Beispiel #4
0
        /// <summary>
        /// Convert all the NDEF records currently stored in the NDEF message to a byte
        /// array suitable for writing to a tag or sending to another device.
        /// </summary>
        /// <returns>The NDEF record(s) converted to an NDEF message.</returns>
        public byte[] ToByteArray()
        {
            // Empty message: single empty record
            if (Count == 0)
            {
                var msg = new NdefMessage {
                    new NdefRecord()
                };
                return(msg.ToByteArray());
            }

            var m = new MemoryStream();

            for (int i = 0; i < Count; i++)
            {
                var record = this[i];

                var flags = (byte)record.TypeNameFormat;

                // Message begin / end flags. If there is only one record in the message,
                // both flags are set.
                if (i == 0)
                {
                    flags |= 0x80;      // MB (message begin = first record in the message)
                }
                if (i == Count - 1)
                {
                    flags |= 0x40;      // ME (message end = last record in the message)
                }
                // cf (chunked records) not supported yet

                // SR (Short Record)?
                if (record.Payload == null || record.Payload.Length < 255)
                {
                    flags |= 0x10;
                }

                // ID present?
                if (record.Id != null && record.Id.Length > 0)
                {
                    flags |= 0x08;
                }

                m.WriteByte(flags);

                // Type length
                if (record.Type != null)
                {
                    m.WriteByte((byte)record.Type.Length);
                }
                else
                {
                    m.WriteByte(0);
                }

                // Payload length 1 byte (SR) or 4 bytes
                if (record.Payload == null)
                {
                    m.WriteByte(0);
                }
                else
                {
                    if ((flags & 0x10) != 0)
                    {
                        // SR
                        m.WriteByte((byte)record.Payload.Length);
                    }
                    else
                    {
                        // No SR (Short Record)
                        var payloadLength = (uint)record.Payload.Length;
                        m.WriteByte((byte)(payloadLength >> 24));
                        m.WriteByte((byte)(payloadLength >> 16));
                        m.WriteByte((byte)(payloadLength >> 8));
                        m.WriteByte((byte)(payloadLength & 0x000000ff));
                    }
                }

                // ID length
                if (record.Id != null && (flags & 0x08) != 0)
                {
                    m.WriteByte((byte)record.Id.Length);
                }

                // Type length
                if (record.Type != null && record.Type.Length > 0)
                {
                    m.Write(record.Type, 0, record.Type.Length);
                }

                // ID data
                if (record.Id != null && record.Id.Length > 0)
                {
                    m.Write(record.Id, 0, record.Id.Length);
                }

                // Payload data
                if (record.Payload != null && record.Payload.Length > 0)
                {
                    m.Write(record.Payload, 0, record.Payload.Length);
                }
            }

            return(m.ToArray());
        }