@iotize/device-com-nfc.cordova 3.8.1 → 3.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (74) hide show
  1. package/LICENSE +30 -30
  2. package/README.md +673 -673
  3. package/bundles/iotize-device-com-nfc.cordova.umd.js +1108 -967
  4. package/bundles/iotize-device-com-nfc.cordova.umd.js.map +1 -1
  5. package/bundles/iotize-device-com-nfc.cordova.umd.min.js +1 -1
  6. package/bundles/iotize-device-com-nfc.cordova.umd.min.js.map +1 -1
  7. package/esm2015/iotize-device-com-nfc.cordova.js +4 -4
  8. package/esm2015/iotize-device-com-nfc.cordova.ngsummary.json +1 -1
  9. package/esm2015/public_api.js +1 -1
  10. package/esm2015/public_api.ngsummary.json +1 -1
  11. package/esm2015/www/cordova-interface.js +1 -1
  12. package/esm2015/www/cordova-interface.js.map +1 -1
  13. package/esm2015/www/errors.js +30 -30
  14. package/esm2015/www/errors.js.map +1 -1
  15. package/esm2015/www/index.js +6 -6
  16. package/esm2015/www/index.js.map +1 -1
  17. package/esm2015/www/index.metadata.json +1 -1
  18. package/esm2015/www/index.ngsummary.json +1 -1
  19. package/esm2015/www/logger.js +2 -2
  20. package/esm2015/www/logger.js.map +1 -1
  21. package/esm2015/www/ndef/definitions.js +16 -16
  22. package/esm2015/www/ndef/definitions.js.map +1 -1
  23. package/esm2015/www/ndef/parse-ndef-message.js +293 -164
  24. package/esm2015/www/ndef/parse-ndef-message.js.map +1 -1
  25. package/esm2015/www/ndef/parse-ndef-message.metadata.json +1 -1
  26. package/esm2015/www/ndef/parse-ndef-message.ngsummary.json +1 -1
  27. package/esm2015/www/nfc-com-protocol.js +169 -169
  28. package/esm2015/www/nfc-com-protocol.js.map +1 -1
  29. package/esm2015/www/tap-ndef/definitions.js +1 -1
  30. package/esm2015/www/tap-ndef/definitions.js.map +1 -1
  31. package/esm2015/www/tap-ndef/index.js +2 -2
  32. package/esm2015/www/tap-ndef/index.js.map +1 -1
  33. package/esm2015/www/tap-ndef/parse-ndef-message.js +51 -51
  34. package/esm2015/www/tap-ndef/parse-ndef-message.js.map +1 -1
  35. package/esm2015/www/utility.js +10 -10
  36. package/esm2015/www/utility.js.map +1 -1
  37. package/esm2015/www/utility.ngsummary.json +1 -1
  38. package/fesm2015/iotize-device-com-nfc.cordova.js +555 -427
  39. package/fesm2015/iotize-device-com-nfc.cordova.js.map +1 -1
  40. package/iotize-device-com-nfc.cordova.d.ts +4 -4
  41. package/iotize-device-com-nfc.cordova.metadata.json +1 -1
  42. package/package.json +2 -2
  43. package/plugin.xml +98 -98
  44. package/public_api.d.ts +1 -1
  45. package/src/android/.gradle/4.10.1/fileChanges/last-build.bin +0 -0
  46. package/src/android/.gradle/4.10.1/fileHashes/fileHashes.bin +0 -0
  47. package/src/android/.gradle/4.10.1/fileHashes/fileHashes.lock +0 -0
  48. package/src/android/.gradle/4.10.1/gc.properties +0 -0
  49. package/src/android/build.gradle +38 -38
  50. package/src/android/gradle/wrapper/gradle-wrapper.properties +6 -6
  51. package/src/android/gradlew +172 -172
  52. package/src/android/gradlew.bat +84 -84
  53. package/src/android/local.properties +8 -8
  54. package/src/android/src/com/chariotsolutions/nfc/plugin/JSONBuilder.java +60 -60
  55. package/src/android/src/com/chariotsolutions/nfc/plugin/NfcPlugin.java +1151 -1151
  56. package/src/android/src/com/chariotsolutions/nfc/plugin/NfcPluginError.java +15 -15
  57. package/src/android/src/com/chariotsolutions/nfc/plugin/PluginResponse.java +85 -85
  58. package/src/android/src/com/chariotsolutions/nfc/plugin/Util.java +140 -140
  59. package/src/ios/AppDelegate+NFC.swift +51 -51
  60. package/src/ios/NFCHelpers.swift +144 -144
  61. package/src/ios/NFCNDEFDelegate.swift +78 -78
  62. package/src/ios/NFCTagReader.swift +506 -506
  63. package/www/cordova-interface.d.ts +34 -34
  64. package/www/errors.d.ts +17 -17
  65. package/www/index.d.ts +6 -6
  66. package/www/logger.d.ts +2 -2
  67. package/www/ndef/definitions.d.ts +15 -15
  68. package/www/ndef/parse-ndef-message.d.ts +12 -69
  69. package/www/nfc-com-protocol.d.ts +36 -36
  70. package/www/phonegap-nfc.js +911 -911
  71. package/www/tap-ndef/definitions.d.ts +25 -25
  72. package/www/tap-ndef/index.d.ts +2 -2
  73. package/www/tap-ndef/parse-ndef-message.d.ts +6 -6
  74. package/www/utility.d.ts +2 -2
