-
Notifications
You must be signed in to change notification settings - Fork 559
/
NfcPlugin.cs
200 lines (167 loc) · 6.21 KB
/
NfcPlugin.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
using System.Runtime.Serialization;
using Windows.Networking.Proximity;
using WPCordovaClassLib.Cordova;
using WPCordovaClassLib.Cordova.Commands;
using WPCordovaClassLib.Cordova.JSON;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Storage.Streams;
using System.Diagnostics;
using System.IO;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using ChariotSolutions.NFC.NDEF;
//
// http://www.nfc-forum.org/specs/spec_list/
// http://msdn.microsoft.com/en-us/library/windows/apps/br241250.aspx
//
namespace Cordova.Extension.Commands
{
class NfcPlugin : BaseCommand
{
private ProximityDevice proximityDevice;
private long subscribedMessageId = -1;
private long publishedMessageId = -1;
public NfcPlugin()
{
Debug.WriteLine("Nfc Plugin");
proximityDevice = ProximityDevice.GetDefault();
if (proximityDevice == null) // shouldn't happen
{
Debug.WriteLine("WARNING: proximity device is null");
}
}
public void init(string args)
{
// not used for WP8
}
// no args
public void registerNdef(string args)
{
Debug.WriteLine("Registering for NDEF");
try
{
subscribedMessageId = proximityDevice.SubscribeForMessage("NDEF", MessageReceivedHandler);
DispatchCommandResult(new PluginResult(PluginResult.Status.OK));
}
catch (System.Exception e)
{
Debug.WriteLine(e);
DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, e.Message));
}
}
// no args
public void removeNdef(string args)
{
Debug.WriteLine("Removing NDEF");
try
{
if (subscribedMessageId != -1)
{
proximityDevice.StopSubscribingForMessage(subscribedMessageId);
subscribedMessageId = -1;
}
DispatchCommandResult(new PluginResult(PluginResult.Status.OK));
}
catch (System.Exception e)
{
Debug.WriteLine(e);
DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, e.Message));
}
}
// args[0] is a NdefMessage, which is a JSON array of NdefRecords
public void writeTag(string args)
{
Debug.WriteLine("Write Tag");
try
{
string ndefMessage = JsonHelper.Deserialize<string[]>(args)[0];
NdefRecord[] records = JsonHelper.Deserialize<NdefRecord[]>(ndefMessage);
byte[] data = Ndef.toBytes(records);
stopPublishing();
publishedMessageId = proximityDevice.PublishBinaryMessage("NDEF:WriteTag", data.AsBuffer(), nfcWriteTagCallback);
// send no result now, C# callback will send success after write
PluginResult result = new PluginResult(PluginResult.Status.NO_RESULT);
result.KeepCallback = true;
DispatchCommandResult(result);
}
catch (System.Exception e)
{
Debug.WriteLine(e);
DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, e.Message));
}
}
// args[0] is a NdefMessage, which is a JSON array of NdefRecords
public void shareTag(string args)
{
Debug.WriteLine("Share Tag");
try
{
string ndefMessage = JsonHelper.Deserialize<string[]>(args)[0];
NdefRecord[] records = JsonHelper.Deserialize<NdefRecord[]>(ndefMessage);
byte[] data = Ndef.toBytes(records);
stopPublishing();
publishedMessageId = proximityDevice.PublishBinaryMessage("NDEF", data.AsBuffer());
DispatchCommandResult(new PluginResult(PluginResult.Status.OK));
}
catch (System.Exception e)
{
Debug.WriteLine(e);
DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, e.Message));
}
}
// no args
public void unshareTag(string args)
{
Debug.WriteLine("Unshare Tag");
try
{
stopPublishing();
DispatchCommandResult(new PluginResult(PluginResult.Status.OK));
}
catch (System.Exception e)
{
Debug.WriteLine(e);
DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, e.Message));
}
}
private void stopPublishing()
{
if (publishedMessageId != -1)
{
proximityDevice.StopPublishingMessage(publishedMessageId);
publishedMessageId = -1;
}
}
// MessageTransmittedHandler called after the message is written to a tag
private void nfcWriteTagCallback(ProximityDevice sender, long messageId)
{
Debug.WriteLine("Successfully wrote message to the NFC tag.");
// only write the tag one time
stopPublishing();
DispatchCommandResult(new PluginResult(PluginResult.Status.OK));
}
private void MessageReceivedHandler(ProximityDevice sender, ProximityMessage message)
{
var bytes = message.Data.ToArray();
List<NdefRecord> records = Ndef.parse(bytes);
NfcTag tag = new NfcTag(records);
// calling a global js method to fire an nfc event
ScriptCallback script = new ScriptCallback("fireNfcTagEvent", new string[] { "ndef", JsonHelper.Serialize(tag) });
this.InvokeCustomScript(script, false);
}
[DataContract]
public class NfcTag
{
public NfcTag()
{
}
public NfcTag(List<NdefRecord> records)
{
ndefMessage = records;
}
[DataMember]
public List<NdefRecord> ndefMessage { get; set; }
}
}
}