Files
NDEFPCLib/import/NFC.xaml.cs
stubbfel 503d79a89f import
2013-08-27 23:18:39 +02:00

127 lines
5.1 KiB
C#

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