finish nfcpage
This commit is contained in:
@@ -32,7 +32,8 @@ namespace CampusAppWP8.Pages.Campusmap
|
|||||||
public CampusMapPage()
|
public CampusMapPage()
|
||||||
{
|
{
|
||||||
this.InitializeComponent();
|
this.InitializeComponent();
|
||||||
this.map = new CBMainMapModel();
|
|
||||||
|
|
||||||
this.MapCanvas.DataContext = this.map;
|
this.MapCanvas.DataContext = this.map;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -24,10 +24,19 @@
|
|||||||
<StackPanel Grid.Row="0" Margin="12,17,0,28">
|
<StackPanel Grid.Row="0" Margin="12,17,0,28">
|
||||||
<TextBlock Text="Campusapp" Style="{StaticResource PhoneTextNormalStyle}"/>
|
<TextBlock Text="Campusapp" Style="{StaticResource PhoneTextNormalStyle}"/>
|
||||||
<TextBlock Text="NFC" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
|
<TextBlock Text="NFC" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
|
||||||
</StackPanel>
|
</StackPanel>ed
|
||||||
|
|
||||||
<!--ContentPanel - zusätzliche Inhalte hier platzieren-->
|
<!--ContentPanel - zusätzliche Inhalte hier platzieren-->
|
||||||
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
|
<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>
|
||||||
</Grid>
|
</Grid>
|
||||||
|
|||||||
@@ -1,90 +1,127 @@
|
|||||||
using System;
|
//-----------------------------------------------------------------------
|
||||||
using System.Collections.Generic;
|
// <copyright file="NFC.xaml.cs" company="BTU/IIT">
|
||||||
using System.Linq;
|
// Company copyright tag.
|
||||||
using System.Net;
|
// </copyright>
|
||||||
using System.Windows;
|
// <author>stubbfel</author>
|
||||||
using System.Windows.Controls;
|
// <sience>08.08.2013</sience>
|
||||||
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;
|
|
||||||
|
|
||||||
|
|
||||||
namespace CampusAppWP8.Pages.Dev
|
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
|
public partial class NFC : PhoneApplicationPage
|
||||||
{
|
{
|
||||||
|
/// <summary>The device.</summary>
|
||||||
private readonly ProximityDevice device = ProximityDevice.GetDefault();
|
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()
|
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>
|
/// <summary>Override the OnNavigatedTo method.</summary>
|
||||||
/// Override the OnNavigatedTo method
|
/// <remarks>Stubbfel, 22.08.2013.</remarks>
|
||||||
/// </summary>
|
/// <param name="e">Arguments of navigation.</param>
|
||||||
/// <param name="e">Arguments of navigation</param>
|
|
||||||
protected override void OnNavigatedTo(NavigationEventArgs e)
|
protected override void OnNavigatedTo(NavigationEventArgs e)
|
||||||
{
|
{
|
||||||
base.OnNavigatedTo(e);
|
base.OnNavigatedTo(e);
|
||||||
|
this.Writecontent.Text = this.ndefList[this.actNDEFIndex].GetContent();
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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.StopPublishingMessage(messageId);
|
||||||
|
this.Dispatcher.BeginInvoke(new Action(() => MessageBox.Show("Writed:" + this.actNDEFIndex)));
|
||||||
this.device.StopSubscribingForMessage(this.lastMsgId);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>Handler, called when the ndef.</summary>
|
||||||
private void WriteableTagDetected(ProximityDevice sender, ProximityMessage message)
|
/// <remarks>Stubbfel, 22.08.2013.</remarks>
|
||||||
{
|
/// <param name="sender"> The sender.</param>
|
||||||
Deployment.Current.Dispatcher.BeginInvoke(() =>
|
/// <param name="message">The message.</param>
|
||||||
{
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
private void NDEFHandler(ProximityDevice sender, ProximityMessage message)
|
private void NDEFHandler(ProximityDevice sender, ProximityMessage message)
|
||||||
{
|
{
|
||||||
|
this.device.StopSubscribingForMessage(message.SubscriptionId);
|
||||||
var NDefMessage = message.Data;
|
var ndefMessage = message.Data;
|
||||||
byte[] testa = NDefMessage.ToArray();
|
byte[] testa = ndefMessage.ToArray();
|
||||||
|
|
||||||
NDEFMessage ndef = new NDEFMessage(testa);
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -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());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user