110 lines
3.2 KiB
C#
110 lines
3.2 KiB
C#
namespace ndefpclib.message
|
|
{
|
|
using ndefpclib.records;
|
|
using ndefpclib.type;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Text;
|
|
|
|
/// <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
|
|
|
|
public NDEFMessage(string content, MsgType type)
|
|
{
|
|
this.records = new List<NDEFRecord>();
|
|
float recordsCount = (float)content.Length / NDEFRecord.MaxRecordPayLoad;
|
|
NDEFRecord tmpRecord = null;
|
|
string praefix = type.PayloadPrefix;
|
|
MsgType.TNFVAL tnf = type.TNFValue;
|
|
MsgType.TYPEVAL typeVal = type.TypeValue;
|
|
|
|
for (int i = 0; recordsCount > 0; i++)
|
|
{
|
|
tmpRecord = new NDEFShortRecord();
|
|
tmpRecord.Type = typeVal;
|
|
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, Encoding payLoadEncoding)
|
|
{
|
|
this.records = new List<NDEFRecord>();
|
|
|
|
NDEFRecord tmpRecord = null;
|
|
for (int i = 0; i < array.Length; i += tmpRecord.RecordSize)
|
|
{
|
|
tmpRecord = new NDEFShortRecord(array, payLoadEncoding, i);
|
|
|
|
this.records.Add(tmpRecord);
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Methods
|
|
|
|
/// <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 virtual byte[] ToByteArray(Encoding payLoadEncoding)
|
|
{
|
|
MemoryStream ms = new MemoryStream();
|
|
foreach (NDEFRecord record in this.records)
|
|
{
|
|
ms.Write(record.ToByteArray(payLoadEncoding), 0, record.RecordSize);
|
|
}
|
|
|
|
return ms.ToArray();
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
}
|