From ebdefaa5e615ac621c1181d375ffa804e4a04fc5 Mon Sep 17 00:00:00 2001 From: stubbfel Date: Tue, 20 Aug 2013 16:25:47 +0200 Subject: [PATCH 1/7] add ndefdraft --- CampusAppWP8/CampusAppWP8/CampusAppWP8.csproj | 12 ++- .../Model/Utility/NDEF/NDEFRecord.cs | 92 +++++++++++++++++++ CampusAppWP8/CampusAppWP8/Pages/Dev/NFC.xaml | 35 +++++++ .../CampusAppWP8/Pages/Dev/NFC.xaml.cs | 90 ++++++++++++++++++ .../CampusAppWP8/Pages/StartPage.xaml | 1 + .../CampusAppWP8/Pages/StartPage.xaml.cs | 17 ++++ 6 files changed, 246 insertions(+), 1 deletion(-) create mode 100644 CampusAppWP8/CampusAppWP8/Model/Utility/NDEF/NDEFRecord.cs create mode 100644 CampusAppWP8/CampusAppWP8/Pages/Dev/NFC.xaml create mode 100644 CampusAppWP8/CampusAppWP8/Pages/Dev/NFC.xaml.cs diff --git a/CampusAppWP8/CampusAppWP8/CampusAppWP8.csproj b/CampusAppWP8/CampusAppWP8/CampusAppWP8.csproj index b4446480..a2653e83 100644 --- a/CampusAppWP8/CampusAppWP8/CampusAppWP8.csproj +++ b/CampusAppWP8/CampusAppWP8/CampusAppWP8.csproj @@ -116,9 +116,13 @@ + + + NFC.xaml + AppSettingPage.xaml @@ -279,6 +283,10 @@ Designer MSBuild:Compile + + Designer + MSBuild:Compile + Designer MSBuild:Compile @@ -428,7 +436,9 @@ - + + Designer + diff --git a/CampusAppWP8/CampusAppWP8/Model/Utility/NDEF/NDEFRecord.cs b/CampusAppWP8/CampusAppWP8/Model/Utility/NDEF/NDEFRecord.cs new file mode 100644 index 00000000..a9bfbbff --- /dev/null +++ b/CampusAppWP8/CampusAppWP8/Model/Utility/NDEF/NDEFRecord.cs @@ -0,0 +1,92 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CampusAppWP8.Model.Utility.NDEF +{ + public class NDEFRecord + { + private const byte typeSize = 0x01; + public enum NDEFFlags + { + UNSET = 0x00, + MBSET = 0x80, + MESET = 0x40, + CFSET = 0x20, + SRSET = 0x10, + ILSET = 0x08 + } + + public enum TNFVAL + { + EMPTY = 0x00, + WKT = 0x01, + MEDIATYPE = 0x02, + URI = 0x03, + NFCE = 0x04, + UNKOWN = 0x05, + UNCHANGED = 0x06, + RESERVED = 0x07 + } + + public enum TYPEVAL + { + EMPTY = 0x00, + URL = 0x55, + TEXT = 0x54, + } + + public NDEFRecord() + { + + } + public NDEFRecord(byte[] array) + { + this.Payload = Encoding.UTF8.GetString(array, 4, array.Length - 4); + } + public NDEFFlags MB { get; set; } + public NDEFFlags ME { get; set; } + public NDEFFlags CF { get; set; } + public NDEFFlags SR { get; set; } + public NDEFFlags IL { get; set; } + public TNFVAL TNF { get; set; } + public TYPEVAL Type { get; set; } + public byte FormatFlags + { + get + { + return (byte)((byte)this.TNF | ((byte)this.MB) | ((byte)this.ME) | ((byte)this.CF) | ((byte)this.SR) | ((byte)this.IL)); + } + } + + + public byte Record = 0xD1; + + public string Payload = "nokia.com"; + + public byte[] toByteArray() + { + MemoryStream m = new MemoryStream(); + byte[] payloadAr = Encoding.UTF8.GetBytes(Payload); + byte[] array = new byte[payloadAr.Length + 4]; + + array[0] = this.FormatFlags; + array[1] = NDEFRecord.typeSize; + array[2] = (byte)(Payload.Length); + array[3] = (byte)this.Type; + + int i = 4; + foreach (byte b in payloadAr) + { + array[i] = b; + i++; + } + return array; + } + + + } +} diff --git a/CampusAppWP8/CampusAppWP8/Pages/Dev/NFC.xaml b/CampusAppWP8/CampusAppWP8/Pages/Dev/NFC.xaml new file mode 100644 index 00000000..cac77add --- /dev/null +++ b/CampusAppWP8/CampusAppWP8/Pages/Dev/NFC.xaml @@ -0,0 +1,35 @@ + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/CampusAppWP8/CampusAppWP8/Pages/Dev/NFC.xaml.cs b/CampusAppWP8/CampusAppWP8/Pages/Dev/NFC.xaml.cs new file mode 100644 index 00000000..6549e8a8 --- /dev/null +++ b/CampusAppWP8/CampusAppWP8/Pages/Dev/NFC.xaml.cs @@ -0,0 +1,90 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net; +using System.Windows; +using System.Windows.Controls; +using System.Windows.Navigation; +using Microsoft.Phone.Controls; +using Microsoft.Phone.Shell; +using Windows.Networking.Proximity; +using System.Runtime.InteropServices.WindowsRuntime; +using CampusAppWP8.Model.Utility.NDEF; + + +namespace CampusAppWP8.Pages.Dev +{ + public partial class NFC : PhoneApplicationPage + { + + private readonly ProximityDevice device = ProximityDevice.GetDefault(); + private long lastMsgId; + public NFC() + { + InitializeComponent(); + } + + /// + /// Override the OnNavigatedTo method + /// + /// Arguments of navigation + protected override void OnNavigatedTo(NavigationEventArgs e) + { + base.OnNavigatedTo(e); + + if (this.device != null) + { + MessageBox.Show("NFC present"); + } + else + { + MessageBox.Show("Your phone has no NFC or NFC is disabled"); + } + this.lastMsgId = this.device.SubscribeForMessage("WriteableTag", WriteableTagDetected); + } + + protected override void OnNavigatedFrom(NavigationEventArgs e) + { + base.OnNavigatedTo(e); + + this.device.StopSubscribingForMessage(this.lastMsgId); + } + + + private void WriteableTagDetected(ProximityDevice sender, ProximityMessage message) + { + Deployment.Current.Dispatcher.BeginInvoke(() => + { + MessageBox.Show("message"); + }); + + 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"; + device.PublishBinaryMessage("NDEF:WriteTag", ndef.toByteArray().AsBuffer(), publishHandler); + } + + private void publishHandler(ProximityDevice sender, long messageId) + { + device.StopPublishingMessage(messageId); + + device.SubscribeForMessage("NDEF", NDEFHandler); + } + + + private void NDEFHandler(ProximityDevice sender, ProximityMessage message) + { + + var NDefMessage = message.Data; + byte[] testa = NDefMessage.ToArray(); + NDEFRecord test = new NDEFRecord(testa); + } + } +} \ No newline at end of file diff --git a/CampusAppWP8/CampusAppWP8/Pages/StartPage.xaml b/CampusAppWP8/CampusAppWP8/Pages/StartPage.xaml index ca2c2d80..ab7c5c3c 100644 --- a/CampusAppWP8/CampusAppWP8/Pages/StartPage.xaml +++ b/CampusAppWP8/CampusAppWP8/Pages/StartPage.xaml @@ -142,6 +142,7 @@ + diff --git a/CampusAppWP8/CampusAppWP8/Pages/StartPage.xaml.cs b/CampusAppWP8/CampusAppWP8/Pages/StartPage.xaml.cs index 6c3d516d..5562226d 100644 --- a/CampusAppWP8/CampusAppWP8/Pages/StartPage.xaml.cs +++ b/CampusAppWP8/CampusAppWP8/Pages/StartPage.xaml.cs @@ -31,6 +31,7 @@ namespace CampusAppWP8.Pages this.InitializeComponent(); ApplicationBarMenuItem menuItem1 = ApplicationBar.MenuItems[0] as ApplicationBarMenuItem; ApplicationBarMenuItem menuItem2 = ApplicationBar.MenuItems[1] as ApplicationBarMenuItem; + ApplicationBarMenuItem menuItem3 = ApplicationBar.MenuItems[2] as ApplicationBarMenuItem; if (menuItem1 != null) { menuItem1.Text = AppResources.Setting_UserProfilAppBarTitle; @@ -41,6 +42,11 @@ namespace CampusAppWP8.Pages menuItem2.Text = AppResources.Setting_ApplAppBarTitle; } + if (menuItem3 != null) + { + menuItem3.Text = "Nfc"; + } + if (!Settings.AppSetting.InitApp) { this.ShowOptIns(); @@ -126,6 +132,17 @@ namespace CampusAppWP8.Pages NavigationService.Navigate(url); } + /// + /// Method Navigate to + /// + /// Caller of the function + /// some EventArgs + private void ApplicationBarMenuItem3_Click(object sender, EventArgs e) + { + Uri url = new Uri("/Pages/Dev/NFC.xaml", UriKind.Relative); + NavigationService.Navigate(url); + } + /// /// Method change the Opacity of the ApplicationBar /// From e0d1082a9323e899623de080550e5d1ef3db9121 Mon Sep 17 00:00:00 2001 From: stubbfel Date: Wed, 21 Aug 2013 11:50:24 +0200 Subject: [PATCH 2/7] add NDEFMessage --- CampusAppWP8/CampusAppWP8/CampusAppWP8.csproj | 4 +- .../Model/Utility/NDEF/NDEFRecord.cs | 92 --------- .../CampusAppWP8/Pages/Dev/NFC.xaml.cs | 13 +- .../CampusAppWP8/Utility/NDEF/NDEFMessage.cs | 86 +++++++++ .../CampusAppWP8/Utility/NDEF/NDEFRecord.cs | 180 ++++++++++++++++++ .../Utility/NDEF/NDEFShortRecord.cs | 60 ++++++ 6 files changed, 336 insertions(+), 99 deletions(-) delete mode 100644 CampusAppWP8/CampusAppWP8/Model/Utility/NDEF/NDEFRecord.cs create mode 100644 CampusAppWP8/CampusAppWP8/Utility/NDEF/NDEFMessage.cs create mode 100644 CampusAppWP8/CampusAppWP8/Utility/NDEF/NDEFRecord.cs create mode 100644 CampusAppWP8/CampusAppWP8/Utility/NDEF/NDEFShortRecord.cs diff --git a/CampusAppWP8/CampusAppWP8/CampusAppWP8.csproj b/CampusAppWP8/CampusAppWP8/CampusAppWP8.csproj index a2653e83..977c79f6 100644 --- a/CampusAppWP8/CampusAppWP8/CampusAppWP8.csproj +++ b/CampusAppWP8/CampusAppWP8/CampusAppWP8.csproj @@ -116,7 +116,9 @@ - + + + diff --git a/CampusAppWP8/CampusAppWP8/Model/Utility/NDEF/NDEFRecord.cs b/CampusAppWP8/CampusAppWP8/Model/Utility/NDEF/NDEFRecord.cs deleted file mode 100644 index a9bfbbff..00000000 --- a/CampusAppWP8/CampusAppWP8/Model/Utility/NDEF/NDEFRecord.cs +++ /dev/null @@ -1,92 +0,0 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace CampusAppWP8.Model.Utility.NDEF -{ - public class NDEFRecord - { - private const byte typeSize = 0x01; - public enum NDEFFlags - { - UNSET = 0x00, - MBSET = 0x80, - MESET = 0x40, - CFSET = 0x20, - SRSET = 0x10, - ILSET = 0x08 - } - - public enum TNFVAL - { - EMPTY = 0x00, - WKT = 0x01, - MEDIATYPE = 0x02, - URI = 0x03, - NFCE = 0x04, - UNKOWN = 0x05, - UNCHANGED = 0x06, - RESERVED = 0x07 - } - - public enum TYPEVAL - { - EMPTY = 0x00, - URL = 0x55, - TEXT = 0x54, - } - - public NDEFRecord() - { - - } - public NDEFRecord(byte[] array) - { - this.Payload = Encoding.UTF8.GetString(array, 4, array.Length - 4); - } - public NDEFFlags MB { get; set; } - public NDEFFlags ME { get; set; } - public NDEFFlags CF { get; set; } - public NDEFFlags SR { get; set; } - public NDEFFlags IL { get; set; } - public TNFVAL TNF { get; set; } - public TYPEVAL Type { get; set; } - public byte FormatFlags - { - get - { - return (byte)((byte)this.TNF | ((byte)this.MB) | ((byte)this.ME) | ((byte)this.CF) | ((byte)this.SR) | ((byte)this.IL)); - } - } - - - public byte Record = 0xD1; - - public string Payload = "nokia.com"; - - public byte[] toByteArray() - { - MemoryStream m = new MemoryStream(); - byte[] payloadAr = Encoding.UTF8.GetBytes(Payload); - byte[] array = new byte[payloadAr.Length + 4]; - - array[0] = this.FormatFlags; - array[1] = NDEFRecord.typeSize; - array[2] = (byte)(Payload.Length); - array[3] = (byte)this.Type; - - int i = 4; - foreach (byte b in payloadAr) - { - array[i] = b; - i++; - } - return array; - } - - - } -} diff --git a/CampusAppWP8/CampusAppWP8/Pages/Dev/NFC.xaml.cs b/CampusAppWP8/CampusAppWP8/Pages/Dev/NFC.xaml.cs index 6549e8a8..d5a2fb86 100644 --- a/CampusAppWP8/CampusAppWP8/Pages/Dev/NFC.xaml.cs +++ b/CampusAppWP8/CampusAppWP8/Pages/Dev/NFC.xaml.cs @@ -9,7 +9,7 @@ using Microsoft.Phone.Controls; using Microsoft.Phone.Shell; using Windows.Networking.Proximity; using System.Runtime.InteropServices.WindowsRuntime; -using CampusAppWP8.Model.Utility.NDEF; +using CampusAppWP8.Utility.NDEF; namespace CampusAppWP8.Pages.Dev @@ -60,15 +60,16 @@ namespace CampusAppWP8.Pages.Dev this.device.StopSubscribingForMessage(message.SubscriptionId); - NDEFRecord ndef = new NDEFRecord(); + /* 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"; - device.PublishBinaryMessage("NDEF:WriteTag", ndef.toByteArray().AsBuffer(), publishHandler); + ndef.ToByteArray(); + ndef.Payload = "test";*/ + NDEFMessage ndef = new NDEFMessage("test", NDEFMessage.TYPEVAL.TEXT); + device.PublishBinaryMessage("NDEF:WriteTag", ndef.ToByteArray().AsBuffer(), publishHandler); } private void publishHandler(ProximityDevice sender, long messageId) @@ -84,7 +85,7 @@ namespace CampusAppWP8.Pages.Dev var NDefMessage = message.Data; byte[] testa = NDefMessage.ToArray(); - NDEFRecord test = new NDEFRecord(testa); + // NDEFRecord test = new NDEFRecord(testa); } } } \ No newline at end of file diff --git a/CampusAppWP8/CampusAppWP8/Utility/NDEF/NDEFMessage.cs b/CampusAppWP8/CampusAppWP8/Utility/NDEF/NDEFMessage.cs new file mode 100644 index 00000000..9dac3ec4 --- /dev/null +++ b/CampusAppWP8/CampusAppWP8/Utility/NDEF/NDEFMessage.cs @@ -0,0 +1,86 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CampusAppWP8.Utility.NDEF +{ + public class NDEFMessage + { + private List records; + + public NDEFMessage(string content, TYPEVAL type) + { + this.records = new List(); + float recordsCount = content.Length / NDEFRecord.MaxRecordPayLoad; + NDEFRecord tmpRecord = null; + + for (int i = 0; recordsCount > 0; i++) + { + tmpRecord = new NDEFShortRecord(); + tmpRecord.Type = type; + tmpRecord.Payload = content.Substring(i * 255, 255); + if (i == 0) + { + tmpRecord.MB = NDEFRecord.NDEFFlags.MBSET; + } + this.records.Add(tmpRecord); + recordsCount--; + } + + this.records[this.records.IndexOf(tmpRecord)].ME = NDEFRecord.NDEFFlags.MESET; + } + + public NDEFMessage(byte[] array) + { + this.records = new List(); + + NDEFRecord tmpRecord = null; + for (int i = 0; i < array.Length; i += tmpRecord.RecordSize) + { + tmpRecord = new NDEFShortRecord(array,i); + this.records.Add(tmpRecord); + } + } + + + /// Values that represent TYPEVAL. + /// Stubbfel, 21.08.2013. + public enum TYPEVAL + { + /// An enum constant representing the empty option. + EMPTY = 0x00, + + /// An enum constant representing the URL option. + URL = 0x55, + + /// An enum constant representing the text option. + TEXT = 0x54, + } + + public string GetContent() + { + string result = string.Empty; + + foreach (NDEFRecord record in this.records) + { + result += record.Payload; + } + + return result; + } + + public byte[] ToByteArray() + { + + MemoryStream ms = new MemoryStream(); + foreach (NDEFRecord record in this.records) + { + ms.Write(record.ToByteArray(), 0, record.RecordSize); + } + return ms.ToArray(); + } + } +} diff --git a/CampusAppWP8/CampusAppWP8/Utility/NDEF/NDEFRecord.cs b/CampusAppWP8/CampusAppWP8/Utility/NDEF/NDEFRecord.cs new file mode 100644 index 00000000..50f9b46e --- /dev/null +++ b/CampusAppWP8/CampusAppWP8/Utility/NDEF/NDEFRecord.cs @@ -0,0 +1,180 @@ +//----------------------------------------------------------------------- +// +// Company copyright tag. +// +// stubbfel +// 21.08.2013 +//---------------------------------------------------------------------- +namespace CampusAppWP8.Utility.NDEF +{ + using System.IO; + using System.Text; + + /// Ndef record of a NDEFMessage. + /// Stubbfel, 21.08.2013. + public abstract class NDEFRecord + { + #region Members + + /// The maximum record pay load. + public const int MaxRecordPayLoad = 255; + + /// Size of the type. + protected const byte TypeSize = 0x01; + + /// Size of the type. + protected int HeaderSize; + + #endregion + + #region Constructors + /// Initializes a new instance of the NDEFRecord class. + /// Stubbfel, 21.08.2013. + public NDEFRecord() + { + } + + /// Initializes a new instance of the NDEFRecord class. + /// Stubbfel, 21.08.2013. + /// The array. + public NDEFRecord(byte[] array, int index = 0) + { + this.FormatFlags = array[index]; + } + + #endregion + + #region enum + + /// Values that represent NDEFFlags. + /// Stubbfel, 21.08.2013. + public enum NDEFFlags + { + /// An Enum constant representing the UNSET option. + UNSET = 0x00, + + /// An Enum constant representing the Message begin option. + MBSET = 0x80, + + /// An Enum constant representing the Message end option. + MESET = 0x40, + + /// An Enum constant representing the CHUNK FLAG option. + CFSET = 0x20, + + /// An Enum constant representing the Short Record set option. + SRSET = 0x10, + + /// An Enum constant representing the ID length option. + ILSET = 0x08, + + /// An enum constant representing the tnfset option. + TNFSET = 0x03 + } + + /// Values that represent TNFVAL. + /// Stubbfel, 21.08.2013. + public enum TNFVAL + { + /// An enum constant representing the empty option. + EMPTY = 0x00, + + /// An enum constant representing the Well-Know-Type option. + WKT = 0x01, + + /// An enum constant representing the MediaType option. + MEDIATYPE = 0x02, + + /// An enum constant representing the URI option. + URI = 0x03, + + /// An enum constant representing the NFCE option. + NFCE = 0x04, + + /// An enum constant representing the unknow option. + unknow = 0x05, + + /// An enum constant representing the unchanged option. + UNCHANGED = 0x06, + + /// An enum constant representing the reserved option. + RESERVED = 0x07 + } + + #endregion + + #region Properties + /// Gets or sets the MBFlag. + /// The MBFlag. + public NDEFFlags MB { get; set; } + + /// Gets or sets MEFlag. + /// The MEFlag . + public NDEFFlags ME { get; set; } + + /// Gets or sets the CFFlag. + /// The CFFlag. + public NDEFFlags CF { get; set; } + + /// Gets or sets the SRFlag. + /// The SRFlag. + public NDEFFlags SR { get; set; } + + /// Gets or sets the ILFlag. + /// The ILFlag. + public NDEFFlags IL { get; set; } + + /// Gets or sets the TNFField. + /// The TNFField. + public TNFVAL TNF { get; set; } + + /// Gets or sets the type. + /// The type. + public NDEFMessage.TYPEVAL Type { get; set; } + + /// Gets 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); + this.MB = (NDEFFlags)(value & (byte)NDEFFlags.MBSET); + this.ME = (NDEFFlags)(value & (byte)NDEFFlags.MESET); + this.CF = (NDEFFlags)(value & (byte)NDEFFlags.CFSET); + this.SR = (NDEFFlags)(value & (byte)NDEFFlags.SRSET); + this.IL = (NDEFFlags)(value & (byte)NDEFFlags.ILSET); + } + + } + + /// Gets or sets the payload. + /// The payload. + public string Payload { get; set; } + + /// Gets the size of the record. + /// The size of the record. + public int RecordSize + { + get + { + return this.HeaderSize + this.Payload.Length; + } + } + + #endregion + + #region Methods + + /// Converts the record to a byte array. + /// Stubbfel, 21.08.2013. + /// This object as a byte[]. + public abstract byte[] ToByteArray(); + + #endregion + } +} diff --git a/CampusAppWP8/CampusAppWP8/Utility/NDEF/NDEFShortRecord.cs b/CampusAppWP8/CampusAppWP8/Utility/NDEF/NDEFShortRecord.cs new file mode 100644 index 00000000..02796f30 --- /dev/null +++ b/CampusAppWP8/CampusAppWP8/Utility/NDEF/NDEFShortRecord.cs @@ -0,0 +1,60 @@ +//----------------------------------------------------------------------- +// +// Company copyright tag. +// +// stubbfel +// 21.08.2013 +//---------------------------------------------------------------------- +namespace CampusAppWP8.Utility.NDEF +{ + using System.Text; + + /// Ndef short record. + /// Stubbfel, 21.08.2013. + public class NDEFShortRecord : NDEFRecord + { + /// Initializes a new instance of the NDEFShortRecord class. + /// Stubbfel, 21.08.2013. + public NDEFShortRecord() + { + this.HeaderSize = 4; + this.SR = NDEFFlags.SRSET; + this.IL = NDEFFlags.UNSET; + this.CF = NDEFFlags.UNSET; + } + + /// Initializes a new instance of the NDEFShortRecord class. + /// Stubbfel, 21.08.2013. + /// The array. + 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); + } + + /// Converts this NDEFShortRecord to a byte array. + /// Stubbfel, 21.08.2013. + /// This object as a byte[]. + public override byte[] ToByteArray() + { + byte[] payloadAr = Encoding.UTF8.GetBytes(Payload); + byte[] array = new byte[payloadAr.Length + this.HeaderSize]; + + array[0] = this.FormatFlags; + array[1] = NDEFRecord.TypeSize; + array[2] = (byte)(Payload.Length); + array[3] = (byte)this.Type; + + int i = this.HeaderSize; + foreach (byte b in payloadAr) + { + array[i] = b; + i++; + } + return array; + } + } +} From dff16799a3768b8b7ce2accf44a6f61a180778f5 Mon Sep 17 00:00:00 2001 From: stubbfel Date: Wed, 21 Aug 2013 15:26:32 +0200 Subject: [PATCH 3/7] fix messages --- .../CampusAppWP8/Pages/Dev/NFC.xaml.cs | 14 ++- .../CampusAppWP8/Utility/NDEF/NDEFMessage.cs | 92 ++++++++++++++++--- .../CampusAppWP8/Utility/NDEF/NDEFRecord.cs | 24 +++-- .../Utility/NDEF/NDEFShortRecord.cs | 11 ++- .../CampusAppWP8/Utility/Utilities.cs | 1 + 5 files changed, 110 insertions(+), 32 deletions(-) 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(); From ac67505a1db2a1b3b546f1b86ef94789608b50fe Mon Sep 17 00:00:00 2001 From: stubbfel Date: Wed, 21 Aug 2013 15:55:40 +0200 Subject: [PATCH 4/7] add toNfcString --- CampusAppWP8/CampusAppWP8/Model/GeoDb/PlaceModel.cs | 9 +++++++++ CampusAppWP8/CampusAppWP8/Pages/Dev/NFC.xaml.cs | 7 ++++--- .../CampusAppWP8/Utility/NDEF/NDEFShortRecord.cs | 2 +- 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/CampusAppWP8/CampusAppWP8/Model/GeoDb/PlaceModel.cs b/CampusAppWP8/CampusAppWP8/Model/GeoDb/PlaceModel.cs index 876a7701..0a2ed4c2 100644 --- a/CampusAppWP8/CampusAppWP8/Model/GeoDb/PlaceModel.cs +++ b/CampusAppWP8/CampusAppWP8/Model/GeoDb/PlaceModel.cs @@ -83,5 +83,14 @@ namespace CampusAppWP8.Model.GeoDb /// The services. [XmlElement("placeService")] public ObservableCollection Services { get; set; } + + /// Converts this object to a nfc string. + /// Stubbfel, 21.08.2013. + /// This object as a string. + public string ToNfcString() + { + string nfcStr = "{\"pid\":\"" + this.PlaceId + "\",\"parent\":\"" + this.ParentId + "\"}"; + return nfcStr; + } } } diff --git a/CampusAppWP8/CampusAppWP8/Pages/Dev/NFC.xaml.cs b/CampusAppWP8/CampusAppWP8/Pages/Dev/NFC.xaml.cs index b5052c7a..be6879c1 100644 --- a/CampusAppWP8/CampusAppWP8/Pages/Dev/NFC.xaml.cs +++ b/CampusAppWP8/CampusAppWP8/Pages/Dev/NFC.xaml.cs @@ -11,6 +11,7 @@ using Windows.Networking.Proximity; using System.Runtime.InteropServices.WindowsRuntime; using CampusAppWP8.Utility.NDEF; using Windows.Storage.Streams; +using CampusAppWP8.Model.GeoDb; namespace CampusAppWP8.Pages.Dev @@ -61,7 +62,7 @@ namespace CampusAppWP8.Pages.Dev this.device.StopSubscribingForMessage(message.SubscriptionId); - NDEFMessage ndef = new NDEFMessage("test", NDEFMessage.TYPEVAL.TEXT); + NDEFMessage ndef = new NDEFMessage((new PlaceModel() { PlaceId = "1"}).ToNfcString(), NDEFMessage.TYPEVAL.TEXT); byte[] ndefB = ndef.ToByteArray(); IBuffer buffer = ndefB.AsBuffer(); device.PublishBinaryMessage("NDEF:WriteTag", ndef.ToByteArray().AsBuffer(), publishHandler); @@ -82,8 +83,8 @@ namespace CampusAppWP8.Pages.Dev byte[] testa = NDefMessage.ToArray(); NDEFMessage ndef = new NDEFMessage(testa); - string c = ndef.GetContent(); - // NDEFRecord test = new NDEFRecord(testa); + Dispatcher.BeginInvoke(new Action(() => MessageBox.Show(ndef.GetContent()))); + } } } \ No newline at end of file diff --git a/CampusAppWP8/CampusAppWP8/Utility/NDEF/NDEFShortRecord.cs b/CampusAppWP8/CampusAppWP8/Utility/NDEF/NDEFShortRecord.cs index 6f7bd4f8..06927893 100644 --- a/CampusAppWP8/CampusAppWP8/Utility/NDEF/NDEFShortRecord.cs +++ b/CampusAppWP8/CampusAppWP8/Utility/NDEF/NDEFShortRecord.cs @@ -42,7 +42,7 @@ namespace CampusAppWP8.Utility.NDEF /// This object as a byte[]. public override byte[] ToByteArray() { - byte[] payloadAr = Encoding.UTF8.GetBytes(this.PayloadPraefix + Payload); + byte[] payloadAr = Encoding.UTF8.GetBytes(this.PayloadPraefix + this.Payload); byte[] array = new byte[payloadAr.Length + this.HeaderSize]; array[0] = this.FormatFlags; From 065a8e62c3e050bf34bd77a42c56c7fca26d9fb3 Mon Sep 17 00:00:00 2001 From: stubbfel Date: Wed, 21 Aug 2013 16:34:32 +0200 Subject: [PATCH 5/7] end day --- CampusAppWP8/CampusAppWP8/Utility/Utilities.cs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/CampusAppWP8/CampusAppWP8/Utility/Utilities.cs b/CampusAppWP8/CampusAppWP8/Utility/Utilities.cs index 246dd11b..0ab67a9c 100644 --- a/CampusAppWP8/CampusAppWP8/Utility/Utilities.cs +++ b/CampusAppWP8/CampusAppWP8/Utility/Utilities.cs @@ -12,11 +12,14 @@ namespace CampusAppWP8.Utility using System.Device.Location; using System.Globalization; using System.Linq; + using System.Runtime.InteropServices.WindowsRuntime; using System.Threading; using System.Windows; using System.Windows.Controls; using System.Windows.Media; using CampusAppWP8.Resources; + using CampusAppWP8.Utility.NDEF; + using Windows.Networking.Proximity; /// /// Collection of utility functions. @@ -276,5 +279,19 @@ namespace CampusAppWP8.Utility } } } + + /// Writes a nfc tag. + /// Stubbfel, 21.08.2013. + /// The tag content. + /// The action. + public static void WriteNfcTag(string tagContent) + { + NDEFMessage ndef = new NDEFMessage(tagContent, NDEFMessage.TYPEVAL.TEXT); + ProximityDevice device = ProximityDevice.GetDefault(); + if (device != null) + { + device.PublishBinaryMessage("NDEF:WriteTag", ndef.ToByteArray().AsBuffer()); + } + } } } \ No newline at end of file From 50012c8ab90e399249c97dfd22bc96ea6a0f2dce Mon Sep 17 00:00:00 2001 From: stubbfel Date: Thu, 22 Aug 2013 15:11:59 +0200 Subject: [PATCH 6/7] finish nfcpage --- .../Pages/Campusmap/CampusMapPage.xaml.cs | 3 +- CampusAppWP8/CampusAppWP8/Pages/Dev/NFC.xaml | 11 +- .../CampusAppWP8/Pages/Dev/NFC.xaml.cs | 165 +++++++++++------- .../CampusAppWP8/Utility/Utilities.cs | 14 -- 4 files changed, 113 insertions(+), 80 deletions(-) diff --git a/CampusAppWP8/CampusAppWP8/Pages/Campusmap/CampusMapPage.xaml.cs b/CampusAppWP8/CampusAppWP8/Pages/Campusmap/CampusMapPage.xaml.cs index ddc82665..6733b1b8 100644 --- a/CampusAppWP8/CampusAppWP8/Pages/Campusmap/CampusMapPage.xaml.cs +++ b/CampusAppWP8/CampusAppWP8/Pages/Campusmap/CampusMapPage.xaml.cs @@ -32,7 +32,8 @@ namespace CampusAppWP8.Pages.Campusmap public CampusMapPage() { this.InitializeComponent(); - this.map = new CBMainMapModel(); + + this.MapCanvas.DataContext = this.map; } diff --git a/CampusAppWP8/CampusAppWP8/Pages/Dev/NFC.xaml b/CampusAppWP8/CampusAppWP8/Pages/Dev/NFC.xaml index cac77add..f4a6505a 100644 --- a/CampusAppWP8/CampusAppWP8/Pages/Dev/NFC.xaml +++ b/CampusAppWP8/CampusAppWP8/Pages/Dev/NFC.xaml @@ -24,10 +24,19 @@ - + ed + + + +