This commit is contained in:
stubbfel
2013-08-27 23:18:39 +02:00
commit 503d79a89f
5 changed files with 576 additions and 0 deletions

154
import/NDEF/NDEFMessage.cs Normal file
View File

@@ -0,0 +1,154 @@
//-----------------------------------------------------------------------
// <copyright file="NDEFMessage.cs" company="BTU/IIT">
// Company copyright tag.
// </copyright>
// <author>stubbfel</author>
// <sience>21.08.2013</sience>
//----------------------------------------------------------------------
namespace CampusAppWP8.Utility.NDEF
{
using System.Collections.Generic;
using System.IO;
/// <summary>Ndef message.</summary>
/// <remarks>Stubbfel, 21.08.2013.</remarks>
public class NDEFMessage
{
#region Members
/// <summary>The records.</summary>
private List<NDEFRecord> records;
#endregion
#region constructors
/// <summary>Initializes a new instance of the NDEFMessage class.</summary>
/// <remarks>Stubbfel, 21.08.2013.</remarks>
/// <param name="content">The content.</param>
/// <param name="type"> The type.</param>
/// <param name="tnf"> (Optional) the tnf.</param>
public NDEFMessage(string content, TYPEVAL type, NDEFRecord.TNFVAL tnf = NDEFRecord.TNFVAL.WKT)
{
this.records = new List<NDEFRecord>();
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.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--;
}
this.records[this.records.IndexOf(tmpRecord)].ME = NDEFRecord.NDEFFlags.MESET;
}
/// <summary>Initializes a new instance of the NDEFMessage class.</summary>
/// <remarks>Stubbfel, 21.08.2013.</remarks>
/// <param name="array">The array.</param>
public NDEFMessage(byte[] array)
{
this.records = new List<NDEFRecord>();
NDEFRecord tmpRecord = null;
for (int i = 0; i < array.Length; i += tmpRecord.RecordSize)
{
tmpRecord = new NDEFShortRecord(array, i);
this.records.Add(tmpRecord);
}
}
#endregion
#region enum
/// <summary>Values that represent TYPEVAL.</summary>
/// <remarks>Stubbfel, 21.08.2013.</remarks>
public enum TYPEVAL
{
/// <summary>An enum constant representing the empty option.</summary>
EMPTY = 0x00,
/// <summary>An enum constant representing the URL option.</summary>
URL = 0x55,
/// <summary>An enum constant representing the text option.</summary>
TEXT = 0x54,
}
#endregion
#region Methods
/// <summary>Gets a praefix.</summary>
/// <remarks>Stubbfel, 21.08.2013.</remarks>
/// <param name="type">The type.</param>
/// <returns>The praefix.</returns>
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;
}
/// <summary>Gets the content.</summary>
/// <remarks>Stubbfel, 21.08.2013.</remarks>
/// <returns>The content.</returns>
public string GetContent()
{
string result = string.Empty;
foreach (NDEFRecord record in this.records)
{
result += record.Payload;
}
return result;
}
/// <summary>Converts this object to a byte array.</summary>
/// <remarks>Stubbfel, 21.08.2013.</remarks>
/// <returns>This object as a byte[].</returns>
public byte[] ToByteArray()
{
MemoryStream ms = new MemoryStream();
foreach (NDEFRecord record in this.records)
{
ms.Write(record.ToByteArray(), 0, record.RecordSize);
}
return ms.ToArray();
}
#endregion
}
}

188
import/NDEF/NDEFRecord.cs Normal file
View File

