Exemplo n.º 1
0
 private void MessageReceived(ProximityDevice sender, ProximityMessage message)
 {
     Dispatcher.BeginInvoke(() => {
         var buf = DataReader.FromBuffer(message.Data);
         List <NdefRecord> recordList = new List <NdefRecord>();
         NdefRecordUtility.ReadNdefRecord(buf, recordList);
         NdefRecord firstRecord = recordList[0];
         byte[] payload         = firstRecord.Payload;
         GetBadgeData(payload);
     });
 }
        /// <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);
        }