Beispiel #1
0
        public async Task <NdefMessage> ReadNdefAsync()
        {
            try
            {
                await _lockSemaphore.WaitAsync();

                if (!_isSessionEnabled)
                {
                    throw new Exception("NFC is not enabled");
                }

                var tcs = new TaskCompletionSource <NdefMessage>();

                _tag.ReadNdef((iosNdefMessage, error) =>
                {
                    if (error != null)
                    {
                        throw new Exception(error.Description);
                    }

                    var ndefMessage = new NdefMessage();
                    ndefMessage.AddRange(iosNdefMessage.Records.Select(iosRecord => new NdefRecord
                    {
                        Id             = iosRecord.Identifier.ToArray(),
                        Type           = iosRecord.Type.ToArray(),
                        TypeNameFormat = (NdefRecord.TypeNameFormatType)iosRecord.TypeNameFormat,
                        Payload        = iosRecord.Payload.ToArray()
                    }));

                    tcs.SetResult(ndefMessage);
                });

                return(await tcs.Task);
            }
            finally
            {
                _lockSemaphore.Release();
            }
        }
Beispiel #2
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;
        }
        /// <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;
        }