Advertisement
Guest User

iOS interface

a guest
Mar 19th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.18 KB | None | 0 0
  1. using CoreFoundation;
  2. using CoreNFC;
  3. using Foundation;
  4. using Poz1.NFCForms.Abstract;
  5. using Poz1.NFCForms.iOS;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11.  
  12. [assembly: Xamarin.Forms.Dependency(typeof(NfcForms))]
  13. namespace Poz1.NFCForms.iOS
  14. {
  15. public class NfcForms : NSObject, INfcForms, INFCNdefReaderSessionDelegate
  16. {
  17. private NFCNdefReaderSession _session;
  18. public event EventHandler<NfcFormsTag> TagDetected;
  19. public void WriteTag(NdefLibrary.Ndef.NdefMessage message)
  20. {
  21. throw new NotImplementedException();
  22. }
  23.  
  24. public bool IsAvailable
  25. {
  26. get
  27. {
  28. return NFCNdefReaderSession.ReadingAvailable;
  29. }
  30. }
  31. //Events are declared to satisfy the interface. but they dont do anything
  32. public event EventHandler<NfcFormsTag> TagDisconnected;
  33. public event EventHandler<NfcFormsTag> TagConnected;
  34. public event EventHandler<NfcFormsTag> NewTag;
  35.  
  36. //actual method that does the reading, in the android part it returns an empty string as its not used by me, could be altered
  37. public async Task<string> ReadTag()
  38. {
  39. var reader = new NfcReader();
  40. var result = await reader.ScanAsync();
  41. if (result != null)
  42. {
  43. return result;
  44. }
  45. else
  46. {
  47. return "No messages found";
  48. }
  49. }
  50. public void DidInvalidate(NFCNdefReaderSession session, NSError error)
  51. {
  52. //never got thrown when i tested with 20+ different mifare ultralights
  53. //throw new NotImplementedException();
  54. }
  55.  
  56. public void DidDetect(NFCNdefReaderSession session, NFCNdefMessage[] messages)
  57. {
  58. var bytes = messages[0].Records[0].Payload.Skip(3).ToArray();
  59. var message = Encoding.UTF8.GetString(bytes);
  60. }
  61. }
  62. public class NfcReader : NSObject, INFCNdefReaderSessionDelegate
  63. {
  64. private NFCNdefReaderSession _session;
  65. private TaskCompletionSource<string> _tcs;
  66.  
  67. public Task<string> ScanAsync()
  68. {
  69. if (!NFCNdefReaderSession.ReadingAvailable)
  70. {
  71. throw new InvalidOperationException("Reading NDEF is not available");
  72. }
  73.  
  74. _tcs = new TaskCompletionSource<string>();
  75. _session = new NFCNdefReaderSession(this, null, true);
  76. _session?.BeginSession();
  77.  
  78. return _tcs.Task;
  79. }
  80.  
  81. public void DidInvalidate(NFCNdefReaderSession session, NSError error)
  82. {
  83. _tcs.TrySetException(new Exception(error?.LocalizedFailureReason));
  84. }
  85.  
  86. public void DidDetect(NFCNdefReaderSession session, NFCNdefMessage[] messages)
  87. {
  88. var bytes = messages[0].Records[0].Payload.Skip(3).ToArray();
  89. var message = Encoding.UTF8.GetString(bytes);
  90. _tcs.SetResult(message);
  91. }
  92. }
  93.  
  94.  
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement