-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathNfcForms.cs
240 lines (191 loc) · 5.2 KB
/
NfcForms.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
using Android.Content;
using Android.Nfc;
using Android.Nfc.Tech;
using Poz1.NFCForms.Abstract;
using Poz1.NFCForms.Droid;
using System;
using System.Collections.Generic;
using System.IO;
[assembly: Xamarin.Forms.Dependency (typeof (NfcForms))]
namespace Poz1.NFCForms.Droid
{
#region Android NFC Techs
public sealed class NFCTechs
{
public const string IsoDep = "android.nfc.tech.IsoDep";
public const string NfcA = "android.nfc.tech.NfcA";
public const string NfcB = "android.nfc.tech.NfcB";
public const string NfcF = "android.nfc.tech.NfcF";
public const string NfcV = "android.nfc.tech.NfcV";
public const string Ndef = "android.nfc.tech.Ndef";
public const string NdefFormatable = "android.nfc.tech.NdefFormatable";
public const string MifareClassic = "android.nfc.tech.MifareClassic";
public const string MifareUltralight = "android.nfc.tech.MifareUltralight";
}
#endregion
public class NfcForms : INfcForms
{
#region Private Variables
private NfcAdapter nfcDevice;
private NfcFormsTag nfcTag;
private Tag droidTag;
#endregion
#region Properties
public bool IsAvailable
{
get
{
return nfcDevice.IsEnabled;
}
}
#endregion
#region Constructors
public NfcForms ()
{
NfcManager NfcManager = (NfcManager)Android.App.Application.Context.GetSystemService(Context.NfcService);
nfcDevice = NfcManager.DefaultAdapter;
nfcTag = new NfcFormsTag ();
}
#endregion
#region Private Methods
private Ndef GetNdef(Tag tag)
{
Ndef ndef = Ndef.Get(tag);
if (ndef == null)
return null;
else
return ndef;
}
private NdefLibrary.Ndef.NdefMessage ReadNdef(Ndef ndef)
{
if (ndef == null || ndef.CachedNdefMessage == null)
{
return null;
}
var bytes = ndef.CachedNdefMessage.ToByteArray();
var message = NdefLibrary.Ndef.NdefMessage.FromByteArray(bytes);
return message;
}
#endregion
#region Public Methods
public void OnNewIntent (object sender, Intent e)
{
droidTag = e.GetParcelableExtra(NfcAdapter.ExtraTag) as Tag;
if (droidTag != null)
{
nfcTag.TechList = new List<string>(droidTag.GetTechList());
nfcTag.Id = droidTag.GetId();
if (GetNdef (droidTag) == null)
{
nfcTag.IsNdefSupported = false;
}
else
{
nfcTag.IsNdefSupported = true;
Ndef ndef = GetNdef (droidTag);
nfcTag.NdefMessage = ReadNdef (ndef);
nfcTag.IsWriteable = ndef.IsWritable;
nfcTag.MaxSize = ndef.MaxSize;
}
RaiseNewTag(nfcTag);
}
}
public void WriteTag (NdefLibrary.Ndef.NdefMessage message)
{
if (droidTag == null)
{
throw new Exception("Tag Error: No Tag to write, register to NewTag event before calling WriteTag()");
}
Ndef ndef = GetNdef (droidTag);
if (ndef == null)
{
throw new Exception("Tag Error: NDEF not supported");
}
try
{
ndef.Connect();
RaiseTagConnected (nfcTag);
}
catch
{
throw new Exception("Tag Error: No Tag nearby");
}
if(!ndef.IsWritable)
{
ndef.Close ();
throw new Exception("Tag Error: Tag is write locked");
}
int size = message.ToByteArray ().Length;
if(ndef.MaxSize < size)
{
ndef.Close ();
throw new Exception("Tag Error: Tag is too small");
}
try
{
List<Android.Nfc.NdefRecord> records = new List<Android.Nfc.NdefRecord>();
for(int i = 0; i< message.Count;i++)
{
if(message[i].CheckIfValid())
records.Add(new Android.Nfc.NdefRecord(Android.Nfc.NdefRecord.TnfWellKnown,message[i].Type,message[i].Id,message[i].Payload));
else
{
throw new Exception("NDEFRecord number " + i + "is not valid");
}
};
Android.Nfc.NdefMessage msg = new Android.Nfc.NdefMessage(records.ToArray());
ndef.WriteNdefMessage(msg);
}
catch (TagLostException tle)
{
throw new Exception("Tag Lost Error: " + tle.Message);
}
catch (IOException ioe)
{
throw new Exception("Tag IO Error: " + ioe.ToString());
}
catch (Android.Nfc.FormatException fe)
{
throw new Exception("Tag Format Error: " + fe.Message);
}
catch (Exception e)
{
throw new Exception("Tag Error: " + e.ToString());
}
finally
{
ndef.Close ();
RaiseTagTagDisconnected (nfcTag);
}
}
#endregion
#region Events
public event EventHandler<NfcFormsTag> TagConnected;
public void RaiseTagConnected(NfcFormsTag tag)
{
nfcTag.IsConnected = true;
if (TagConnected != null)
{
TagConnected(this, tag);
}
}
public event EventHandler<NfcFormsTag> TagDisconnected;
public void RaiseTagTagDisconnected(NfcFormsTag tag)
{
nfcTag.IsConnected = false;
if (TagDisconnected != null)
{
TagDisconnected(this, tag);
}
}
public event EventHandler<NfcFormsTag> NewTag;
public void RaiseNewTag(NfcFormsTag tag)
{
if (NewTag != null)
{
NewTag(this, tag);
}
}
#endregion
}
}