示例#1
0
        /// <summary>
        /// Deletes any details currently stored in the Smart Poster
        /// and re-initializes them by parsing the contents of the payload.
        /// </summary>
        private void ParsePayloadToData(byte[] payload)
        {
            InitializeData();
            if (payload == null || payload.Length == 0)
            {
                return;
            }

            var message = NdefMessage.FromByteArray(payload);

            foreach (NdefRecord record in message)
            {
                var specializedType = record.CheckSpecializedType(false);
                if (specializedType == null)
                {
                    continue;
                }

                if (specializedType == typeof(NdefUriRecord))
                {
                    // URI
                    RecordUri = new NdefUriRecord(record);
                }
                else if (specializedType == typeof(NdefTextRecord))
                {
                    // Title
                    var textRecord = new NdefTextRecord(record);
                    if (Titles == null)
                    {
                        Titles = new List <NdefTextRecord>();
                    }
                    Titles.Add(textRecord);
                }
                else if (specializedType == typeof(NdefSpActRecord))
                {
                    // Action
                    _recordAction = new NdefSpActRecord(record);
                }
                else if (specializedType == typeof(NdefSpSizeRecord))
                {
                    // Size
                    _recordSize = new NdefSpSizeRecord(record);
                }
                else if (specializedType == typeof(NdefSpMimeTypeRecord))
                {
                    // Mime Type
                    _recordMimeType = new NdefSpMimeTypeRecord(record);
                }
                else if (specializedType == typeof(NdefMimeImageRecordBase))
                {
                    // Image
                    _recordImage = new NdefMimeImageRecordBase(record);
                }
                else
                {
                    Debug.WriteLine("Sp: Don't know how to handle this record: " +
                                    BitConverter.ToString(record.Type));
                }
            }
        }
示例#2
0
 /// <summary>
 /// Create a Smart Poster record based on the record passed
 /// through the argument.
 /// </summary>
 /// <remarks>
 /// Internalizes and parses the payload of the original record.
 /// The original record can be a Smart Poster or a URI record.
 /// </remarks>
 /// <param name="other">Record to copy into this smart poster record.</param>
 /// <exception cref="NdefException">Thrown if attempting to create a Smart Poster
 /// based on an incompatible record type.</exception>
 public NdefSpRecord(NdefRecord other)
     : base(other)
 {
     if (TypeNameFormat != TypeNameFormatType.NfcRtd)
     {
         throw new NdefException(NdefExceptionMessages.ExInvalidCopy);
     }
     if (_type.SequenceEqual(SmartPosterType))
     {
         // Other record was a Smart Poster, so parse and internalize the Sp payload
         ParsePayloadToData(_payload);
     }
     else if (_type.SequenceEqual(NdefUriRecord.UriType))
     {
         // Create Smart Poster based on URI record
         RecordUri = new NdefUriRecord(other)
         {
             Id = null
         };
         // Set type of this instance to Smart Poster
         _type = new byte[SmartPosterType.Length];
         Array.Copy(SmartPosterType, _type, SmartPosterType.Length);
     }
     else
     {
         throw new NdefException(NdefExceptionMessages.ExInvalidCopy);
     }
 }
示例#3
0
        /// <summary>
        /// Checks if the record sent via the parameter is indeed a Windows Phone Settings record.
        /// </summary>
        /// <param name="record">Record to check.</param>
        /// <returns>True if the record has the correct type and type name format
        /// to be a WP Settings record + if its URI equals one of the allowed
        /// scheme names. False if it's a different record.</returns>
        public new static bool IsRecordType(NdefRecord record)
        {
            if (!NdefUriRecord.IsRecordType(record))
            {
                return(false);
            }
            var testRecord = new NdefUriRecord(record);

            return(testRecord.Uri != null && SettingsSchemes.Any(curScheme => testRecord.Uri.Equals(curScheme.Value)));
        }
        /// <summary>
        /// Checks if the record sent via the parameter is indeed a NearSpeak record.
        /// Only checks the type and type name format, doesn't analyze if the
        /// payload is valid.
        /// </summary>
        /// <param name="record">Record to check.</param>
        /// <returns>True if the record has the correct type and type name format
        /// to be a NearSpeak record, false if it's a different record.</returns>
        public new static bool IsRecordType(NdefRecord record)
        {
            if (!NdefUriRecord.IsRecordType(record))
            {
                return(false);
            }
            var testRecord = new NdefUriRecord(record);

            if (testRecord.Uri == null || testRecord.Uri.Length < NearSpeakScheme.Length + 7)
            {
                return(false);
            }
            return(testRecord.Uri.StartsWith(NearSpeakScheme));
        }
示例#5
0
 public MessageViewModel(NdefLibrary.Ndef.NdefRecord message)
 {
     Type type = message.CheckSpecializedType(true);
     Type = type.Name;
     Info = "Feel free to add this mapping to the message view model";
     if (type == typeof (NdefLibrary.Ndef.NdefTextRecord))
     {
         var text = new NdefTextRecord(message);
         Info = string.Format("The message on the tag is \"{0}\". The language is \"{1}\"", text.Text,
             text.LanguageCode);
     }
     if (type == typeof(NdefLibrary.Ndef.NdefUriRecord))
     {
         var text = new NdefUriRecord(message);
         Info = string.Format("The URI on the tag is \"{0}\"", text.Uri);
     }
 }
示例#6
0
 private void MessageReceivedHandler(ProximityDevice sender, ProximityMessage message)
 {
     SetStatusOutput("Found proximity card");
     // Convert to NdefMessage from NDEF / NFC Library
     var msgArray = message.Data.ToArray();
     NdefMessage ndefMessage = NdefMessage.FromByteArray(msgArray);
     // Loop over all records contained in the message
     foreach (NdefRecord record in ndefMessage)
     {
         // Check the type of each record 
         if (record.CheckSpecializedType(false) == typeof(NdefUriRecord))
         {
             // Convert and extract URI info
             var uriRecord = new NdefUriRecord(record);
             SetStatusOutput("NDEF URI: " + uriRecord.Uri);
         }
     }
 }
