fix messages

This commit is contained in:
stubbfel
2013-08-21 15:26:32 +02:00
parent e0d1082a93
commit dff16799a3
5 changed files with 110 additions and 32 deletions

View File

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

View File

@@ -1,31 +1,58 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
//-----------------------------------------------------------------------
// <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;
public NDEFMessage(string content, TYPEVAL type)
#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 = 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;
}
/// <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>();
@@ -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
/// <summary>Values that represent TYPEVAL.</summary>
/// <remarks>Stubbfel, 21.08.2013.</remarks>
@@ -60,6 +94,35 @@ namespace CampusAppWP8.Utility.NDEF
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;
@@ -72,15 +135,20 @@ namespace CampusAppWP8.Utility.NDEF
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
}
}

View File

@@ -20,10 +20,7 @@ namespace CampusAppWP8.Utility.NDEF
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;
protected const byte TypeSize = 0x01;
#endregion
@@ -37,6 +34,7 @@ namespace CampusAppWP8.Utility.NDEF
/// <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];
@@ -132,14 +130,15 @@ namespace CampusAppWP8.Utility.NDEF
/// <value>The type.</value>
public NDEFMessage.TYPEVAL Type { get; set; }
/// <summary>Gets the format flags.</summary>
/// <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);
@@ -149,7 +148,6 @@ namespace CampusAppWP8.Utility.NDEF
this.SR = (NDEFFlags)(value & (byte)NDEFFlags.SRSET);
this.IL = (NDEFFlags)(value & (byte)NDEFFlags.ILSET);
}
}
/// <summary>Gets or sets the payload.</summary>
@@ -162,10 +160,18 @@ namespace CampusAppWP8.Utility.NDEF
{
get
{
return this.HeaderSize + this.Payload.Length;
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
@@ -178,3 +184,5 @@ namespace CampusAppWP8.Utility.NDEF
#endregion
}
}
// End of Utility\NDEF\NDEFRecord.cs

View File

@@ -26,13 +26,15 @@ namespace CampusAppWP8.Utility.NDEF
/// <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];
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);
}
/// <summary>Converts this NDEFShortRecord to a byte array.</summary>
@@ -40,12 +42,12 @@ namespace CampusAppWP8.Utility.NDEF
/// <returns>This object as a byte[].</returns>
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;
}
}

View File

@@ -224,6 +224,7 @@ namespace CampusAppWP8.Utility
// set to 0 point, if access to device is not allow
geoposition = new GeoPosition<GeoCoordinate>();
geoposition.Location = new GeoCoordinate(0, 0);
geoposition.Timestamp = DateTime.Now;
}
watcher.Stop();