@@ -0,0 +1,188 @@
//-----------------------------------------------------------------------
// <copyright file="NDEFRecord.cs" company="BTU/IIT">
// Company copyright tag.
// </copyright>
// <author>stubbfel</author>
// <sience>21.08.2013</sience>
//----------------------------------------------------------------------
namespace CampusAppWP8.Utility.NDEF
{
using System.IO;
using System.Text;
/// <summary>Ndef record of a NDEFMessage.</summary>
/// <remarks>Stubbfel, 21.08.2013.</remarks>
public abstract class NDEFRecord
{
#region Members
/// <summary>The maximum record pay load.</summary>
public const int MaxRecordPayLoad = 255;
/// <summary>Size of the type.</summary>
protected const byte TypeSize = 0x01;
#endregion
#region Constructors
/// <summary>Initializes a new instance of the NDEFRecord class.</summary>
/// <remarks>Stubbfel, 21.08.2013.</remarks>
public NDEFRecord()
{
}
/// <summary>Initializes a new instance of the NDEFRecord class.</summary>
/// <remarks>Stubbfel, 21.08.2013.</remarks>
/// <param name="array">The array.</param>
/// <param name="index">(Optional) zero-based index of the.</param>
public NDEFRecord(byte[] array, int index = 0)
{
this.FormatFlags = array[index];
}
#endregion
#region enum
/// <summary>Values that represent NDEFFlags.</summary>
/// <remarks>Stubbfel, 21.08.2013.</remarks>
public enum NDEFFlags
{
/// <summary>An Enum constant representing the UNSET option.</summary>
UNSET = 0x00,
/// <summary>An Enum constant representing the Message begin option.</summary>
MBSET = 0x80,
/// <summary>An Enum constant representing the Message end option.</summary>
MESET = 0x40,
/// <summary>An Enum constant representing the CHUNK FLAG option.</summary>
CFSET = 0x20,
/// <summary>An Enum constant representing the Short Record set option.</summary>
SRSET = 0x10,
/// <summary>An Enum constant representing the ID length option.</summary>
ILSET = 0x08,
/// <summary>An enum constant representing the tnfset option.</summary>
TNFSET = 0x03
}
/// <summary>Values that represent TNFVAL.</summary>
/// <remarks>Stubbfel, 21.08.2013.</remarks>
public enum TNFVAL
{
/// <summary>An enum constant representing the empty option.</summary>
EMPTY = 0x00,
/// <summary>An enum constant representing the Well-Know-Type option.</summary>
WKT = 0x01,
/// <summary>An enum constant representing the MediaType option.</summary>
MEDIATYPE = 0x02,
/// <summary>An enum constant representing the URI option.</summary>
URI = 0x03,
/// <summary>An enum constant representing the NFCE option.</summary>
NFCE = 0x04,
/// <summary>An enum constant representing the unknow option.</summary>
unknow = 0x05,
/// <summary>An enum constant representing the unchanged option.</summary>
UNCHANGED = 0x06,
/// <summary>An enum constant representing the reserved option.</summary>
RESERVED = 0x07
}
#endregion
#region Properties
/// <summary>Gets or sets the MBFlag.</summary>
/// <value>The MBFlag.</value>
public NDEFFlags MB { get; set; }
/// <summary>Gets or sets MEFlag.</summary>
/// <value>The MEFlag .</value>
public NDEFFlags ME { get; set; }
/// <summary>Gets or sets the CFFlag.</summary>
/// <value>The CFFlag.</value>
public NDEFFlags CF { get; set; }
/// <summary>Gets or sets the SRFlag.</summary>
/// <value>The SRFlag.</value>
public NDEFFlags SR { get; set; }
/// <summary>Gets or sets the ILFlag.</summary>
/// <value>The ILFlag.</value>
public NDEFFlags IL { get; set; }
/// <summary>Gets or sets the TNFField.</summary>
/// <value>The TNFField.</value>
public TNFVAL TNF { get; set; }
/// <summary>Gets or sets the type.</summary>
/// <value>The type.</value>
public NDEFMessage.TYPEVAL Type { get; set; }
/// <summary>Gets or sets the format flags.</summary>
/// <value>The format flags.</value>
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);
}
}
/// <summary>Gets or sets the payload.</summary>
/// <value>The payload.</value>
public string Payload { get; set; }
/// <summary>Gets the size of the record.</summary>
/// <value>The size of the record.</value>
public int RecordSize
{
get
{
return this.HeaderSize + this.Payload.Length + this.PayloadPraefix.Length;
}
}
/// <summary>Gets or sets the payload praefix.</summary>
/// <value>The payload praefix.</value>
public string PayloadPraefix { get; set; }
/// <summary>Gets or sets the size of the header.</summary>
/// <value>The size of the header.</value>
protected int HeaderSize { get; set; }
#endregion
#region Methods
/// <summary>Converts the record to a byte array.</summary>
/// <remarks>Stubbfel, 21.08.2013.</remarks>
/// <returns>This object as a byte[].</returns>
public abstract byte[] ToByteArray();
#endregion
}
}
// End of Utility\NDEF\NDEFRecord.cs