示例#7
0
 /// <summary>
 /// Checks if the record sent via the parameter is indeed a Mailto record.
 /// Checks the type and type name format and if the URI starts with the correct scheme.
 /// </summary>
 /// <param name="record">Record to check.</param>
 /// <returns>True if the record has the correct type, type name format and payload
 /// to be a Mailto record, false if it's a different record.</returns>
 public new static bool IsRecordType(NdefRecord record)
 {
     if (record?.Type == null)
     {
         return(false);
     }
     if (record.TypeNameFormat == TypeNameFormatType.NfcRtd && record.Payload != null)
     {
         if (record.Type.SequenceEqual(NdefUriRecord.UriType))
         {
             var testRecord = new NdefUriRecord(record);
             return(testRecord.Uri.StartsWith(MailtoScheme));
         }
         if (record.Type.SequenceEqual(SmartPosterType))
         {
             var testRecord = new NdefSpRecord(record);
             return(testRecord.Uri.StartsWith(MailtoScheme));
         }
     }
     return(false);
 }
示例#8
0
        static void Main(string[] args)
        {
            // Dynamically construct some more NDEF records
            var spRecord = new NdefSpRecord
            {
                Uri = "http://andijakl.github.io/ndef-nfc/",
                NfcAction = NdefSpActRecord.NfcActionType.DoAction
            };
            spRecord.AddTitle(new NdefTextRecord { LanguageCode = "en", Text = "NFC Library" });
            spRecord.AddTitle(new NdefTextRecord { LanguageCode = "de", Text = "NFC Bibliothek" });
            NfcRecords.Add("SmartPoster", spRecord);

            // Ensure the path exists
            var tagsDirectory = Path.Combine(Environment.CurrentDirectory, FileDirectory);
            Directory.CreateDirectory(tagsDirectory);

            // Write tag contents to files
            foreach (var curNdefRecord in NfcRecords)
            {
                WriteTagFile(tagsDirectory, curNdefRecord.Key, curNdefRecord.Value);
            }

            // Multi-record file
            var record1 = new NdefUriRecord {Uri = "http://www.twitter.com"};
            var record2 = new NdefAndroidAppRecord {PackageName = "com.twitter.android"};
            var twoRecordsMsg = new NdefMessage {record1, record2};
            WriteTagFile(tagsDirectory, "TwoRecords", twoRecordsMsg);

            var record3 = new NdefRecord
            {
                TypeNameFormat = NdefRecord.TypeNameFormatType.ExternalRtd,
                Type = Encoding.UTF8.GetBytes("custom.com:myapp")
            };
            var threeRecordsMsg = new NdefMessage { record1, record3, record2 };
            WriteTagFile(tagsDirectory, "ThreeRecords", threeRecordsMsg);

            // Success message on output
            Console.WriteLine("Generated {0} tag files in {1}.", NfcRecords.Count, tagsDirectory);
            Debug.WriteLine("Generated {0} tag files in {1}.", NfcRecords.Count, tagsDirectory);
        }
示例#9
0
 /// <summary>
 /// Set the URI based on another URI record instead of specifying the URI itself.
 /// </summary>
 /// <param name="newUri"></param>
 public void SetUri(NdefUriRecord newUri)
 {
     RecordUri = new NdefUriRecord(newUri);
     AssemblePayload();
 }
示例#10
0
        /// <summary>
        /// Deletes any details currently stored in the Smart Poster 
        /// and re-initializes them by parsing the contents of the payload.
        /// </summary>
        private void ParsePayloadToData(byte[] payload)
        {
            InitializeData();
            if (payload == null || payload.Length == 0)
                return;

            var message = NdefMessage.FromByteArray(payload);

            foreach (NdefRecord record in message)
            {
                var specializedType = record.CheckSpecializedType(false);
                if (specializedType == null) continue;

                if (specializedType == typeof(NdefUriRecord))
                {
                    // URI
                    RecordUri = new NdefUriRecord(record);
                }
                else if (specializedType == typeof (NdefTextRecord))
                {
                    // Title
                    var textRecord = new NdefTextRecord(record);
                    if (Titles == null) Titles = new List<NdefTextRecord>();
                    Titles.Add(textRecord);
                }
                else if (specializedType == typeof (NdefSpActRecord))
                {
                    // Action
                    _recordAction = new NdefSpActRecord(record);
                }
                else if (specializedType == typeof (NdefSpSizeRecord))
                {
                    // Size
                    _recordSize = new NdefSpSizeRecord(record);
                }
                else if (specializedType == typeof (NdefSpMimeTypeRecord))
                {
                    // Mime Type
                    _recordMimeType = new NdefSpMimeTypeRecord(record);
                }
                else if (specializedType == typeof(NdefMimeImageRecordBase))
                {
                    // Image
                    _recordImage = new NdefMimeImageRecordBase(record);
                }
                else
                {
                    Debug.WriteLine("Sp: Don't know how to handle this record: " +
                                    BitConverter.ToString(record.Type));
                }
            }
        }
示例#11
0
        private ObservableCollection<string> readNDEFMEssage(NdefMessage message)
        {

            ObservableCollection<string> collection = new ObservableCollection<string>();
            foreach (NdefRecord record in message)
            {
                // Go through each record, check if it's a Smart Poster
                if (record.CheckSpecializedType(false) == typeof(NdefSpRecord))
                {
                    // Convert and extract Smart Poster info
                    var spRecord = new NdefSpRecord(record);
                    collection.Add("URI: " + spRecord.Uri);
                    collection.Add("Titles: " + spRecord.TitleCount());
                    collection.Add("1. Title: " + spRecord.Titles[0].Text);
                    collection.Add("Action set: " + spRecord.ActionInUse());
                }

                if (record.CheckSpecializedType(false) == typeof(NdefUriRecord))
                {
                    // Convert and extract Smart Poster info
                    var spRecord = new NdefUriRecord(record);
                    collection.Add("Text: " + spRecord.Uri);
                }
            }
            return collection;
        }
