add NDEFMessage

This commit is contained in:
stubbfel
2013-08-21 11:50:24 +02:00
parent ebdefaa5e6
commit e0d1082a93
6 changed files with 336 additions and 99 deletions

View File

@@ -116,7 +116,9 @@
<Compile Include="Model\Utility\DegreeListPickerItemListModel.cs" />
<Compile Include="Model\Utility\CleanUrlParamModel.cs" />
<Compile Include="Model\Utility\CampusListPickerItemListModel.cs" />
<Compile Include="Model\Utility\NDEF\NDEFRecord.cs" />
<Compile Include="Utility\NDEF\NDEFMessage.cs" />
<Compile Include="Utility\NDEF\NDEFRecord.cs" />
<Compile Include="Utility\NDEF\NDEFShortRecord.cs" />
<Compile Include="Model\Utility\RoleListPickerItemListModel.cs" />
<Compile Include="Model\Utility\SemesterListPickerItemListModel.cs" />
<Compile Include="Model\Utility\ListPickerItemListModel.cs" />

View File

@@ -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;
}
}
}

View File

@@ -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);
}
}
}

View File

@@ -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<NDEFRecord> records;
public NDEFMessage(string content, TYPEVAL type)
{
this.records = new List<NDEFRecord>();
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>();
NDEFRecord tmpRecord = null;
for (int i = 0; i < array.Length; i += tmpRecord.RecordSize)
{
tmpRecord = new NDEFShortRecord(array,i);
this.records.Add(tmpRecord);
}
}
/// <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,
}
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();
}
}
}

View File

@@ -0,0 +1,180 @@
//-----------------------------------------------------------------------
// <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;
/// <summary>Size of the type.</summary>
protected int HeaderSize;
#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>
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 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;
}
}
#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
}
}

View File

@@ -0,0 +1,60 @@
//-----------------------------------------------------------------------
// <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>
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);
}
/// <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(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;
}
}
}