/// <summary>
        /// Extracts the data from the given record and places it into the
        /// given text RTD.
        /// </summary>
        /// <param name="record">The record containing the data.</param>
        /// <param name="text">Where the data is placed.</param>
        public static void ReadTextRtd(NdefRecord record, NdefTextRtd text)
        {
            byte[] buf = record.Payload;

            if (IsBitSet(buf[0], 6))
            {
                return;
            }

            var langLen = (byte)(buf[0] & 0x3f);
            var langBuf = new byte[langLen];
            System.Buffer.BlockCopy(buf, 1, langBuf, 0, langLen);
            text.Language=System.Text.Encoding.UTF8.GetString(langBuf, 0, langBuf.Length);
            var textLen = buf.Length - 1 - langLen;

            if (textLen <= 0)
            {
                return;
            }

            var textBuf = new byte[textLen];
            System.Buffer.BlockCopy(buf, 1 + langLen, textBuf, 0, textLen);

            if (IsBitSet(buf[0], 7))
            {
                text.Encoding = "UTF-16";
                text.Text = System.Text.Encoding.Unicode.GetString(textBuf, 0, textBuf.Length);
            }
            else
            {
                text.Encoding = "UTF-8";
                text.Text = System.Text.Encoding.UTF8.GetString(textBuf, 0, textBuf.Length);
            }
        }
        /// <summary>
        /// Extracts the data from the given record and places it into the
        /// given text RTD.
        /// </summary>
        /// <param name="record">The record containing the data.</param>
        /// <param name="text">Where the data is placed.</param>
        static public void ReadTextRtd(NdefRecord record, NdefTextRtd text)
        {
            byte[] buf = record.Payload;

            if (IsBitSet(buf[0], 6))
            {
                return;
            }

            var langLen = (byte)(buf[0] & 0x3f);
            var langBuf = new byte[langLen];

            System.Buffer.BlockCopy(buf, 1, langBuf, 0, langLen);
            text.Language = System.Text.Encoding.UTF8.GetString(langBuf, 0, langBuf.Length);
            var textLen = buf.Length - 1 - langLen;

            if (textLen <= 0)
            {
                return;
            }

            var textBuf = new byte[textLen];

            System.Buffer.BlockCopy(buf, 1 + langLen, textBuf, 0, textLen);

            if (IsBitSet(buf[0], 7))
            {
                text.Encoding = "UTF-16";
                text.Text     = System.Text.Encoding.Unicode.GetString(textBuf, 0, textBuf.Length);
            }
            else
            {
                text.Encoding = "UTF-8";
                text.Text     = System.Text.Encoding.UTF8.GetString(textBuf, 0, textBuf.Length);
            }
        }
        /// <summary>
        /// Parses the details from the given message. The output is a string
        /// that can be appended into the log.
        /// </summary>
        /// <param name="message">The message to parse.</param>
        /// <returns>The parsed details as a string.</returns>
        private string ParseNDEF(ProximityMessage message)
        {
            var output = "";

            using (var buf = DataReader.FromBuffer(message.Data)) {
                NdefRecordUtility.ReadNdefRecord(buf,recordList);

                for (int i = 0, recordNumber = 0, spRecordNumber = 0; i < recordList.Count; i++)
                {
                    NdefRecord record = recordList.ElementAt(i);

                    if (!record.IsSpRecord )
                    {
                        if (System.Text.Encoding.UTF8.GetString(record.Type, 0, record.TypeLength) == "Sp")
                        {
                            output = output + "\n --End of Record No." + recordNumber; spRecordNumber = 0;
                            continue;
                        }
                        else
                        {
                            recordNumber++;
                            output = output + "\n --Record No." + recordNumber;
                        }
                    }
                    else
                    {
                        if (spRecordNumber == 0)
                        {
                            recordNumber++;
                            output = output + "\n --Record No." + recordNumber;
                        }

                        spRecordNumber++;
                        output = output + "\n Sp sub-record No." + spRecordNumber;
                    }

                    output = output + "\n MB:" + ((record.Mb) ? "1;" : "0;");
                    output = output + " ME:" + ((record.Me) ? "1;" : "0;");
                    output = output + " CF:" + ((record.Cf) ? "1;" : "0;");
                    output = output + " SR:" + ((record.Sr) ? "1;" : "0;");
                    output = output + " IL:" + ((record.Il) ? "1;" : "0;");

                    string typeName = NdefRecordUtility.GetTypeNameFormat(record);

                    if (record.TypeLength > 0)
                    {
                        output = output + "\n Type: " + typeName + ":"
                            + System.Text.Encoding.UTF8.GetString(record.Type, 0, record.TypeLength);
                    }

                    if ((record.Il) && (record.IdLength > 0))
                    {
                        output = output + "\n Id:"
                            + System.Text.Encoding.UTF8.GetString(record.Id, 0, record.IdLength);
                    }

                    if ((record.PayloadLength > 0) && (record.Payload != null))
                    {
                        if ((record.Tnf == 0x01)
                            && (System.Text.Encoding.UTF8.GetString(record.Type, 0, record.TypeLength) == "U"))
                        {
                            NdefUriRtd uri = new NdefUriRtd();
                            NdefRecordUtility.ReadUriRtd(record, uri);
                            output = output + "\n Uri: " + uri.GetFullUri();
                        }
                        else if ((record.Tnf == 0x01)
                            && (System.Text.Encoding.UTF8.GetString(record.Type, 0, record.TypeLength) == "T"))
                        {
                            NdefTextRtd text = new NdefTextRtd();
                            NdefRecordUtility.ReadTextRtd(record, text);
                            output = output + "\n Language: " + text.Language;
                            output = output + "\n Encoding: " + text.Encoding;
                            output = output + "\n Text: " + text.Text;
                        }
                        else
                        {
                            if (record.Tnf==0x01)
                            {
                                output = output + "\n Payload:"
                                    + System.Text.Encoding.UTF8.GetString(record.Payload, 0, record.Payload.Length);
                            }
                        }
                    }

                    if (!record.IsSpRecord)
                    {
                        output = output + "\n --End of Record No." + recordNumber;
                    }
                }
            }

            return output;
        }
        /// <summary>
        /// Parses the details from the given message. The output is a string
        /// that can be appended into the log.
        /// </summary>
        /// <param name="message">The message to parse.</param>
        /// <returns>The parsed details as a string.</returns>
        private string ParseNDEF(ProximityMessage message)
        {
            var output = "";

            using (var buf = DataReader.FromBuffer(message.Data)) {
                NdefRecordUtility.ReadNdefRecord(buf, recordList);

                for (int i = 0, recordNumber = 0, spRecordNumber = 0; i < recordList.Count; i++)
                {
                    NdefRecord record = recordList.ElementAt(i);

                    if (!record.IsSpRecord)
                    {
                        if (System.Text.Encoding.UTF8.GetString(record.Type, 0, record.TypeLength) == "Sp")
                        {
                            output = output + "\n --End of Record No." + recordNumber; spRecordNumber = 0;
                            continue;
                        }
                        else
                        {
                            recordNumber++;
                            output = output + "\n --Record No." + recordNumber;
                        }
                    }
                    else
                    {
                        if (spRecordNumber == 0)
                        {
                            recordNumber++;
                            output = output + "\n --Record No." + recordNumber;
                        }

                        spRecordNumber++;
                        output = output + "\n Sp sub-record No." + spRecordNumber;
                    }

                    output = output + "\n MB:" + ((record.Mb) ? "1;" : "0;");
                    output = output + " ME:" + ((record.Me) ? "1;" : "0;");
                    output = output + " CF:" + ((record.Cf) ? "1;" : "0;");
                    output = output + " SR:" + ((record.Sr) ? "1;" : "0;");
                    output = output + " IL:" + ((record.Il) ? "1;" : "0;");

                    string typeName = NdefRecordUtility.GetTypeNameFormat(record);

                    if (record.TypeLength > 0)
                    {
                        output = output + "\n Type: " + typeName + ":"
                                 + System.Text.Encoding.UTF8.GetString(record.Type, 0, record.TypeLength);
                    }

                    if ((record.Il) && (record.IdLength > 0))
                    {
                        output = output + "\n Id:"
                                 + System.Text.Encoding.UTF8.GetString(record.Id, 0, record.IdLength);
                    }

                    if ((record.PayloadLength > 0) && (record.Payload != null))
                    {
                        if ((record.Tnf == 0x01) &&
                            (System.Text.Encoding.UTF8.GetString(record.Type, 0, record.TypeLength) == "U"))
                        {
                            NdefUriRtd uri = new NdefUriRtd();
                            NdefRecordUtility.ReadUriRtd(record, uri);
                            output = output + "\n Uri: " + uri.GetFullUri();
                        }
                        else if ((record.Tnf == 0x01) &&
                                 (System.Text.Encoding.UTF8.GetString(record.Type, 0, record.TypeLength) == "T"))
                        {
                            NdefTextRtd text = new NdefTextRtd();
                            NdefRecordUtility.ReadTextRtd(record, text);
                            output = output + "\n Language: " + text.Language;
                            output = output + "\n Encoding: " + text.Encoding;
                            output = output + "\n Text: " + text.Text;
                        }
                        else
                        {
                            if (record.Tnf == 0x01)
                            {
                                output = output + "\n Payload:"
                                         + System.Text.Encoding.UTF8.GetString(record.Payload, 0, record.Payload.Length);
                            }
                        }
                    }

                    if (!record.IsSpRecord)
                    {
                        output = output + "\n --End of Record No." + recordNumber;
                    }
                }
            }

            return(output);
        }