MainPage.xaml.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /**
  2. * Copyright (c) 2012 Nokia Corporation.
  3. */
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using Microsoft.Phone.Controls;
  8. using Windows.Networking.Proximity;
  9. using Windows.Storage.Streams;
  10. namespace NfcNdefTagReader
  11. {
  12. /// <summary>
  13. /// The application logic implementation of the main page.
  14. /// </summary>
  15. public partial class MainPage : PhoneApplicationPage
  16. {
  17. // The list of records
  18. private List<NdefRecord> recordList;
  19. // The subscription ID from ProximityDevice
  20. private long subscriptionId;
  21. // ProximityDevice instance
  22. private ProximityDevice device;
  23. // The log content which will be filled with the details of each
  24. // detected NFC tag
  25. private string logText;
  26. /// <summary>
  27. /// Constructor.
  28. /// </summary>
  29. public MainPage()
  30. {
  31. InitializeComponent();
  32. recordList = new List<NdefRecord>();
  33. device = ProximityDevice.GetDefault();
  34. device.DeviceArrived += DeviceArrived;
  35. device.DeviceDeparted += DeviceDeparted;
  36. SubscribeForMessage();
  37. }
  38. /// <summary>
  39. /// Gets called when a detected NFC device is disconnected after arrival.
  40. /// </summary>
  41. /// <param name="sender">ProximityDevice instance</param>
  42. private void DeviceDeparted(ProximityDevice sender)
  43. {
  44. Dispatcher.BeginInvoke(() =>
  45. {
  46. logText = logText + "\nLost at " + DateTime.Now.Hour + ":" + DateTime.Now.Minute + ":" + DateTime.Now.Second + "\n";
  47. AppText.Text = logText + AppText.Text;
  48. });
  49. }
  50. /// <summary>
  51. /// Gets called when a NFC device is detected.
  52. /// </summary>
  53. /// <param name="sender">ProximityDevice instance</param>
  54. private void DeviceArrived(ProximityDevice sender)
  55. {
  56. Dispatcher.BeginInvoke(() =>
  57. {
  58. logText = "\nDetected at " + DateTime.Now.Hour + ":" + DateTime.Now.Minute + ":" + DateTime.Now.Second;
  59. });
  60. }
  61. /// <summary>
  62. /// Gets called when a message is received. Updates the UI by adding
  63. /// the details into the log and scrolling the log if necessary.
  64. /// Note that subscription for messages needs to be redone.
  65. /// </summary>
  66. /// <param name="sender">ProximityDevice instance</param>
  67. /// <param name="message">A message to be handled.</param>
  68. private void MessageReceived(ProximityDevice sender, ProximityMessage message)
  69. {
  70. Dispatcher.BeginInvoke(() =>
  71. {
  72. if (device != null)
  73. {
  74. device.StopSubscribingForMessage(subscriptionId);
  75. }
  76. logText = logText + ParseNDEF(message);
  77. ScrollViewer.UpdateLayout();
  78. ScrollViewer.ScrollToVerticalOffset(0);
  79. SubscribeForMessage();
  80. });
  81. }
  82. /// <summary>
  83. /// Parses the details from the given message. The output is a string
  84. /// that can be appended into the log.
  85. /// </summary>
  86. /// <param name="message">The message to parse.</param>
  87. /// <returns>The parsed details as a string.</returns>
  88. private string ParseNDEF(ProximityMessage message) {
  89. var output = "";
  90. using (var buf = DataReader.FromBuffer(message.Data)) {
  91. NdefRecordUtility.ReadNdefRecord(buf,recordList);
  92. for (int i = 0, recordNumber = 0, spRecordNumber = 0; i < recordList.Count; i++)
  93. {
  94. NdefRecord record = recordList.ElementAt(i);
  95. if (!record.IsSpRecord )
  96. {
  97. if (System.Text.Encoding.UTF8.GetString(record.Type, 0, record.TypeLength) == "Sp")
  98. {
  99. output = output + "\n --End of Record No." + recordNumber; spRecordNumber = 0;
  100. continue;
  101. }
  102. else
  103. {
  104. recordNumber++;
  105. output = output + "\n --Record No." + recordNumber;
  106. }
  107. }
  108. else
  109. {
  110. if (spRecordNumber == 0)
  111. {
  112. recordNumber++;
  113. output = output + "\n --Record No." + recordNumber;
  114. }
  115. spRecordNumber++;
  116. output = output + "\n Sp sub-record No." + spRecordNumber;
  117. }
  118. output = output + "\n MB:" + ((record.Mb) ? "1;" : "0;");
  119. output = output + " ME:" + ((record.Me) ? "1;" : "0;");
  120. output = output + " CF:" + ((record.Cf) ? "1;" : "0;");
  121. output = output + " SR:" + ((record.Sr) ? "1;" : "0;");
  122. output = output + " IL:" + ((record.Il) ? "1;" : "0;");
  123. string typeName = NdefRecordUtility.GetTypeNameFormat(record);
  124. if (record.TypeLength > 0)
  125. {
  126. output = output + "\n Type: " + typeName + ":"
  127. + System.Text.Encoding.UTF8.GetString(record.Type, 0, record.TypeLength);
  128. }
  129. if ((record.Il) && (record.IdLength > 0))
  130. {
  131. output = output + "\n Id:"
  132. + System.Text.Encoding.UTF8.GetString(record.Id, 0, record.IdLength);
  133. }
  134. if ((record.PayloadLength > 0) && (record.Payload != null))
  135. {
  136. if ((record.Tnf == 0x01)
  137. && (System.Text.Encoding.UTF8.GetString(record.Type, 0, record.TypeLength) == "U"))
  138. {
  139. NdefUriRtd uri = new NdefUriRtd();
  140. NdefRecordUtility.ReadUriRtd(record, uri);
  141. output = output + "\n Uri: " + uri.GetFullUri();
  142. }
  143. else if ((record.Tnf == 0x01)
  144. && (System.Text.Encoding.UTF8.GetString(record.Type, 0, record.TypeLength) == "T"))
  145. {
  146. NdefTextRtd text = new NdefTextRtd();
  147. NdefRecordUtility.ReadTextRtd(record, text);
  148. output = output + "\n Language: " + text.Language;
  149. output = output + "\n Encoding: " + text.Encoding;
  150. output = output + "\n Text: " + text.Text;
  151. }
  152. else
  153. {
  154. if (record.Tnf==0x01)
  155. {
  156. output = output + "\n Payload:"
  157. + System.Text.Encoding.UTF8.GetString(record.Payload, 0, record.Payload.Length);
  158. }
  159. }
  160. }
  161. if (!record.IsSpRecord)
  162. {
  163. output = output + "\n --End of Record No." + recordNumber;
  164. }
  165. }
  166. }
  167. return output;
  168. }
  169. /// <summary>
  170. /// Subscribes for NDEF messages. This ensures that we get notified
  171. /// about the NFC events.
  172. /// </summary>
  173. private void SubscribeForMessage()
  174. {
  175. if (device != null)
  176. {
  177. recordList.Clear();
  178. subscriptionId = device.SubscribeForMessage("NDEF", MessageReceived);
  179. }
  180. }
  181. }
  182. }