示例#12
0
 /// <summary>
 /// Checks if the record sent via the parameter is indeed an Sms record.
 /// Checks the type and type name format and if the URI starts with the correct scheme.
 /// </summary>
 /// <param name="record">Record to check.</param>
 /// <returns>True if the record has the correct type, type name format and payload
 /// to be an Sms record, false if it's a different record.</returns>
 public new static bool IsRecordType(NdefRecord record)
 {
     if (record?.Type == null) return false;
     if (record.TypeNameFormat == TypeNameFormatType.NfcRtd && record.Payload != null)
     {
         if (record.Type.SequenceEqual(NdefUriRecord.UriType))
         {
             var testRecord = new NdefUriRecord(record);
             return testRecord.Uri.StartsWith(SmsScheme, StringComparison.OrdinalIgnoreCase);
         }
         if (record.Type.SequenceEqual(SmartPosterType))
         {
             var testRecord = new NdefSpRecord(record);
             return testRecord.Uri.StartsWith(SmsScheme, StringComparison.OrdinalIgnoreCase);
         }
     }
     return false;
 }
示例#13
0
        private string HandleNDEFRecord(NdefRecord record)
        {
            string tag = string.Empty;
            var specializedType = record.CheckSpecializedType(false);

            if (specializedType == typeof(NdefTextRecord))
            {
                var oRecord = new NdefTextRecord(record);
                try
                {
                    tag = oRecord.Text;
                    this.NotifyUser(tag, NotifyType.PublishMessage);
                }
                catch { }
            }
            else if (specializedType == typeof(NdefSpRecord))
            {
                var oRecord = new NdefSpRecord(record);
                //this.NotifyUser(NotifyType.PeerMessage, "Not Supportint Type, Value (Uri:" + oRecord.Uri + ")");
            }
            else if (specializedType == typeof(NdefUriRecord))
            {
                var oRecord = new NdefUriRecord(record);
                //this.NotifyUser(NotifyType.PeerMessage, "Not Supportint Type, Value (Uri:" + oRecord.Uri + ")");
            }
            else if (specializedType == typeof(NdefTelRecord))
            {
                var oRecord = new NdefTelRecord(record);
                //this.NotifyUser(NotifyType.PeerMessage, "Not Supportint Type, Value (Tel:" + oRecord.TelNumber + ")");
            }
            else if (specializedType == typeof(NdefMailtoRecord))
            {
                var oRecord = new NdefMailtoRecord(record);
                //this.NotifyUser(NotifyType.PeerMessage, "Not Supportint Type, Value (Subject:" + oRecord.Subject + ")");
            }
            else if (specializedType == typeof(NdefAndroidAppRecord))
            {
                var oRecord = new NdefAndroidAppRecord(record);
                //this.NotifyUser(NotifyType.PeerMessage, "Not Supportint Type, Value (Package:" + oRecord.PackageName + ")");
            }
            else if (specializedType == typeof(NdefLaunchAppRecord))
            {
                var oRecord = new NdefLaunchAppRecord(record);
                //this.NotifyUser(NotifyType.PeerMessage, "Not Supportint Type, Value (Platform:" + oRecord.PlatformIds + ")");
            }
            else if (specializedType == typeof(NdefSmsRecord))
            {
                var oRecord = new NdefSmsRecord(record);
                //this.NotifyUser(NotifyType.PeerMessage, "Not Supportint Type, Value (Sms:" + oRecord.SmsBody + ")");
            }
            else if (specializedType == typeof(NdefSmartUriRecord))
            {
                var oRecord = new NdefSmartUriRecord(record);
                //this.NotifyUser(NotifyType.PeerMessage, "Not Supportint Type, Value (Uri:" + oRecord.Uri + ")");
            }
            else
            {
                //this.NotifyUser(NotifyType.PeerMessage, "Not Supporting Type");
            }

            return tag;
        }
示例#14
0
文件: CardReader.cs 项目: edjo23/shop
        private bool IsShopCard(byte[] buffer)
        {
            // NDEF - byte 0 should be 0x03, byte 1 should be length of remaining bytes.
            if (buffer.Length < 2 || buffer[0] != 0x03 || buffer[1] != buffer.Length - 2)
                return false;

            try
            {
                var msg = NdefMessage.FromByteArray(buffer.Skip(2).ToArray());

                if (msg.Count > 0 && msg.First().CheckSpecializedType(false) == typeof(NdefUriRecord))
                {
                    var record = new NdefUriRecord(msg.First());

                    return record.Uri.StartsWith(CardUri);
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex);
            }

            return false;
        }