@@ -1,450 +1,578 @@
1
1
  import { Buffer } from 'buffer';
2
- import { bufferToAsciiString, bufferToHexString, hexStringToBuffer } from '@iotize/common/byte-converter';
2
+ import { bufferToAsciiString, asciiStringToByteBuffer, bufferToHexString, hexStringToBuffer } from '@iotize/common/byte-converter';
3
3
  import { ConnectionState } from '@iotize/tap/protocol/api';
4
4
  import { QueueComProtocol } from '@iotize/tap/protocol/core';
5
5
  import { defer } from 'rxjs';
6
6
  import { createDebugger } from '@iotize/common/debug';
7
7
 
8
- class NfcError extends Error {
9
- constructor(code, message) {
10
- super(message);
11
- this.code = code;
12
- }
13
- static tagLostError() {
14
- return new NfcError(NfcError.ErrorCode.TagLostError, 'NFC tag lost');
15
- }
16
- static notConnectedError() {
17
- return new NfcError(NfcError.ErrorCode.NotConnectedError, 'NFC tag is not connected');
18
- }
19
- static unknownError(errString) {
20
- throw new NfcError(NfcError.ErrorCode.Unknown, errString);
21
- }
22
- static tagConnectionFailed() {
23
- throw new NfcError(NfcError.ErrorCode.NotConnectedError, `Tag connection failed`);
24
- }
25
- static internalError(message) {
26
- throw new NfcError(NfcError.ErrorCode.InternalError, message);
27
- }
28
- }
29
- (function (NfcError) {
30
- let ErrorCode;
31
- (function (ErrorCode) {
32
- ErrorCode["Unknown"] = "NfcUnknownError";
33
- ErrorCode["InternalError"] = "NfcInternalError";
34
- ErrorCode["TagLostError"] = "NfcTagLostError";
35
- ErrorCode["NotConnectedError"] = "NfcNotConnectedError";
36
- })(ErrorCode = NfcError.ErrorCode || (NfcError.ErrorCode = {}));
8
+ class NfcError extends Error {
9
+ constructor(code, message) {
10
+ super(message);
11
+ this.code = code;
12
+ }
13
+ static tagLostError() {
14
+ return new NfcError(NfcError.ErrorCode.TagLostError, 'NFC tag lost');
15
+ }
16
+ static notConnectedError() {
17
+ return new NfcError(NfcError.ErrorCode.NotConnectedError, 'NFC tag is not connected');
18
+ }
19
+ static unknownError(errString) {
20
+ throw new NfcError(NfcError.ErrorCode.Unknown, errString);
21
+ }
22
+ static tagConnectionFailed() {
23
+ throw new NfcError(NfcError.ErrorCode.NotConnectedError, `Tag connection failed`);
24
+ }
25
+ static internalError(message) {
26
+ throw new NfcError(NfcError.ErrorCode.InternalError, message);
27
+ }
28
+ }
29
+ (function (NfcError) {
30
+ let ErrorCode;
31
+ (function (ErrorCode) {
32
+ ErrorCode["Unknown"] = "NfcUnknownError";
33
+ ErrorCode["InternalError"] = "NfcInternalError";
34
+ ErrorCode["TagLostError"] = "NfcTagLostError";
35
+ ErrorCode["NotConnectedError"] = "NfcNotConnectedError";
36
+ })(ErrorCode = NfcError.ErrorCode || (NfcError.ErrorCode = {}));
37
37
  })(NfcError || (NfcError = {}));
38
38
 
39
- var TNF;
40
- (function (TNF) {
41
- TNF[TNF["EMPTY"] = 0] = "EMPTY";
42
- TNF[TNF["WELL_KNOWN_TYPE"] = 1] = "WELL_KNOWN_TYPE";
43
- TNF[TNF["MIME_TYPE"] = 2] = "MIME_TYPE";
44
- TNF[TNF["ABSOLUTE_URI"] = 3] = "ABSOLUTE_URI";
45
- TNF[TNF["EXTERNAL"] = 4] = "EXTERNAL";
46
- TNF[TNF["UNKNOWN"] = 5] = "UNKNOWN";
47
- TNF[TNF["UNCHANGED"] = 6] = "UNCHANGED";
48
- TNF[TNF["RFU"] = 7] = "RFU";
49
- })(TNF || (TNF = {}));
50
- const WellKnownType = {
51
- URI: [0x55],
52
- TEXT: [0x54],
53
- SMPART_POSTER: [0x53, 0x70],
39
+ var TNF;
40
+ (function (TNF) {
41
+ TNF[TNF["EMPTY"] = 0] = "EMPTY";
42
+ TNF[TNF["WELL_KNOWN_TYPE"] = 1] = "WELL_KNOWN_TYPE";
43
+ TNF[TNF["MIME_TYPE"] = 2] = "MIME_TYPE";
44
+ TNF[TNF["ABSOLUTE_URI"] = 3] = "ABSOLUTE_URI";
45
+ TNF[TNF["EXTERNAL"] = 4] = "EXTERNAL";
46
+ TNF[TNF["UNKNOWN"] = 5] = "UNKNOWN";
47
+ TNF[TNF["UNCHANGED"] = 6] = "UNCHANGED";
48
+ TNF[TNF["RFU"] = 7] = "RFU";
49
+ })(TNF || (TNF = {}));
50
+ const WellKnownType = {
51
+ URI: [0x55],
52
+ TEXT: [0x54],
53
+ SMPART_POSTER: [0x53, 0x70],
54
54
  };
55
55
 
56
- function toAsciiString(payload) {
57
- if (payload.length > 0 && payload[0] === 0) {
58
- payload = payload.slice(1);
59
- }
60
- return bufferToAsciiString(payload);
61
- }
62
- function arrayEquals(a1, a2) {
63
- return (a1.length === a2.length && a1.every((a1Item, index) => a1Item === a2[index]));
56
+ function toAsciiString(payload) {
57
+ if (payload.length > 0 && payload[0] === 0) {
58
+ payload = payload.slice(1);
59
+ }
60
+ return bufferToAsciiString(payload);
61
+ }
62
+ function arrayEquals(a1, a2) {
63
+ return (a1.length === a2.length && a1.every((a1Item, index) => a1Item === a2[index]));
64
64
  }
65
65
 
66
- function parseNdefMessages(messages) {
67
- return (messages || []).map(parseNdefMessage);
68
- }
69
- function parseNdefMessage(message) {
70
- switch (message.tnf) {
71
- case TNF.ABSOLUTE_URI:
72
- return {
73
- tnf: 'uri',
74
- value: toAsciiString(message.payload),
75
- };
76
- case TNF.WELL_KNOWN_TYPE:
77
- return {
78
- tnf: 'wkt',
79
- value: parseWellKnownType(message),
80
- };
81
- case TNF.EMPTY:
82
- return {
83
- tnf: 'empty',
84
- value: {
85
- type: message.type,
86
- payload: message.payload,
87
- },
88
- };
89
- case TNF.EXTERNAL:
90
- return {
91
- tnf: 'external',
92
- value: paseExternalType(message),
93
- };
94
- case TNF.MIME_TYPE:
95
- return {
96
- tnf: 'mimetype',
97
- value: parseMimeType(message),
98
- };
99
- case TNF.RFU:
100
- return {
101
- tnf: 'rfu',
102
- value: {
103
- type: message.type,
104
- payload: message.payload,
105
- },
106
- };
107
- case TNF.UNCHANGED:
108
- return {
109
- tnf: 'unchanged',
110
- value: {
111
- type: message.type,
112
- payload: message.payload,
113
- },
114
- };
115
- case TNF.UNKNOWN:
116
- return {
117
- tnf: 'unknown',
118
- value: {
119
- type: message.type,
120
- payload: message.payload,
121
- },
122
- };
123
- default:
124
- return {
125
- tnf: message.tnf,
126
- value: {
127
- type: message.type,
128
- payload: message.payload,
129
- },
130
- };
131
- }
132
- }
133
- function parseWellKnownType(message) {
134
- if (arrayEquals(message.type, WellKnownType.URI)) {
135
- const prefixCode = message.payload[0];
136
- let prefix = WELL_KNOWN_TYPE_URI_PREFIX[prefixCode] || '';
137
- return {
138
- type: 'uri',
139
- value: prefix + toAsciiString(message.payload.slice(1)),
140
- };
141
- }
142
- else if (arrayEquals(message.type, WellKnownType.TEXT)) {
143
- const encodingNumber = message.payload[0];
144
- let encoding = 'utf8';
145
- let languageSizeInBytes = 0;
146
- if (typeof encodingNumber === 'number') {
147
- // b0: encodage utf8 s’il vaut 0 et utf16 s’il vaut 1.
148
- // b1: RFU (set to 0)
149
- // b2-b7: la taille en octets de la langue (n)
150
- // B8-B8+n-1: language
151
- // Bn: message
152
- const b0 = encodingNumber & 0b10000000;
153
- encoding = b0 ? 'utf16le' : 'utf8';
154
- languageSizeInBytes = encodingNumber & 0b00111111;
155
- }
156
- const text = message.payload.slice(languageSizeInBytes + 1);
157
- return {
158
- type: 'text',
159
- value: Buffer.from(text).toString(encoding),
160
- };
161
- }
162
- // else if (arrayEquals(message.type, WellKnownType.SMPART_POSTER)) {
163
- // return {
164
- // type: 'sp',
165
- // // TODO implement
166
- // payload: message.payload,
167
- // };
168
- // }
169
- else {
170
- return {
171
- type: 'unknown',
172
- value: {
173
- type: message.type,
174
- payload: message.payload,
175
- },
176
- };
177
- }
178
- }
179
- function paseExternalType(message) {
180
- const value = Buffer.from(message.payload).toString('utf8');
181
- const domain = Buffer.from(message.type).toString('utf8');
182
- return {
183
- domain,
184
- value,
185
- };
186
- }
187
- const WELL_KNOWN_TYPE_URI_PREFIX = {
188
- 0x0: '',
189
- 0x1: 'http://www.',
190
- 0x2: 'http://www.',
191
- 0x3: 'http://',
192
- 0x4: 'https://',
193
- 0x5: 'tel:',
194
- 0x6: 'mailto:',
195
- 0x07: 'ftp://anonymous:anonymous@',
196
- 0x1d: 'file://',
197
- 0x08: 'ftp://ftp.',
198
- 0x09: 'ftps://',
199
- 0x0a: 'sftp://',
200
- 0x0b: 'smb://',
201
- 0x0c: 'nfs://',
202
- 0x0d: 'ftp://',
203
- 0x0e: 'dav://',
204
- 0x0f: 'news:',
205
- 0x10: 'telnet://',
206
- 0x11: 'imap:',
207
- 0x12: 'rtsp://',
208
- 0x13: 'urn:',
209
- 0x14: 'pop:',
210
- 0x15: 'sip:',
211
- 0x16: 'sips:',
212
- 0x17: 'tftp:',
213
- 0x18: 'btspp://',
214
- 0x19: 'btl2cap://',
215
- 0x1a: 'btgoep://',
216
- 0x1b: 'tcpobex://',
217
- 0x1c: 'irdaobex://',
218
- 0x1e: 'urn:epc:id:',
219
- 0x1f: 'urn:epc:tag:',
220
- 0x20: 'urn:epc:pat:',
221
- 0x21: 'urn:epc:raw:',
222
- 0x22: 'urn:epc:',
223
- };
224
- function parseMimeType(message) {
225
- return Buffer.from(message.payload).toString('utf8');
66
+ function parseNdefMessages(messages) {
67
+ return (messages || []).map(parseNdefMessage);
68
+ }
69
+ function parseNdefMessage(message) {
70
+ switch (message.tnf) {
71
+ case TNF.ABSOLUTE_URI:
72
+ return {
73
+ id: message.id,
74
+ type: message.type,
75
+ tnf: 'uri',
76
+ value: toAsciiString(message.payload),
77
+ };
78
+ case TNF.WELL_KNOWN_TYPE:
79
+ return {
80
+ id: message.id,
81
+ type: message.type,
82
+ tnf: 'wkt',
83
+ value: parseWellKnownType(message),
84
+ };
85
+ case TNF.EMPTY:
86
+ return {
87
+ id: message.id,
88
+ type: message.type,
89
+ tnf: 'empty',
90
+ value: {
91
+ type: message.type,
92
+ payload: message.payload,
93
+ },
94
+ };
95
+ case TNF.EXTERNAL:
96
+ return {
97
+ id: message.id,
98
+ type: message.type,
99
+ tnf: 'external',
100
+ value: paseExternalType(message),
101
+ };
102
+ case TNF.MIME_TYPE:
103
+ return {
104
+ id: message.id,
105
+ type: message.type,
106
+ tnf: 'mimetype',
107
+ value: parseMimeType(message),
108
+ };
109
+ case TNF.RFU:
110
+ return {
111
+ id: message.id,
112
+ type: message.type,
113
+ tnf: 'rfu',
114
+ value: {
115
+ type: message.type,
116
+ payload: message.payload,
117
+ },
118
+ };
119
+ case TNF.UNCHANGED:
120
+ return {
121
+ id: message.id,
122
+ type: message.type,
123
+ tnf: 'unchanged',
124
+ value: {
125
+ type: message.type,
126
+ payload: message.payload,
127
+ },
128
+ };
129
+ case TNF.UNKNOWN:
130
+ return {
131
+ id: message.id,
132
+ type: message.type,
133
+ tnf: 'unknown',
134
+ value: {
135
+ type: message.type,
136
+ payload: message.payload,
137
+ },
138
+ };
139
+ default:
140
+ return {
141
+ id: message.id,
142
+ type: message.type,
143
+ tnf: message.tnf,
144
+ value: {
145
+ type: message.type,
146
+ payload: message.payload,
147
+ },
148
+ };
149
+ }
150
+ }
151
+ function parseWellKnownType(message) {
152
+ if (arrayEquals(message.type.slice(), WellKnownType.URI)) {
153
+ const prefixCode = message.payload[0];
154
+ let prefix = WELL_KNOWN_TYPE_URI_PREFIX[prefixCode] || '';
155
+ return {
156
+ type: 'uri',
157
+ value: prefix + toAsciiString(message.payload.slice(1)),
158
+ };
159
+ }
160
+ else if (arrayEquals(message.type.slice(), WellKnownType.TEXT)) {
161
+ const encodingNumber = message.payload[0];
162
+ let encoding = 'utf8';
163
+ let languageSizeInBytes = 0;
164
+ if (typeof encodingNumber === 'number') {
165
+ // b0: encodage utf8 s’il vaut 0 et utf16 s’il vaut 1.
166
+ // b1: RFU (set to 0)
167
+ // b2-b7: la taille en octets de la langue (n)
168
+ // B8-B8+n-1: language
169
+ // Bn: message
170
+ const b0 = encodingNumber & 0b10000000;
171
+ encoding = b0 ? 'utf16le' : 'utf8';
172
+ languageSizeInBytes = encodingNumber & 0b00111111;
173
+ }
174
+ const text = message.payload.slice(languageSizeInBytes + 1);
175
+ const language = message.payload.slice(8, languageSizeInBytes + 1);
176
+ return {
177
+ type: 'text',
178
+ value: Buffer.from(text).toString(encoding),
179
+ encoding,
180
+ language: Buffer.from(language).toString(encoding)
181
+ };
182
+ }
183
+ // else if (arrayEquals(message.type.slice(), WellKnownType.SMPART_POSTER)) {
184
+ // return {
185
+ // type: 'sp',
186
+ // // TODO implement
187
+ // payload: message.payload,
188
+ // };
189
+ // }
190
+ else {
191
+ return {
192
+ type: 'unknown',
193
+ value: {
194
+ type: message.type.slice(),
195
+ payload: message.payload,
196
+ },
197
+ };
198
+ }
199
+ }
200
+ function paseExternalType(message) {
201
+ const value = Buffer.from(message.payload).toString('utf8');
202
+ const domain = Buffer.from(message.type).toString('utf8');
203
+ return {
204
+ domain,
205
+ value,
206
+ };
207
+ }
208
+ const WELL_KNOWN_TYPE_URI_PREFIX = {
209
+ 0x0: '',
210
+ 0x1: 'http://www.',
211
+ 0x2: 'http://www.',
212
+ 0x3: 'http://',
213
+ 0x4: 'https://',
214
+ 0x5: 'tel:',
215
+ 0x6: 'mailto:',
216
+ 0x07: 'ftp://anonymous:anonymous@',
217
+ 0x1d: 'file://',
218
+ 0x08: 'ftp://ftp.',
219
+ 0x09: 'ftps://',
220
+ 0x0a: 'sftp://',
221
+ 0x0b: 'smb://',
222
+ 0x0c: 'nfs://',
223
+ 0x0d: 'ftp://',
224
+ 0x0e: 'dav://',
225
+ 0x0f: 'news:',
226
+ 0x10: 'telnet://',
227
+ 0x11: 'imap:',
228
+ 0x12: 'rtsp://',
229
+ 0x13: 'urn:',
230
+ 0x14: 'pop:',
231
+ 0x15: 'sip:',
232
+ 0x16: 'sips:',
233
+ 0x17: 'tftp:',
234
+ 0x18: 'btspp://',
235
+ 0x19: 'btl2cap://',
236
+ 0x1a: 'btgoep://',
237
+ 0x1b: 'tcpobex://',
238
+ 0x1c: 'irdaobex://',
239
+ 0x1e: 'urn:epc:id:',
240
+ 0x1f: 'urn:epc:tag:',
241
+ 0x20: 'urn:epc:pat:',
242
+ 0x21: 'urn:epc:raw:',
243
+ 0x22: 'urn:epc:',
244
+ };
245
+ function parseMimeType(message) {
246
+ return Buffer.from(message.payload).toString('utf8');
247
+ }
248
+ function encodeWellKnownType(message) {
249
+ if (message.value.type == 'uri') {
250
+ //identify the prefix
251
+ const prefixesEntries = Object.entries(WELL_KNOWN_TYPE_URI_PREFIX);
252
+ let prefixByte = 0;
253
+ for (const [id, prefix] of prefixesEntries) {
254
+ if (id == '0x0') // Skip first as it is always recognized. (what about 0x1 vs 0x2?)
255
+ continue;
256
+ if (message.value.value.startsWith(prefix)) {
257
+ prefixByte = parseInt(id, 16);
258
+ break;
259
+ }
260
+ }
261
+ const lengthToSlice = WELL_KNOWN_TYPE_URI_PREFIX[prefixByte].length;
262
+ const slicedString = message.value.value.slice(lengthToSlice);
263
+ return [prefixByte, ...(asciiStringToByteBuffer(slicedString))];
264
+ }
265
+ else if (message.value.type === 'text') {
266
+ // b1: RFU (set to 0)
267
+ // b2-b7: la taille en octets de la langue (n)
268
+ // B8-B8+n-1: language
269
+ // Bn: message
270
+ let firstByte = message.value.encoding == 'utf16le' ? 0b10000000 : 0;
271
+ firstByte &= (message.value.language & 0b00111111);
272
+ const languageBytes = Buffer.from(message.value.language, message.value.encoding);
273
+ const textBytes = Buffer.from(message.value.value, message.value.encoding);
274
+ return [firstByte, ...languageBytes, ...textBytes];
275
+ }
276
+ else
277
+ return [...message.value.type, ...message.value.payload];
278
+ }
279
+ function encodeExternalType(message) {
280
+ return Array.from(Buffer.from(message.value.value, 'utf8'));
281
+ }
282
+ function encodeMimeType(message) {
283
+ return Array.from(Buffer.from(message.value, 'utf8'));
284
+ }
285
+ function encodeNDEFMessages(ndefRecords) {
286
+ return (ndefRecords || []).map(encodeNDEFMessage);
287
+ }
288
+ function encodeNDEFMessage(ndefRecord) {
289
+ switch (ndefRecord.tnf) {
290
+ case 'uri':
291
+ return {
292
+ id: ndefRecord.id,
293
+ type: ndefRecord.type,
294
+ tnf: TNF.ABSOLUTE_URI,
295
+ payload: Array.from(asciiStringToByteBuffer(ndefRecord.value))
296
+ };
297
+ case 'wkt':
298
+ return {
299
+ id: ndefRecord.id,
300
+ type: ndefRecord.type,
301
+ tnf: TNF.WELL_KNOWN_TYPE,
302
+ payload: encodeWellKnownType(ndefRecord),
303
+ };
304
+ case 'empty':
305
+ return {
306
+ id: ndefRecord.id,
307
+ type: ndefRecord.type,
308
+ tnf: TNF.EMPTY,
309
+ payload: ndefRecord.payload,
310
+ };
311
+ case 'external':
312
+ return {
313
+ id: ndefRecord.id,
314
+ type: ndefRecord.type,
315
+ tnf: TNF.EXTERNAL,
316
+ payload: encodeExternalType(ndefRecord),
317
+ };
318
+ case 'mimetype':
319
+ return {
320
+ id: ndefRecord.id,
321
+ type: ndefRecord.type,
322
+ tnf: TNF.MIME_TYPE,
323
+ payload: encodeMimeType(ndefRecord),
324
+ };
325
+ case 'rfu':
326
+ return {
327
+ id: ndefRecord.id,
328
+ type: ndefRecord.type,
329
+ tnf: TNF.RFU,
330
+ payload: ndefRecord.payload
331
+ };
332
+ case 'unchanged':
333
+ return {
334
+ id: ndefRecord.id,
335
+ type: ndefRecord.type,
336
+ tnf: TNF.UNCHANGED,
337
+ payload: ndefRecord.payload,
338
+ };
339
+ case 'unknown':
340
+ return {
341
+ id: ndefRecord.id,
342
+ type: ndefRecord.type,
343
+ tnf: TNF.UNKNOWN,
344
+ payload: ndefRecord.payload,
345
+ };
346
+ default:
347
+ return {
348
+ id: ndefRecord.id,
349
+ type: ndefRecord.type,
350
+ tnf: Number(ndefRecord.tnf),
351
+ payload: ndefRecord.payload,
352
+ };
353
+ }
226
354
  }
227
355
 
228
356
  const debug = createDebugger(`@iotize/device-com-nfc.cordova`);
229
357
 
230
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
231
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
232
- return new (P || (P = Promise))(function (resolve, reject) {
233
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
234
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
235
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
236
- step((generator = generator.apply(thisArg, _arguments || [])).next());
237
- });
238
- };
239
- class PromiseQueue {
240
- constructor() {
241
- this.queue = Promise.resolve(true);
242
- }
243
- add(operation) {
244
- return new Promise((resolve, reject) => {
245
- this.queue = this.queue.then(operation).then(resolve).catch(reject);
246
- });
247
- }
248
- }
249
- class NFCComProtocol extends QueueComProtocol {
250
- constructor(options = {
251
- connect: {
252
- timeout: 2000,
253
- },
254
- disconnect: {
255
- timeout: 1000,
256
- },
257
- send: {
258
- timeout: 1000,
259
- },
260
- }) {
261
- super();
262
- this._sendQueue = new PromiseQueue();
263
- this.options = options;
264
- if (typeof nfc == undefined) {
265
- console.warn('NFC plugin has not been setup properly. Global variable NFC does not exist');
266
- }
267
- }
268
- static iOSProtocol() {
269
- return new NFCComProtocol({
270
- connect: {
271
- timeout: 10000, // bigger timer on connect as connect launches a reading session
272
- },
273
- disconnect: {
274
- timeout: 1000,
275
- },
276
- send: {
277
- timeout: 1000,
278
- },
279
- });
280
- }
281
- /**
282
- * We force tag connection with nfc as we need to refresh tag
283
- * @param options
284
- * @returns
285
- */
286
- connect(options) {
287
- this.connectionState = ConnectionState.CONNECTING; // Hack to force NFC tag connect call even if we are already connected
288
- return super.connect(options);
289
- }
290
- /**
291
- * Not used as we have rewrote "connect()" function
292
- * @param options
293
- * @returns
294
- */
295
- _connect(options) {
296
- return defer(() => __awaiter(this, void 0, void 0, function* () {
297
- debug('_connect', options);
298
- try {
299
- yield nfc.connect('android.nfc.tech.NfcV', this.options.connect.timeout);
300
- }
301
- catch (err) {
302
- if (typeof err === 'string') {
303
- if (err === 'Tag connection failed') {
304
- throw NfcError.tagConnectionFailed();
305
- }
306
- else {
307
- throw NfcError.unknownError(err);
308
- }
309
- }
310
- throw err;
311
- }
312
- }));
313
- }
314
- _disconnect(options) {
315
- return defer(() => __awaiter(this, void 0, void 0, function* () {
316
- yield nfc.close();
317
- this._sendQueue = new PromiseQueue();
318
- }));
319
- }
320
- /**
321
- * Not used
322
- * @param options
323
- * @returns
324
- */
325
- write(data) {
326
- return __awaiter(this, void 0, void 0, function* () {
327
- throw new Error('Method not implemented.');
328
- });
329
- }
330
- /**
331
- * Not used
332
- * @param options
333
- * @returns
334
- */
335
- read() {
336
- return __awaiter(this, void 0, void 0, function* () {
337
- throw new Error('Method not implemented.');
338
- });
339
- }
340
- send(data, options) {
341
- return defer(() => __awaiter(this, void 0, void 0, function* () {
342
- return yield this._sendQueue.add(() => __awaiter(this, void 0, void 0, function* () {
343
- try {
344
- const response = yield nfc.transceiveTap(bufferToHexString(data));
345
- if (typeof response != 'string') {
346
- throw NfcError.internalError(`Internal error. Plugin should respond a hexadecimal string`);
347
- }
348
- debug('NFC plugin response: ', response);
349
- return hexStringToBuffer(response);
350
- }
351
- catch (errString) {
352
- if (typeof errString === 'string') {
353
- const error = stringToError(errString);
354
- if (error.code === NfcError.ErrorCode.NotConnectedError ||
355
- error.code === NfcError.ErrorCode.TagLostError) {
356
- this._onConnectionLost(error);
357
- }
358
- throw error;
359
- }
360
- else {
361
- throw errString;
362
- }
363
- }
364
- }));
365
- }));
366
- }
367
- _onConnectionLost(error) {
368
- if (this.connectionState !== ConnectionState.DISCONNECTED) {
369
- this.setConnectionState(ConnectionState.DISCONNECTED);
370
- }
371
- }
372
- }
373
- /**
374
- * Convert error string returned by the plugin into an error object
375
- * It only checks a few Android error string for now
376
- *
377
- * TODO complete implementation with other error types
378
- *
379
- * @param errString
380
- */
381
- function stringToError(errString) {
382
- const errStringLc = errString.toLowerCase();
383
- if (errStringLc.indexOf('tag was lost') >= 0) {
384
- return NfcError.tagLostError();
385
- }
386
- else if (errStringLc.indexOf('not connected') >= 0) {
387
- return NfcError.notConnectedError();
388
- }
389
- else {
390
- return NfcError.unknownError(errString);
391
- }
358
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
359
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
360
+ return new (P || (P = Promise))(function (resolve, reject) {
361
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
362
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
363
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
364
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
365
+ });
366
+ };
367
+ class PromiseQueue {
368
+ constructor() {
369
+ this.queue = Promise.resolve(true);
370
+ }
371
+ add(operation) {
372
+ return new Promise((resolve, reject) => {
373
+ this.queue = this.queue.then(operation).then(resolve).catch(reject);
374
+ });
375
+ }
376
+ }
377
+ class NFCComProtocol extends QueueComProtocol {
378
+ constructor(options = {
379
+ connect: {
380
+ timeout: 2000,
381
+ },
382
+ disconnect: {
383
+ timeout: 1000,
384
+ },
385
+ send: {
386
+ timeout: 1000,
387
+ },
388
+ }) {
389
+ super();
390
+ this._sendQueue = new PromiseQueue();
391
+ this.options = options;
392
+ if (typeof nfc == undefined) {
393
+ console.warn('NFC plugin has not been setup properly. Global variable NFC does not exist');
394
+ }
395
+ }
396
+ static iOSProtocol() {
397
+ return new NFCComProtocol({
398
+ connect: {
399
+ timeout: 10000, // bigger timer on connect as connect launches a reading session
400
+ },
401
+ disconnect: {
402
+ timeout: 1000,
403
+ },
404
+ send: {
405
+ timeout: 1000,
406
+ },
407
+ });
408
+ }
409
+ /**
410
+ * We force tag connection with nfc as we need to refresh tag
411
+ * @param options
412
+ * @returns
413
+ */
414
+ connect(options) {
415
+ this.connectionState = ConnectionState.CONNECTING; // Hack to force NFC tag connect call even if we are already connected
416
+ return super.connect(options);
417
+ }
418
+ /**
419
+ * Not used as we have rewrote "connect()" function
420
+ * @param options
421
+ * @returns
422
+ */
423
+ _connect(options) {
424
+ return defer(() => __awaiter(this, void 0, void 0, function* () {
425
+ debug('_connect', options);
426
+ try {
427
+ yield nfc.connect('android.nfc.tech.NfcV', this.options.connect.timeout);
428
+ }
429
+ catch (err) {
430
+ if (typeof err === 'string') {
431
+ if (err === 'Tag connection failed') {
432
+ throw NfcError.tagConnectionFailed();
433
+ }
434
+ else {
435
+ throw NfcError.unknownError(err);
436
+ }
437
+ }
438
+ throw err;
439
+ }
440
+ }));
441
+ }
442
+ _disconnect(options) {
443
+ return defer(() => __awaiter(this, void 0, void 0, function* () {
444
+ yield nfc.close();
445
+ this._sendQueue = new PromiseQueue();
446
+ }));
447
+ }
448
+ /**
449
+ * Not used
450
+ * @param options
451
+ * @returns
452
+ */
453
+ write(data) {
454
+ return __awaiter(this, void 0, void 0, function* () {
455
+ throw new Error('Method not implemented.');
456
+ });
457
+ }
458
+ /**
459
+ * Not used
460
+ * @param options
461
+ * @returns
462
+ */
463
+ read() {
464
+ return __awaiter(this, void 0, void 0, function* () {
465
+ throw new Error('Method not implemented.');
466
+ });
467
+ }
468
+ send(data, options) {
469
+ return defer(() => __awaiter(this, void 0, void 0, function* () {
470
+ return yield this._sendQueue.add(() => __awaiter(this, void 0, void 0, function* () {
471
+ try {
472
+ const response = yield nfc.transceiveTap(bufferToHexString(data));
473
+ if (typeof response != 'string') {
474
+ throw NfcError.internalError(`Internal error. Plugin should respond a hexadecimal string`);
475
+ }
476
+ debug('NFC plugin response: ', response);
477
+ return hexStringToBuffer(response);
478
+ }
479
+ catch (errString) {
480
+ if (typeof errString === 'string') {
481
+ const error = stringToError(errString);
482
+ if (error.code === NfcError.ErrorCode.NotConnectedError ||
483
+ error.code === NfcError.ErrorCode.TagLostError) {
484
+ this._onConnectionLost(error);
485
+ }
486
+ throw error;
487
+ }
488
+ else {
489
+ throw errString;
490
+ }
491
+ }
492
+ }));
493
+ }));
494
+ }
495
+ _onConnectionLost(error) {
496
+ if (this.connectionState !== ConnectionState.DISCONNECTED) {
497
+ this.setConnectionState(ConnectionState.DISCONNECTED);
498
+ }
499
+ }
500
+ }
501
+ /**
502
+ * Convert error string returned by the plugin into an error object
503
+ * It only checks a few Android error string for now
504
+ *
505
+ * TODO complete implementation with other error types
506
+ *
507
+ * @param errString
508
+ */
509
+ function stringToError(errString) {
510
+ const errStringLc = errString.toLowerCase();
511
+ if (errStringLc.indexOf('tag was lost') >= 0) {
512
+ return NfcError.tagLostError();
513
+ }
514
+ else if (errStringLc.indexOf('not connected') >= 0) {
515
+ return NfcError.notConnectedError();
516
+ }
517
+ else {
518
+ return NfcError.unknownError(errString);
519
+ }
392
520
  }
393
521
 
394
- /**
395
- * We manage only on NDEF message with 3 records:
396
- * record 0 = URI; record 1 = AAR; record 2 = BLE MAC ADDRESS / BSSID; record 3: universal link
397
- */
398
- function parseTapNdefMessage(messages) {
399
- let result = {};
400
- if (messages.length >= 1) {
401
- let asciiUri = messages[0].payload;
402
- result.uri = toAsciiString(asciiUri);
403
- }
404
- if (messages.length >= 2) {
405
- result.aar = toAsciiString(messages[1].payload);
406
- }
407
- if (messages.length >= 3) {
408
- let payload3 = messages[2].payload;
409
- let type = payload3[0];
410
- let content = payload3.slice(1);
411
- result.type = type;
412
- switch (type) {
413
- case TapNdefProtocolType.BLE:
414
- result.macAddress = convertBytesToBLEAddress(content);
415
- break;
416
- case TapNdefProtocolType.WiFi:
417
- result.ssid = toAsciiString(content);
418
- break;
419
- }
420
- }
421
- if (messages.length >= 4) {
422
- result.name = toAsciiString(messages[3].payload);
423
- }
424
- return result;
425
- }
426
- var TapNdefProtocolType;
427
- (function (TapNdefProtocolType) {
428
- TapNdefProtocolType[TapNdefProtocolType["WiFi"] = 32] = "WiFi";
429
- TapNdefProtocolType[TapNdefProtocolType["BLE"] = 64] = "BLE";
430
- })(TapNdefProtocolType || (TapNdefProtocolType = {}));
431
- function convertBytesToBLEAddress(bytes) {
432
- return bytes
433
- .map((byte) => {
434
- if (byte < 0) {
435
- byte += 256;
436
- }
437
- let byteString = '0' + byte.toString(16).toUpperCase();
438
- byteString = byteString.slice(-2);
439
- return byteString;
440
- })
441
- .reverse()
442
- .join(':');
522
+ /**
523
+ * We manage only on NDEF message with 3 records:
524
+ * record 0 = URI; record 1 = AAR; record 2 = BLE MAC ADDRESS / BSSID; record 3: universal link
525
+ */
526
+ function parseTapNdefMessage(messages) {
527
+ let result = {};
528
+ if (messages.length >= 1) {
529
+ let asciiUri = messages[0].payload;
530
+ result.uri = toAsciiString(asciiUri);
531
+ }
532
+ if (messages.length >= 2) {
533
+ result.aar = toAsciiString(messages[1].payload);
534
+ }
535
+ if (messages.length >= 3) {
536
+ let payload3 = messages[2].payload;
537
+ let type = payload3[0];
538
+ let content = payload3.slice(1);
539
+ result.type = type;
540
+ switch (type) {
541
+ case TapNdefProtocolType.BLE:
542
+ result.macAddress = convertBytesToBLEAddress(content);
543
+ break;
544
+ case TapNdefProtocolType.WiFi:
545
+ result.ssid = toAsciiString(content);
546
+ break;
547
+ }
548
+ }
549
+ if (messages.length >= 4) {
550
+ result.name = toAsciiString(messages[3].payload);
551
+ }
552
+ return result;
553
+ }
554
+ var TapNdefProtocolType;
555
+ (function (TapNdefProtocolType) {
556
+ TapNdefProtocolType[TapNdefProtocolType["WiFi"] = 32] = "WiFi";
557
+ TapNdefProtocolType[TapNdefProtocolType["BLE"] = 64] = "BLE";
558
+ })(TapNdefProtocolType || (TapNdefProtocolType = {}));
559
+ function convertBytesToBLEAddress(bytes) {
560
+ return bytes
561
+ .map((byte) => {
562
+ if (byte < 0) {
563
+ byte += 256;
564
+ }
565
+ let byteString = '0' + byte.toString(16).toUpperCase();
566
+ byteString = byteString.slice(-2);
567
+ return byteString;
568
+ })
569
+ .reverse()
570
+ .join(':');
443
571
  }
444
572
 
445
- /**
446
- * Generated bundle index. Do not edit.
573
+ /**
574
+ * Generated bundle index. Do not edit.
447
575
  */
448
576
 
449
- export { NFCComProtocol, NfcError, TNF, WellKnownType, parseNdefMessage, parseNdefMessages, parseTapNdefMessage };
577
+ export { NFCComProtocol, NfcError, TNF, WellKnownType, encodeNDEFMessage, encodeNDEFMessages, parseNdefMessage, parseNdefMessages, parseTapNdefMessage };
450
578
  //# sourceMappingURL=iotize-device-com-nfc.cordova.js.map