View File

@@ -0,0 +1,63 @@
//-----------------------------------------------------------------------
// <copyright file="NDEFShortRecord.cs" company="BTU/IIT">
// Company copyright tag.
// </copyright>
// <author>stubbfel</author>
// <sience>21.08.2013</sience>
//----------------------------------------------------------------------
namespace CampusAppWP8.Utility.NDEF
{
using System.Text;
/// <summary>Ndef short record.</summary>
/// <remarks>Stubbfel, 21.08.2013.</remarks>
public class NDEFShortRecord : NDEFRecord
{
/// <summary>Initializes a new instance of the NDEFShortRecord class.</summary>
/// <remarks>Stubbfel, 21.08.2013.</remarks>
public NDEFShortRecord()
{
this.HeaderSize = 4;
this.SR = NDEFFlags.SRSET;
this.IL = NDEFFlags.UNSET;
this.CF = NDEFFlags.UNSET;
}
/// <summary>Initializes a new instance of the NDEFShortRecord class.</summary>
/// <remarks>Stubbfel, 21.08.2013.</remarks>
/// <param name="array">The array.</param>
/// <param name="index">(Optional) zero-based index of the.</param>
public NDEFShortRecord(byte[] array, int index = 0)
: base(array)
{
this.HeaderSize = 4;
this.Type = (NDEFMessage.TYPEVAL)array[index + 3];
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);
}
/// <summary>Converts this NDEFShortRecord to a byte array.</summary>
/// <remarks>Stubbfel, 21.08.2013.</remarks>
/// <returns>This object as a byte[].</returns>
public override byte[] ToByteArray()
{
byte[] payloadAr = Encoding.UTF8.GetBytes(this.PayloadPraefix + this.Payload);
byte[] array = new byte[payloadAr.Length + this.HeaderSize];
array[0] = this.FormatFlags;
array[1] = NDEFRecord.TypeSize;
array[2] = (byte)(Payload.Length + this.PayloadPraefix.Length);
array[3] = (byte)this.Type;
int i = this.HeaderSize;
foreach (byte b in payloadAr)
{
array[i] = b;
i++;
}
return array;
}
}
}

44
import/NFC.xaml Normal file
View File

@@ -0,0 +1,44 @@
<phone:PhoneApplicationPage
x:Class="CampusAppWP8.Pages.Dev.NFC"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
mc:Ignorable="d"
shell:SystemTray.IsVisible="True">
<!--LayoutRoot ist das Stammraster, in dem alle anderen Seiteninhalte platziert werden-->
<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<!--TitlePanel enthält den Namen der Anwendung und den Seitentitel-->
<StackPanel Grid.Row="0" Margin="12,17,0,28">
<TextBlock Text="Campusapp" Style="{StaticResource PhoneTextNormalStyle}"/>
<TextBlock Text="NFC" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
</StackPanel>
<!--ContentPanel - zusätzliche Inhalte hier platzieren-->
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<StackPanel>
<TextBlock Name="Writecontent" Height="500"/>
<StackPanel Orientation="Horizontal">
<Button Content="Write" Name="Write" HorizontalAlignment="Left" VerticalAlignment="Top" Click="Write_Click"/>
<Button Content="Prev" Name="Prev" HorizontalAlignment="Left" VerticalAlignment="Top" Click="Prev_Click"/>
<Button Content="Next" Name="Next" HorizontalAlignment="Left" VerticalAlignment="Top" Click="Next_Click"/>
<Button Content="Read" Name="Read" HorizontalAlignment="Left" VerticalAlignment="Top" Click="Read_Click"/>
</StackPanel>
</StackPanel>
</Grid>
</Grid>
</phone:PhoneApplicationPage>

127
import/NFC.xaml.cs Normal file
View File