示例#15
0
        private async Task ParseTagContents(NdefMessage ndefMessage, StringBuilder tagContents)
        {
            // Loop over all records contained in the NDEF message
            foreach (NdefRecord record in ndefMessage)
            {
                // --------------------------------------------------------------------------
                // Print generic information about the record
                if (record.Id != null && record.Id.Length > 0)
                {
                    // Record ID (if present)
                    tagContents.AppendFormat("Id: {0}\n", Encoding.UTF8.GetString(record.Id, 0, record.Id.Length));
                }
                // Record type name, as human readable string
                tagContents.AppendFormat("Type name: {0}\n", ConvertTypeNameFormatToString(record.TypeNameFormat));
                // Record type
                if (record.Type != null && record.Type.Length > 0)
                {
                    tagContents.AppendFormat("Record type: {0}\n",
                        Encoding.UTF8.GetString(record.Type, 0, record.Type.Length));
                }

                // --------------------------------------------------------------------------
                // Check the type of each record
                // Using 'true' as parameter for CheckSpecializedType() also checks for sub-types of records,
                // e.g., it will return the SMS record type if a URI record starts with "sms:"
                // If using 'false', a URI record will always be returned as Uri record and its contents won't be further analyzed
                // Currently recognized sub-types are: SMS, Mailto, Tel, Nokia Accessories, NearSpeak, WpSettings
                var specializedType = record.CheckSpecializedType(true);

                if (specializedType == typeof(NdefMailtoRecord))
                {
                    // --------------------------------------------------------------------------
                    // Convert and extract Mailto record info
                    var mailtoRecord = new NdefMailtoRecord(record);
                    tagContents.Append("-> Mailto record\n");
                    tagContents.AppendFormat("Address: {0}\n", mailtoRecord.Address);
                    tagContents.AppendFormat("Subject: {0}\n", mailtoRecord.Subject);
                    tagContents.AppendFormat("Body: {0}\n", mailtoRecord.Body);
                }
                else if (specializedType == typeof(NdefUriRecord))
                {
                    // --------------------------------------------------------------------------
                    // Convert and extract URI record info
                    var uriRecord = new NdefUriRecord(record);
                    tagContents.Append("-> URI record\n");
                    tagContents.AppendFormat("URI: {0}\n", uriRecord.Uri);
                }
                else if (specializedType == typeof(NdefSpRecord))
                {
                    // --------------------------------------------------------------------------
                    // Convert and extract Smart Poster info
                    var spRecord = new NdefSpRecord(record);
                    tagContents.Append("-> Smart Poster record\n");
                    tagContents.AppendFormat("URI: {0}", spRecord.Uri);
                    tagContents.AppendFormat("Titles: {0}", spRecord.TitleCount());
                    if (spRecord.TitleCount() > 1)
                        tagContents.AppendFormat("1. Title: {0}", spRecord.Titles[0].Text);
                    tagContents.AppendFormat("Action set: {0}", spRecord.ActionInUse());
                    // You can also check the action (if in use by the record), 
                    // mime type and size of the linked content.
                }
                //else if (specializedType == typeof(NdefVcardRecordBase))
                //{
                //    // --------------------------------------------------------------------------
                //    // Convert and extract business card info
                //    var vcardRecord = new NdefVcardRecord(record);
                //    tagContents.Append("-> Business Card record" + Environment.NewLine);
                //    var contact = vcardRecord.ContactData;

                //    // Contact has phone or email info set? Use contact manager to show the contact card
                //    await _dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                //    {
                //        if (contact.Emails.Any() || contact.Phones.Any())
                //        {
                //            var rect = GetElementRect(StatusOutput);
                //            ContactManager.ShowContactCard(contact, rect, Placement.Below);
                //        }
                //        else
                //        {
                //            // No phone or email set - contact manager would not show the contact card.
                //            // -> parse manually
                //            tagContents.AppendFormat("Name: {0}\n", contact.DisplayName);
                //            tagContents.Append("[not parsing other values in the demo app]");
                //        }
                //    });
                //}
                //else if (specializedType == typeof(NdefIcalendarRecordBase))
                //{
                //    // --------------------------------------------------------------------------
                //    // Convert and extract iCalendar info
                //    var icalendarRecord = new NdefIcalendarRecord(record);
                //    tagContents.Append("-> iCalendar record" + Environment.NewLine);
                //    var ap = icalendarRecord.AppointmentData;
                //    if (!String.IsNullOrEmpty(ap.Subject))
                //        tagContents.AppendFormat("Subject: {0}\n", ap.Subject);
                //    if (!String.IsNullOrEmpty(ap.Details))
                //        tagContents.AppendFormat("Details: {0}\n", ap.Details);
                //    if (!String.IsNullOrEmpty(ap.Organizer.Address))
                //        tagContents.AppendFormat("Organizer Address: {0}\n", ap.Organizer.Address);
                //    if (!String.IsNullOrEmpty(ap.Location))
                //        tagContents.AppendFormat("Location: {0}\n", ap.Location);
                //    tagContents.AppendFormat("Start time: {0}\n", ap.StartTime);
                //    tagContents.AppendFormat("Duration: {0}\n", ap.Duration);
                //    tagContents.AppendFormat("AllDay? {0}\n", ap.AllDay ? "yes" : "no");
                //    if (ap.Reminder != null)
                //        tagContents.AppendFormat("Reminder: {0}\n", ap.Reminder);
                //}
                else if (specializedType == typeof(NdefLaunchAppRecord))
                {
                    // --------------------------------------------------------------------------
                    // Convert and extract LaunchApp record info
                    var launchAppRecord = new NdefLaunchAppRecord(record);
                    tagContents.Append("-> LaunchApp record" + Environment.NewLine);
                    if (!string.IsNullOrEmpty(launchAppRecord.Arguments))
                        tagContents.AppendFormat("Arguments: {0}\n", launchAppRecord.Arguments);
                    if (launchAppRecord.PlatformIds != null)
                    {
                        foreach (var platformIdTuple in launchAppRecord.PlatformIds)
                        {
                            if (platformIdTuple.Key != null)
                                tagContents.AppendFormat("Platform: {0}\n", platformIdTuple.Key);
                            if (platformIdTuple.Value != null)
                                tagContents.AppendFormat("App ID: {0}\n", platformIdTuple.Value);
                        }
                    }
                }
                //else if (specializedType == typeof(NdefMimeImageRecordBase))
                //{
                //    // --------------------------------------------------------------------------
                //    // Convert and extract Image record info
                //    var imgRecord = new NdefMimeImageRecord(record);
                //    tagContents.Append("-> MIME / Image record" + Environment.NewLine);
                //    _dispatcher.RunAsync(CoreDispatcherPriority.Normal, async () => SetStatusImage(await imgRecord.GetImageAsBitmap()));

                //}
                else
                {
                    // Other type, not handled by this demo
                    tagContents.Append("NDEF record not parsed by this demo app" + Environment.NewLine);
                }
            }
        }
 /// <summary>
 /// Checks if the record sent via the parameter is indeed a NearSpeak record.
 /// Only checks the type and type name format, doesn't analyze if the
 /// payload is valid.
 /// </summary>
 /// <param name="record">Record to check.</param>
 /// <returns>True if the record has the correct type and type name format
 /// to be a NearSpeak record, false if it's a different record.</returns>
 public static new bool IsRecordType(NdefRecord record)
 {
     if (!NdefUriRecord.IsRecordType(record)) return false;
     var testRecord = new NdefUriRecord(record);
     if (testRecord.Uri == null || testRecord.Uri.Length < NearSpeakScheme.Length + 3)
         return false;
     return testRecord.Uri.StartsWith(NearSpeakScheme);
 }
