diff --git a/CampusAppWP8/CampusAppWP8/Pages/Dev/NFC.xaml.cs b/CampusAppWP8/CampusAppWP8/Pages/Dev/NFC.xaml.cs index d5a2fb86..b5052c7a 100644 --- a/CampusAppWP8/CampusAppWP8/Pages/Dev/NFC.xaml.cs +++ b/CampusAppWP8/CampusAppWP8/Pages/Dev/NFC.xaml.cs @@ -10,6 +10,7 @@ using Microsoft.Phone.Shell; using Windows.Networking.Proximity; using System.Runtime.InteropServices.WindowsRuntime; using CampusAppWP8.Utility.NDEF; +using Windows.Storage.Streams; namespace CampusAppWP8.Pages.Dev @@ -60,15 +61,9 @@ namespace CampusAppWP8.Pages.Dev this.device.StopSubscribingForMessage(message.SubscriptionId); - /* NDEFRecord ndef = new NDEFRecord(); - ndef.TNF = NDEFRecord.TNFVAL.URI; - ndef.MB = NDEFRecord.NDEFFlags.MBSET; - ndef.ME = NDEFRecord.NDEFFlags.MESET; - ndef.SR = NDEFRecord.NDEFFlags.SRSET; - ndef.Type = NDEFRecord.TYPEVAL.TEXT; - ndef.ToByteArray(); - ndef.Payload = "test";*/ NDEFMessage ndef = new NDEFMessage("test", NDEFMessage.TYPEVAL.TEXT); + byte[] ndefB = ndef.ToByteArray(); + IBuffer buffer = ndefB.AsBuffer(); device.PublishBinaryMessage("NDEF:WriteTag", ndef.ToByteArray().AsBuffer(), publishHandler); } @@ -85,6 +80,9 @@ namespace CampusAppWP8.Pages.Dev var NDefMessage = message.Data; byte[] testa = NDefMessage.ToArray(); + + NDEFMessage ndef = new NDEFMessage(testa); + string c = ndef.GetContent(); // NDEFRecord test = new NDEFRecord(testa); } } diff --git a/CampusAppWP8/CampusAppWP8/Utility/NDEF/NDEFMessage.cs b/CampusAppWP8/CampusAppWP8/Utility/NDEF/NDEFMessage.cs index 9dac3ec4..f4038e46 100644 --- a/CampusAppWP8/CampusAppWP8/Utility/NDEF/NDEFMessage.cs +++ b/CampusAppWP8/CampusAppWP8/Utility/NDEF/NDEFMessage.cs @@ -1,31 +1,58 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - +//----------------------------------------------------------------------- +// +// Company copyright tag. +// +// stubbfel +// 21.08.2013 +//---------------------------------------------------------------------- namespace CampusAppWP8.Utility.NDEF { + using System.Collections.Generic; + using System.IO; + + /// Ndef message. + /// Stubbfel, 21.08.2013. public class NDEFMessage { + #region Members + + /// The records. private List records; - public NDEFMessage(string content, TYPEVAL type) + #endregion + + #region constructors + + /// Initializes a new instance of the NDEFMessage class. + /// Stubbfel, 21.08.2013. + /// The content. + /// The type. + /// (Optional) the tnf. + public NDEFMessage(string content, TYPEVAL type, NDEFRecord.TNFVAL tnf = NDEFRecord.TNFVAL.WKT) { this.records = new List(); - float recordsCount = content.Length / NDEFRecord.MaxRecordPayLoad; + float recordsCount = (float)content.Length / NDEFRecord.MaxRecordPayLoad; NDEFRecord tmpRecord = null; + string praefix = NDEFMessage.GetPraefix(type); for (int i = 0; recordsCount > 0; i++) { tmpRecord = new NDEFShortRecord(); tmpRecord.Type = type; - tmpRecord.Payload = content.Substring(i * 255, 255); + tmpRecord.TNF = tnf; + tmpRecord.PayloadPraefix = praefix; + int recordsize = 255; + if (content.Length < (i + 1) * recordsize) + { + recordsize = content.Length - (i * recordsize); + } + + tmpRecord.Payload = content.Substring(i * 255, recordsize); if (i == 0) { tmpRecord.MB = NDEFRecord.NDEFFlags.MBSET; } + this.records.Add(tmpRecord); recordsCount--; } @@ -33,6 +60,9 @@ namespace CampusAppWP8.Utility.NDEF this.records[this.records.IndexOf(tmpRecord)].ME = NDEFRecord.NDEFFlags.MESET; } + /// Initializes a new instance of the NDEFMessage class. + /// Stubbfel, 21.08.2013. + /// The array. public NDEFMessage(byte[] array) { this.records = new List(); @@ -40,11 +70,15 @@ namespace CampusAppWP8.Utility.NDEF NDEFRecord tmpRecord = null; for (int i = 0; i < array.Length; i += tmpRecord.RecordSize) { - tmpRecord = new NDEFShortRecord(array,i); + tmpRecord = new NDEFShortRecord(array, i); + this.records.Add(tmpRecord); } } + #endregion + + #region enum /// Values that represent TYPEVAL. /// Stubbfel, 21.08.2013. @@ -60,6 +94,35 @@ namespace CampusAppWP8.Utility.NDEF TEXT = 0x54, } + #endregion + + #region Methods + /// Gets a praefix. + /// Stubbfel, 21.08.2013. + /// The type. + /// The praefix. + public static string GetPraefix(TYPEVAL type) + { + string praefix = string.Empty; + + switch (type) + { + case TYPEVAL.TEXT: + praefix = "\x02" + "de"; + break; + case TYPEVAL.URL: + praefix = "\x01"; + break; + default: + break; + } + + return praefix; + } + + /// Gets the content. + /// Stubbfel, 21.08.2013. + /// The content. public string GetContent() { string result = string.Empty; @@ -72,15 +135,20 @@ namespace CampusAppWP8.Utility.NDEF return result; } + /// Converts this object to a byte array. + /// Stubbfel, 21.08.2013. + /// This object as a byte[]. public byte[] ToByteArray() { - MemoryStream ms = new MemoryStream(); foreach (NDEFRecord record in this.records) { ms.Write(record.ToByteArray(), 0, record.RecordSize); } + return ms.ToArray(); } + + #endregion } } diff --git a/CampusAppWP8/CampusAppWP8/Utility/NDEF/NDEFRecord.cs b/CampusAppWP8/CampusAppWP8/Utility/NDEF/NDEFRecord.cs index 50f9b46e..8d3a0c7e 100644 --- a/CampusAppWP8/CampusAppWP8/Utility/NDEF/NDEFRecord.cs +++ b/CampusAppWP8/CampusAppWP8/Utility/NDEF/NDEFRecord.cs @@ -20,10 +20,7 @@ namespace CampusAppWP8.Utility.NDEF public const int MaxRecordPayLoad = 255; /// Size of the type. - protected const byte TypeSize = 0x01; - - /// Size of the type. - protected int HeaderSize; + protected const byte TypeSize = 0x01; #endregion @@ -37,6 +34,7 @@ namespace CampusAppWP8.Utility.NDEF /// Initializes a new instance of the NDEFRecord class. /// Stubbfel, 21.08.2013. /// The array. + /// (Optional) zero-based index of the. public NDEFRecord(byte[] array, int index = 0) { this.FormatFlags = array[index]; @@ -132,14 +130,15 @@ namespace CampusAppWP8.Utility.NDEF /// The type. public NDEFMessage.TYPEVAL Type { get; set; } - /// Gets the format flags. + /// Gets or sets the format flags. /// The format flags. public byte FormatFlags { get { return (byte)((byte)this.TNF | ((byte)this.MB) | ((byte)this.ME) | ((byte)this.CF) | ((byte)this.SR) | ((byte)this.IL)); - } + } + protected set { this.TNF = (TNFVAL)(value & (byte)NDEFFlags.TNFSET); @@ -149,7 +148,6 @@ namespace CampusAppWP8.Utility.NDEF this.SR = (NDEFFlags)(value & (byte)NDEFFlags.SRSET); this.IL = (NDEFFlags)(value & (byte)NDEFFlags.ILSET); } - } /// Gets or sets the payload. @@ -162,10 +160,18 @@ namespace CampusAppWP8.Utility.NDEF { get { - return this.HeaderSize + this.Payload.Length; + return this.HeaderSize + this.Payload.Length + this.PayloadPraefix.Length; } } + /// Gets or sets the payload praefix. + /// The payload praefix. + public string PayloadPraefix { get; set; } + + /// Gets or sets the size of the header. + /// The size of the header. + protected int HeaderSize { get; set; } + #endregion #region Methods @@ -178,3 +184,5 @@ namespace CampusAppWP8.Utility.NDEF #endregion } } + +// End of Utility\NDEF\NDEFRecord.cs diff --git a/CampusAppWP8/CampusAppWP8/Utility/NDEF/NDEFShortRecord.cs b/CampusAppWP8/CampusAppWP8/Utility/NDEF/NDEFShortRecord.cs index 02796f30..6f7bd4f8 100644 --- a/CampusAppWP8/CampusAppWP8/Utility/NDEF/NDEFShortRecord.cs +++ b/CampusAppWP8/CampusAppWP8/Utility/NDEF/NDEFShortRecord.cs @@ -26,13 +26,15 @@ namespace CampusAppWP8.Utility.NDEF /// Initializes a new instance of the NDEFShortRecord class. /// Stubbfel, 21.08.2013. /// The array. + /// (Optional) zero-based index of the. public NDEFShortRecord(byte[] array, int index = 0) : base(array) { this.HeaderSize = 4; this.Type = (NDEFMessage.TYPEVAL)array[index + 3]; - int payLoadSize = array[index + 2]; - this.Payload = Encoding.UTF8.GetString(array, index + this.HeaderSize, payLoadSize); + this.PayloadPraefix = NDEFMessage.GetPraefix(this.Type); + int payLoadSize = array[index + 2] - this.PayloadPraefix.Length; + this.Payload = Encoding.UTF8.GetString(array, index + this.HeaderSize + this.PayloadPraefix.Length, payLoadSize); } /// Converts this NDEFShortRecord to a byte array. @@ -40,12 +42,12 @@ namespace CampusAppWP8.Utility.NDEF /// This object as a byte[]. public override byte[] ToByteArray() { - byte[] payloadAr = Encoding.UTF8.GetBytes(Payload); + byte[] payloadAr = Encoding.UTF8.GetBytes(this.PayloadPraefix + Payload); byte[] array = new byte[payloadAr.Length + this.HeaderSize]; array[0] = this.FormatFlags; array[1] = NDEFRecord.TypeSize; - array[2] = (byte)(Payload.Length); + array[2] = (byte)(Payload.Length + this.PayloadPraefix.Length); array[3] = (byte)this.Type; int i = this.HeaderSize; @@ -54,6 +56,7 @@ namespace CampusAppWP8.Utility.NDEF array[i] = b; i++; } + return array; } } diff --git a/CampusAppWP8/CampusAppWP8/Utility/Utilities.cs b/CampusAppWP8/CampusAppWP8/Utility/Utilities.cs index bacf89c4..246dd11b 100644 --- a/CampusAppWP8/CampusAppWP8/Utility/Utilities.cs +++ b/CampusAppWP8/CampusAppWP8/Utility/Utilities.cs @@ -224,6 +224,7 @@ namespace CampusAppWP8.Utility // set to 0 point, if access to device is not allow geoposition = new GeoPosition(); geoposition.Location = new GeoCoordinate(0, 0); + geoposition.Timestamp = DateTime.Now; } watcher.Stop();