@@ -0,0 +1,127 @@
//-----------------------------------------------------------------------
// <copyright file="NFC.xaml.cs" company="BTU/IIT">
// Company copyright tag.
// </copyright>
// <author>stubbfel</author>
// <sience>08.08.2013</sience>
//----------------------------------------------------------------------
namespace CampusAppWP8.Pages.Dev
{
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Windows;
using System.Windows.Navigation;
using CampusAppWP8.Model.Campusmap;
using CampusAppWP8.Model.GeoDb;
using CampusAppWP8.Utility.NDEF;
using Microsoft.Phone.Controls;
using Windows.Networking.Proximity;
/// <summary>Nfc page.</summary>
/// <remarks>Stubbfel, 22.08.2013.</remarks>
public partial class NFC : PhoneApplicationPage
{
/// <summary>The device.</summary>
private readonly ProximityDevice device = ProximityDevice.GetDefault();
/// <summary>Zero-based index of the act ndef.</summary>
private int actNDEFIndex;
/// <summary>List of ndefs.</summary>
private List<NDEFMessage> ndefList;
/// <summary>Initializes a new instance of the NFC class.</summary>
/// <remarks>Stubbfel, 22.08.2013.</remarks>
public NFC()
{
this.InitializeComponent();
this.ndefList = new List<NDEFMessage>();
MapModel map = new CBMainMapModel();
foreach (PlaceModel place in map.Spatial.Places)
{
this.ndefList.Add(new NDEFMessage(place.ToNfcString(), NDEFMessage.TYPEVAL.TEXT));
}
this.actNDEFIndex = 0;
}
/// <summary>Override the OnNavigatedTo method.</summary>
/// <remarks>Stubbfel, 22.08.2013.</remarks>
/// <param name="e">Arguments of navigation.</param>
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
this.Writecontent.Text = this.ndefList[this.actNDEFIndex].GetContent();
}
/// <summary>Handler, called when the publish.</summary>
/// <remarks>Stubbfel, 22.08.2013.</remarks>
/// <param name="sender"> The sender.</param>
/// <param name="messageId">Identifier for the message.</param>
private void PublishHandler(ProximityDevice sender, long messageId)
{
this.device.StopPublishingMessage(messageId);
this.Dispatcher.BeginInvoke(new Action(() => MessageBox.Show("Writed:" + this.actNDEFIndex)));
}
/// <summary>Handler, called when the ndef.</summary>
/// <remarks>Stubbfel, 22.08.2013.</remarks>
/// <param name="sender"> The sender.</param>
/// <param name="message">The message.</param>
private void NDEFHandler(ProximityDevice sender, ProximityMessage message)
{
this.device.StopSubscribingForMessage(message.SubscriptionId);
var ndefMessage = message.Data;
byte[] testa = ndefMessage.ToArray();
NDEFMessage ndef = new NDEFMessage(testa);
this.Dispatcher.BeginInvoke(new Action(() => MessageBox.Show(ndef.GetContent())));
}
/// <summary>Event handler. Called by Read for click events.</summary>
/// <remarks>Stubbfel, 22.08.2013.</remarks>
/// <param name="sender">The sender.</param>
/// <param name="e"> Routed event information.</param>
private void Read_Click(object sender, RoutedEventArgs e)
{
this.device.SubscribeForMessage("NDEF", this.NDEFHandler);
}
/// <summary>Event handler. Called by Next for click events.</summary>
/// <remarks>Stubbfel, 22.08.2013.</remarks>
/// <param name="sender">The sender.</param>
/// <param name="e"> Routed event information.</param>
private void Next_Click(object sender, RoutedEventArgs e)
{
if (this.actNDEFIndex < this.ndefList.Count - 1)
{
this.actNDEFIndex++;
this.Writecontent.Text = this.ndefList[this.actNDEFIndex].GetContent();
}
}
/// <summary>Event handler. Called by Prev for click events.</summary>
/// <remarks>Stubbfel, 22.08.2013.</remarks>
/// <param name="sender">The sender.</param>
/// <param name="e"> Routed event information.</param>
private void Prev_Click(object sender, RoutedEventArgs e)
{
if (this.actNDEFIndex > 0)
{
this.actNDEFIndex--;
this.Writecontent.Text = this.ndefList[this.actNDEFIndex].GetContent();
}
}
/// <summary>Event handler. Called by Write for click events.</summary>
/// <remarks>Stubbfel, 22.08.2013.</remarks>
/// <param name="sender">The sender.</param>
/// <param name="e"> Routed event information.</param>
private void Write_Click(object sender, RoutedEventArgs e)
{
this.device.PublishBinaryMessage("NDEF:WriteTag", this.ndefList[this.actNDEFIndex].ToByteArray().AsBuffer(), this.PublishHandler);
}
}
}