示例#17
0
        // ------------------------------------------
        //  PRIVATE READER
        // ------------------------------------------
        private void MessageReceivedHandler(ProximityDevice sender, ProximityMessage message)
        {
            // Parse raw byte array to NDEF message
              var rawMsg = message.Data.ToArray();
              var ndefMessage = NdefMessage.FromByteArray(rawMsg);

              // Loop over all records contained in the NDEF message
              foreach (NdefRecord record in ndefMessage) {
            Console.WriteLine("Record type: " + Encoding.UTF8.GetString(record.Type, 0, record.Type.Length));

            // Check the type of each record - handling a Smart Poster in this example
            var specializedType = record.CheckSpecializedType(true);

            // Convert and extract Smart Poster info
            if (specializedType == typeof(NdefSpRecord)) {
              var spRecord = new NdefSpRecord(record);
              String msg = "URI: " + spRecord.Uri + "\n"
                         + "Titles: " + spRecord.TitleCount() + "\n"
                         + "1. Title: " + spRecord.Titles[0].Text + "\n"
                         + "Action set: " + spRecord.ActionInUse();
              NFCManager.getInstance().Alert(msg);
            }

            // Convert and extract URI record info
            else if (specializedType == typeof(NdefUriRecord)) {
              var uriRecord = new NdefUriRecord(record);
              String msg = "URI: " + uriRecord.Uri + "\n";
              NFCManager.getInstance().Alert(msg);
              NFCManager.getInstance().SendRequest(uriRecord.Uri);
            }

            // Convert and extract Mailto record info
            else if (specializedType == typeof(NdefMailtoRecord)) {
              var mailtoRecord = new NdefMailtoRecord(record);
              String msg = "Address: " + mailtoRecord.Address + "\n"
                     + "Subject: " + mailtoRecord.Subject + "\n"
                     + "Body: " + mailtoRecord.Body;
              NFCManager.getInstance().Alert(msg);
            }
              }
        }
示例#18
0
 // ------------------------------------------
 //  PUBLIC
 // http://software.intel.com/en-us/articles/using-winrt-apis-from-desktop-applications
 // ------------------------------------------
 public void WriteURIRecord(String uri)
 {
     var uriRecord = new NdefUriRecord { Uri = uri };
       PublishRecord(uriRecord, true);
 }
示例#19
0
        /// <summary>
        /// Create a Smart Poster record based on the record passed
        /// through the argument.
        /// </summary>
        /// <remarks>
        /// Internalizes and parses the payload of the original record.
        /// The original record can be a Smart Poster or a URI record.
        /// </remarks>
        /// <param name="other">Record to copy into this smart poster record.</param>
        /// <exception cref="NdefException">Thrown if attempting to create a Smart Poster
        /// based on an incompatible record type.</exception>
        public NdefSpRecord(NdefRecord other)
            : base(other)
        {
            if (TypeNameFormat != TypeNameFormatType.NfcRtd)
                throw new NdefException(NdefExceptionMessages.ExInvalidCopy);
            if (_type.SequenceEqual(SmartPosterType))
            {
                // Other record was a Smart Poster, so parse and internalize the Sp payload
                ParsePayloadToData(_payload);
            }
            else if (_type.SequenceEqual(NdefUriRecord.UriType))
            {
                // Create Smart Poster based on URI record
                RecordUri = new NdefUriRecord(other) {Id = null};
                // Set type of this instance to Smart Poster
                _type = new byte[SmartPosterType.Length];
                Array.Copy(SmartPosterType, _type, SmartPosterType.Length);
            }
            else
            {
                throw new NdefException(NdefExceptionMessages.ExInvalidCopy);
            }

        }
示例#20
0
 /// <summary>
 /// Checks the type name format and type of this record and returns
 /// the appropriate specialized class, if one is available and known
 /// for this record type.
 /// </summary>
 /// <param name="checkForSubtypes">If set to true, also checks for
 /// subtypes of the URL / SmartPoster record where the library offers
 /// a convenient handling class - e.g. for SMS or Mailto records,
 /// which are actually URL schemes.</param>
 /// <returns>Type name of the specialized class that can understand
 /// and manipulate the payload through convenience methods.</returns>
 public Type CheckSpecializedType(bool checkForSubtypes)
 {
     // Note: can't check for specialized types like the geo record
     // or the SMS record yet, as these are just convenience classes
     // for creating URI / Smart Poster records.
     if (checkForSubtypes)
     {
         // Need to check specialized URI / Sp records before checking for base types.
         if (NdefSmsRecord.IsRecordType(this))
         {
             return(typeof(NdefSmsRecord));
         }
         if (NdefMailtoRecord.IsRecordType(this))
         {
             return(typeof(NdefMailtoRecord));
         }
         if (NdefTelRecord.IsRecordType(this))
         {
             return(typeof(NdefTelRecord));
         }
         if (NdefWindowsSettingsRecord.IsRecordType(this))
         {
             return(typeof(NdefWindowsSettingsRecord));
         }
     }
     // Unique / base record types
     if (NdefUriRecord.IsRecordType(this))
     {
         return(typeof(NdefUriRecord));
     }
     if (NdefSpRecord.IsRecordType(this))
     {
         return(typeof(NdefSpRecord));
     }
     if (NdefTextRecord.IsRecordType(this))
     {
         return(typeof(NdefTextRecord));
     }
     if (NdefSpActRecord.IsRecordType(this))
     {
         return(typeof(NdefSpActRecord));
     }
     if (NdefSpSizeRecord.IsRecordType(this))
     {
         return(typeof(NdefSpSizeRecord));
     }
     if (NdefSpMimeTypeRecord.IsRecordType(this))
     {
         return(typeof(NdefSpMimeTypeRecord));
     }
     if (NdefLaunchAppRecord.IsRecordType(this))
     {
         return(typeof(NdefLaunchAppRecord));
     }
     if (NdefAndroidAppRecord.IsRecordType(this))
     {
         return(typeof(NdefAndroidAppRecord));
     }
     if (NdefVcardRecordBase.IsRecordType(this))
     {
         return(typeof(NdefVcardRecordBase));
     }
     if (NdefIcalendarRecordBase.IsRecordType(this))
     {
         return(typeof(NdefIcalendarRecordBase));
     }
     if (NdefBtSecureSimplePairingRecord.IsRecordType(this))
     {
         return(typeof(NdefBtSecureSimplePairingRecord));
     }
     if (NdefHandoverSelectRecord.IsRecordType(this))
     {
         return(typeof(NdefHandoverSelectRecord));
     }
     if (NdefHandoverErrorRecord.IsRecordType(this))
     {
         return(typeof(NdefHandoverErrorRecord));
     }
     if (NdefHandoverAlternativeCarrierRecord.IsRecordType(this))
     {
         return(typeof(NdefHandoverAlternativeCarrierRecord));
     }
     if (NdefMimeImageRecordBase.IsRecordType(this))
     {
         return(typeof(NdefMimeImageRecordBase));
     }
     return(typeof(NdefRecord));
 }
