155 lines
4.8 KiB
C#
155 lines
4.8 KiB
C#
//-----------------------------------------------------------------------
|
|
// <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
|
|
}
|
|
}
|