finish nfcpage

This commit is contained in:
stubbfel
2013-08-22 15:11:59 +02:00
parent 065a8e62c3
commit 50012c8ab9
4 changed files with 113 additions and 80 deletions

View File

@@ -32,7 +32,8 @@ namespace CampusAppWP8.Pages.Campusmap
public CampusMapPage()
{
this.InitializeComponent();
this.map = new CBMainMapModel();
this.MapCanvas.DataContext = this.map;
}

View File

@@ -24,10 +24,19 @@
<StackPanel Grid.Row="0" Margin="12,17,0,28">
<TextBlock Text="Campusapp" Style="{StaticResource PhoneTextNormalStyle}"/>
<TextBlock Text="NFC" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
</StackPanel>
</StackPanel>ed
<!--ContentPanel - zusätzliche Inhalte hier platzieren-->
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<StackPanel>
<TextBlock Name="Writecontent" Height="500"/>
<StackPanel Orientation="Horizontal">
<Button Content="Write" Name="Write" HorizontalAlignment="Left" VerticalAlignment="Top" Click="Write_Click"/>
<Button Content="Prev" Name="Prev" HorizontalAlignment="Left" VerticalAlignment="Top" Click="Prev_Click"/>
<Button Content="Next" Name="Next" HorizontalAlignment="Left" VerticalAlignment="Top" Click="Next_Click"/>
<Button Content="Read" Name="Read" HorizontalAlignment="Left" VerticalAlignment="Top" Click="Read_Click"/>
</StackPanel>
</StackPanel>
</Grid>
</Grid>

View File

@@ -1,90 +1,127 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Windows.Networking.Proximity;
using System.Runtime.InteropServices.WindowsRuntime;
using CampusAppWP8.Utility.NDEF;
using Windows.Storage.Streams;
using CampusAppWP8.Model.GeoDb;
//-----------------------------------------------------------------------
// <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();
private long lastMsgId;
/// <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()
{
InitializeComponent();
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>
/// <param name="e">Arguments of navigation</param>
/// <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);
if (this.device != null)
{
MessageBox.Show("NFC present");
}
else
{
MessageBox.Show("Your phone has no NFC or NFC is disabled");
}
this.lastMsgId = this.device.SubscribeForMessage("WriteableTag", WriteableTagDetected);
this.Writecontent.Text = this.ndefList[this.actNDEFIndex].GetContent();
}
protected override void OnNavigatedFrom(NavigationEventArgs e)
/// <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)
{
base.OnNavigatedTo(e);
this.device.StopSubscribingForMessage(this.lastMsgId);
this.device.StopPublishingMessage(messageId);
this.Dispatcher.BeginInvoke(new Action(() => MessageBox.Show("Writed:" + this.actNDEFIndex)));
}
private void WriteableTagDetected(ProximityDevice sender, ProximityMessage message)
{
Deployment.Current.Dispatcher.BeginInvoke(() =>
{
MessageBox.Show("message");
});
this.device.StopSubscribingForMessage(message.SubscriptionId);
NDEFMessage ndef = new NDEFMessage((new PlaceModel() { PlaceId = "1"}).ToNfcString(), NDEFMessage.TYPEVAL.TEXT);
byte[] ndefB = ndef.ToByteArray();
IBuffer buffer = ndefB.AsBuffer();
device.PublishBinaryMessage("NDEF:WriteTag", ndef.ToByteArray().AsBuffer(), publishHandler);
}
private void publishHandler(ProximityDevice sender, long messageId)
{
device.StopPublishingMessage(messageId);
device.SubscribeForMessage("NDEF", NDEFHandler);
}
/// <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)
{
var NDefMessage = message.Data;
byte[] testa = NDefMessage.ToArray();
this.device.StopSubscribingForMessage(message.SubscriptionId);
var ndefMessage = message.Data;
byte[] testa = ndefMessage.ToArray();
NDEFMessage ndef = new NDEFMessage(testa);
Dispatcher.BeginInvoke(new Action(() => MessageBox.Show(ndef.GetContent())));
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);
}
}
}

View File

@@ -279,19 +279,5 @@ namespace CampusAppWP8.Utility
}
}
}
/// <summary>Writes a nfc tag.</summary>
/// <remarks>Stubbfel, 21.08.2013.</remarks>
/// <param name="tagContent">The tag content.</param>
/// <param name="action"> The action.</param>
public static void WriteNfcTag(string tagContent)
{
NDEFMessage ndef = new NDEFMessage(tagContent, NDEFMessage.TYPEVAL.TEXT);
ProximityDevice device = ProximityDevice.GetDefault();
if (device != null)
{
device.PublishBinaryMessage("NDEF:WriteTag", ndef.ToByteArray().AsBuffer());
}
}
}
}