示例#21
0
        private NdefRecord GetPublishingMessage(Type type, string data)
        {
            NdefRecord retValue = null;

            if (type == typeof(NdefTextRecord))
            {
                var oRecord = new NdefTextRecord();
                oRecord.Text = data;
                retValue = oRecord;
            }
            else if (type == typeof(NdefSpRecord))
            {
                var oRecord = new NdefSpRecord();
                //this.NotifyUser(NotifyType.PeerMessage, "Not Supportint Type, Value (Uri:" + oRecord.Uri + ")");
            }
            else if (type == typeof(NdefUriRecord))
            {
                var oRecord = new NdefUriRecord();
                //this.NotifyUser(NotifyType.PeerMessage, "Not Supportint Type, Value (Uri:" + oRecord.Uri + ")");
            }
            else if (type == typeof(NdefTelRecord))
            {
                var oRecord = new NdefTelRecord();
                //this.NotifyUser(NotifyType.PeerMessage, "Not Supportint Type, Value (Tel:" + oRecord.TelNumber + ")");
            }
            else if (type == typeof(NdefMailtoRecord))
            {
                var oRecord = new NdefMailtoRecord();
                //this.NotifyUser(NotifyType.PeerMessage, "Not Supportint Type, Value (Subject:" + oRecord.Subject + ")");
            }
            else if (type == typeof(NdefAndroidAppRecord))
            {
                var oRecord = new NdefAndroidAppRecord();
                //this.NotifyUser(NotifyType.PeerMessage, "Not Supportint Type, Value (Package:" + oRecord.PackageName + ")");
            }
            else if (type == typeof(NdefLaunchAppRecord))
            {
                var oRecord = new NdefLaunchAppRecord();         
     
                string praid = CoreApplication.Id; // The Application Id value from your package.appxmanifest.
                string appName = Package.Current.Id.FamilyName + "!" + praid;

                NdefLaunchAppRecord launch = new NdefLaunchAppRecord();
                launch.AddPlatformAppId("Windows", appName);
                launch.Arguments = data;
                retValue = oRecord;
            }
            else if (type == typeof(NdefSmsRecord))
            {
                var oRecord = new NdefSmsRecord();
                //this.NotifyUser(NotifyType.PeerMessage, "Not Supportint Type, Value (Sms:" + oRecord.SmsBody + ")");
            }
            else if (type == typeof(NdefSmartUriRecord))
            {
                var oRecord = new NdefSmartUriRecord();
                //this.NotifyUser(NotifyType.PeerMessage, "Not Supportint Type, Value (Uri:" + oRecord.Uri + ")");
            }
            else
            {
                //
            }

            return retValue;
        }
示例#22
0
 private void BtnPublishUri_Click(object sender, RoutedEventArgs e)
 {
     // Create a URI record
     var record = new NdefUriRecord { Uri = "http://www.nfcinteractor.com/" };
     // Publish the record using the proximity device
     PublishRecord(record, false);
 }
示例#23
0
 /// <summary>
 /// Set the URI based on another URI record instead of specifying the URI itself.
 /// </summary>
 /// <param name="newUri"></param>
 public void SetUri(NdefUriRecord newUri)
 {
     RecordUri = new NdefUriRecord(newUri);
     AssemblePayload();
 }
示例#24
0
 /// <summary>
 /// Checks the type name format and type of this record and returns
 /// the appropriate specialized class, if one is available and known
 /// for this record type.
 /// </summary>
 /// <returns>Type name of the specialized class that can understand
 /// and manipulate the payload through convenience methods.</returns>
 public Type CheckSpecializedType(bool checkForSubtypes)
 {
     // Note: can't check for specialized types like the geo record
     // or the SMS record yet, as these are just convenience classes
     // for creating URI / Smart Poster records.
     if (checkForSubtypes)
     {
         // Need to check specialized URI / Sp records before checking for base types.
         if (NdefSmsRecord.IsRecordType(this))
         {
             return(typeof(NdefSmsRecord));
         }
         if (NdefMailtoRecord.IsRecordType(this))
         {
             return(typeof(NdefMailtoRecord));
         }
         if (NdefTelRecord.IsRecordType(this))
         {
             return(typeof(NdefTelRecord));
         }
         if (NdefNokiaAccessoriesRecord.IsRecordType(this))
         {
             return(typeof(NdefNokiaAccessoriesRecord));
         }
         if (NdefNearSpeakRecord.IsRecordType(this))
         {
             return(typeof(NdefNearSpeakRecord));
         }
         if (NdefWpSettingsRecord.IsRecordType(this))
         {
             return(typeof(NdefWpSettingsRecord));
         }
     }
     // Unique / base record types
     if (NdefUriRecord.IsRecordType(this))
     {
         return(typeof(NdefUriRecord));
     }
     if (NdefSpRecord.IsRecordType(this))
     {
         return(typeof(NdefSpRecord));
     }
     if (NdefTextRecord.IsRecordType(this))
     {
         return(typeof(NdefTextRecord));
     }
     if (NdefSpActRecord.IsRecordType(this))
     {
         return(typeof(NdefSpActRecord));
     }
     if (NdefSpSizeRecord.IsRecordType(this))
     {
         return(typeof(NdefSpSizeRecord));
     }
     if (NdefSpMimeTypeRecord.IsRecordType(this))
     {
         return(typeof(NdefSpMimeTypeRecord));
     }
     if (NdefLaunchAppRecord.IsRecordType(this))
     {
         return(typeof(NdefLaunchAppRecord));
     }
     if (NdefAndroidAppRecord.IsRecordType(this))
     {
         return(typeof(NdefAndroidAppRecord));
     }
     return(typeof(NdefRecord));
 }
示例#25
0
        private async void MessageReceivedHandler(ProximityDevice sender, ProximityMessage message)
        {
            // Get the raw NDEF message data as byte array
            var rawMsg = message.Data.ToArray();
            // Let the NDEF library parse the NDEF message out of the raw byte array
            var ndefMessage = NdefMessage.FromByteArray(rawMsg);

            // Analysis result
            var tagContents = new StringBuilder();

            // Loop over all records contained in the NDEF message
            foreach (NdefRecord record in ndefMessage)
            {
                // --------------------------------------------------------------------------
                // Print generic information about the record
                if (record.Id != null && record.Id.Length > 0)
                {
                    // Record ID (if present)
                    tagContents.AppendFormat("Id: {0}\n", Encoding.UTF8.GetString(record.Id, 0, record.Id.Length));
                }
                // Record type name, as human readable string
                tagContents.AppendFormat("Type name: {0}\n", ConvertTypeNameFormatToString(record.TypeNameFormat));
                // Record type
                if (record.Type != null && record.Type.Length > 0)
                {
                    tagContents.AppendFormat("Record type: {0}\n",
                                             Encoding.UTF8.GetString(record.Type, 0, record.Type.Length));
                }

                // --------------------------------------------------------------------------
                // Check the type of each record
                // Using 'true' as parameter for CheckSpecializedType() also checks for sub-types of records,
                // e.g., it will return the SMS record type if a URI record starts with "sms:"
                // If using 'false', a URI record will always be returned as Uri record and its contents won't be further analyzed
                // Currently recognized sub-types are: SMS, Mailto, Tel, Nokia Accessories, NearSpeak, WpSettings
                var specializedType = record.CheckSpecializedType(true);

                if (specializedType == typeof(NdefMailtoRecord))
                {
                    // --------------------------------------------------------------------------
                    // Convert and extract Mailto record info
                    var mailtoRecord = new NdefMailtoRecord(record);
                    tagContents.Append("-> Mailto record\n");
                    tagContents.AppendFormat("Address: {0}\n", mailtoRecord.Address);
                    tagContents.AppendFormat("Subject: {0}\n", mailtoRecord.Subject);
                    tagContents.AppendFormat("Body: {0}\n", mailtoRecord.Body);
                }
                else if (specializedType == typeof(NdefUriRecord))
                {
                    // --------------------------------------------------------------------------
                    // Convert and extract URI record info
                    var uriRecord = new NdefUriRecord(record);
                    tagContents.Append("-> URI record\n");
                    tagContents.AppendFormat("URI: {0}\n", uriRecord.Uri);
                }
                else if (specializedType == typeof (NdefLaunchAppRecord))
                {
                    // --------------------------------------------------------------------------
                    // Convert and extract LaunchApp record info
                    var launchAppRecord = new NdefLaunchAppRecord(record);
                    tagContents.Append("-> LaunchApp record" + Environment.NewLine);
                    if (!string.IsNullOrEmpty(launchAppRecord.Arguments))
                        tagContents.AppendFormat("Arguments: {0}\n", launchAppRecord.Arguments);
                    if (launchAppRecord.PlatformIds != null)
                    {
                        foreach (var platformIdTuple in launchAppRecord.PlatformIds)
                        {
                            if (platformIdTuple.Key != null)
                                tagContents.AppendFormat("Platform: {0}\n", platformIdTuple.Key);
                            if (platformIdTuple.Value != null)
                                tagContents.AppendFormat("App ID: {0}\n", platformIdTuple.Value);
                        }
                    }
                }
                else if (specializedType == typeof(NdefVcardRecordBase))
                {
                    // --------------------------------------------------------------------------
                    // Convert and extract business card info
                    var vcardRecord = await NdefVcardRecord.CreateFromGenericBaseRecord(record);
                    tagContents.Append("-> Business Card record" + Environment.NewLine);
                    tagContents.AppendFormat("vCard Version: {0}" + Environment.NewLine,
                                             vcardRecord.VCardFormatToWrite == VCardFormat.Version2_1 ? "2.1" : "3.0");
                    var contact = vcardRecord.ContactData;
                    var contactInfo = await contact.GetPropertiesAsync();
                    foreach (var curProperty in contactInfo.OrderBy(i => i.Key))
                    {
                        tagContents.Append(String.Format("{0}: {1}" + Environment.NewLine, curProperty.Key, curProperty.Value));
                    }
                }
                else
                {
                    // Other type, not handled by this demo
                    tagContents.Append("NDEF record not parsed by this demo app" + Environment.NewLine);
                }
            }
            // Update status text for UI
            SetStatusOutput(string.Format(AppResources.StatusTagParsed, tagContents));
        }
示例#26
0
        private void MessageReceivedHandler(ProximityDevice sender, ProximityMessage message)
        {
            // Get the raw NDEF message data as byte array
            var rawMsg = message.Data.ToArray();
            // Let the NDEF library parse the NDEF message out of the raw byte array
            var ndefMessage = NdefMessage.FromByteArray(rawMsg);

            // Analysis result
            var tagContents = new StringBuilder();
           
            // Loop over all records contained in the NDEF message
            foreach (NdefRecord record in ndefMessage)
            {
                // --------------------------------------------------------------------------
                // Print generic information about the record
                if (record.Id != null && record.Id.Length > 0)
                {
                    // Record ID (if present)
                    tagContents.AppendFormat("Id: {0}\n", Encoding.UTF8.GetString(record.Id, 0, record.Id.Length));
                }
                // Record type name, as human readable string
                tagContents.AppendFormat("Type name: {0}\n", ConvertTypeNameFormatToString(record.TypeNameFormat));
                // Record type
                if (record.Type != null && record.Type.Length > 0)
                {
                    tagContents.AppendFormat("Record type: {0}\n",
                                             Encoding.UTF8.GetString(record.Type, 0, record.Type.Length));
                }

                // --------------------------------------------------------------------------
                // Check the type of each record
                // Using 'true' as parameter for CheckSpecializedType() also checks for sub-types of records,
                // e.g., it will return the SMS record type if a URI record starts with "sms:"
                // If using 'false', a URI record will always be returned as Uri record and its contents won't be further analyzed
                // Currently recognized sub-types are: SMS, Mailto, Tel, Nokia Accessories, NearSpeak, WpSettings
                var specializedType = record.CheckSpecializedType(true);

                if (specializedType == typeof(NdefMailtoRecord))
                {
                    // --------------------------------------------------------------------------
                    // Convert and extract Mailto record info
                    var mailtoRecord = new NdefMailtoRecord(record);
                    tagContents.Append("-> Mailto record\n");
                    tagContents.AppendFormat("Address: {0}\n", mailtoRecord.Address);
                    tagContents.AppendFormat("Subject: {0}\n", mailtoRecord.Subject);
                    tagContents.AppendFormat("Body: {0}\n", mailtoRecord.Body);
                }
                else if (specializedType == typeof(NdefUriRecord))
                {
                    // --------------------------------------------------------------------------
                    // Convert and extract URI record info
                    var uriRecord = new NdefUriRecord(record);
                    tagContents.Append("-> URI record\n");
                    tagContents.AppendFormat("URI: {0}\n", uriRecord.Uri);
                }
                else if (specializedType == typeof(NdefSpRecord))
                {
                    // --------------------------------------------------------------------------
                    // Convert and extract Smart Poster info
                    var spRecord = new NdefSpRecord(record);
                    tagContents.Append("-> Smart Poster record\n");
                    tagContents.AppendFormat("URI: {0}", spRecord.Uri);
                    tagContents.AppendFormat("Titles: {0}", spRecord.TitleCount());
                    if (spRecord.TitleCount() > 1)
                        tagContents.AppendFormat("1. Title: {0}", spRecord.Titles[0].Text);
                    tagContents.AppendFormat("Action set: {0}", spRecord.ActionInUse());
                    // You can also check the action (if in use by the record), 
                    // mime type and size of the linked content.
                }
                else if (specializedType == typeof (NdefLaunchAppRecord))
                {
                    
                    // --------------------------------------------------------------------------
                    // Convert and extract LaunchApp record info
                    var launchAppRecord = new NdefLaunchAppRecord(record);
                    tagContents.Append("-> LaunchApp record" + Environment.NewLine);
                   
                    if (!string.IsNullOrEmpty(launchAppRecord.Arguments))
                     
                        tagContents.AppendFormat("Arguments: {0}\n", launchAppRecord.Arguments);
                    if (launchAppRecord.PlatformIds != null)
                    {
                        foreach (var platformIdTuple in launchAppRecord.PlatformIds)
                        {
                            if (platformIdTuple.Key != null)
                                tagContents.AppendFormat("Platform: {0}\n", platformIdTuple.Key);
                            if (platformIdTuple.Value != null)
                                tagContents.AppendFormat("App ID: {0}\n", platformIdTuple.Value);
                        }
                    }
                }
                else
                {
                    // Other type, not handled by this demo
                    tagContents.Append("NDEF record not parsed by this demo app" + Environment.NewLine);
                }
            }
            // Update status text for UI
            SetStatusOutput(string.Format(AppResources.StatusTagParsed, tagContents));
        }
 /// <summary>
 /// Checks if the record sent via the parameter is indeed a Windows Phone Settings record.
 /// </summary>
 /// <param name="record">Record to check.</param>
 /// <returns>True if the record has the correct type and type name format
 /// to be a WP Settings record + if its URI equals one of the allowed
 /// scheme names. False if it's a different record.</returns>
 public static new bool IsRecordType(NdefRecord record)
 {
     if (!NdefUriRecord.IsRecordType(record)) return false;
     var testRecord = new NdefUriRecord(record);
     return testRecord.Uri != null && SettingsSchemes.Any(curScheme => testRecord.Uri.Equals(curScheme.Value));
 }
示例#28
0
 /// <summary>
 /// Checks if the record sent via the parameter is indeed a Telephone record.
 /// Checks the type and type name format and if the URI starts with the correct scheme.
 /// </summary>
 /// <param name="record">Record to check.</param>
 /// <returns>True if the record has the correct type, type name format and payload
 /// to be a Telephone record, false if it's a different record.</returns>
 public static new bool IsRecordType(NdefRecord record)
 {
     if (record.Type == null) return false;
     if (record.TypeNameFormat == TypeNameFormatType.NfcRtd && record.Payload != null)
     {
         if (record.Type.SequenceEqual(NdefUriRecord.UriType))
         {
             var testRecord = new NdefUriRecord(record);
             return testRecord.Uri.StartsWith(TelScheme);
         }
         if (record.Type.SequenceEqual(SmartPosterType))
         {
             var testRecord = new NdefSpRecord(record);
             return testRecord.Uri.StartsWith(TelScheme);
         }
     }
     return false;
 }
示例#29
0
 /// <summary>
 /// (Re)set all the stored sub records of the Smart Poster.
 /// </summary>
 private void InitializeData()
 {
     RecordUri = null;
     Titles = null;
     _recordAction = null;
     _recordSize = null;
     _recordMimeType = null;
 }