@iotize/device-com-nfc.cordova 3.8.1 → 3.8.2
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.
- package/LICENSE +30 -30
- package/README.md +673 -673
- package/bundles/iotize-device-com-nfc.cordova.umd.js +946 -967
- package/bundles/iotize-device-com-nfc.cordova.umd.js.map +1 -1
- package/bundles/iotize-device-com-nfc.cordova.umd.min.js +1 -1
- package/bundles/iotize-device-com-nfc.cordova.umd.min.js.map +1 -1
- package/esm2015/iotize-device-com-nfc.cordova.js +4 -4
- package/esm2015/public_api.js +1 -1
- package/esm2015/www/cordova-interface.js +1 -1
- package/esm2015/www/errors.js +30 -30
- package/esm2015/www/errors.js.map +1 -1
- package/esm2015/www/index.js +6 -6
- package/esm2015/www/index.js.map +1 -1
- package/esm2015/www/logger.js +2 -2
- package/esm2015/www/logger.js.map +1 -1
- package/esm2015/www/ndef/definitions.js +16 -16
- package/esm2015/www/ndef/definitions.js.map +1 -1
- package/esm2015/www/ndef/parse-ndef-message.js +164 -164
- package/esm2015/www/ndef/parse-ndef-message.js.map +1 -1
- package/esm2015/www/nfc-com-protocol.js +169 -169
- package/esm2015/www/tap-ndef/definitions.js +1 -1
- package/esm2015/www/tap-ndef/definitions.js.map +1 -1
- package/esm2015/www/tap-ndef/index.js +2 -2
- package/esm2015/www/tap-ndef/index.js.map +1 -1
- package/esm2015/www/tap-ndef/parse-ndef-message.js +51 -51
- package/esm2015/www/tap-ndef/parse-ndef-message.js.map +1 -1
- package/esm2015/www/utility.js +10 -10
- package/esm2015/www/utility.js.map +1 -1
- package/esm2015/www/utility.ngsummary.json +1 -1
- package/fesm2015/iotize-device-com-nfc.cordova.js +425 -425
- package/fesm2015/iotize-device-com-nfc.cordova.js.map +1 -1
- package/iotize-device-com-nfc.cordova.d.ts +4 -4
- package/package.json +1 -1
- package/public_api.d.ts +1 -1
- package/src/android/.gradle/4.10.1/fileChanges/last-build.bin +0 -0
- package/src/android/.gradle/4.10.1/fileHashes/fileHashes.bin +0 -0
- package/src/android/.gradle/4.10.1/fileHashes/fileHashes.lock +0 -0
- package/src/android/.gradle/4.10.1/gc.properties +0 -0
- package/src/android/.gradle/vcs-1/gc.properties +0 -0
- package/src/android/build.gradle +38 -38
- package/src/android/gradle/wrapper/gradle-wrapper.properties +6 -6
- package/src/android/gradlew +172 -172
- package/src/android/gradlew.bat +84 -84
- package/src/android/local.properties +8 -8
- package/src/android/src/com/chariotsolutions/nfc/plugin/JSONBuilder.java +60 -60
- package/src/android/src/com/chariotsolutions/nfc/plugin/NfcPlugin.java +92 -81
- package/src/android/src/com/chariotsolutions/nfc/plugin/NfcPluginError.java +15 -15
- package/src/android/src/com/chariotsolutions/nfc/plugin/PluginResponse.java +85 -85
- package/src/android/src/com/chariotsolutions/nfc/plugin/Util.java +140 -140
- package/src/ios/NFCTagReader.swift +32 -2
- package/src/ios/NFCTapPlugin.swift +57 -1
- package/www/cordova-interface.d.ts +34 -34
- package/www/errors.d.ts +17 -17
- package/www/index.d.ts +6 -6
- package/www/logger.d.ts +2 -2
- package/www/ndef/definitions.d.ts +15 -15
- package/www/ndef/parse-ndef-message.d.ts +69 -69
- package/www/nfc-com-protocol.d.ts +36 -36
- package/www/tap-ndef/definitions.d.ts +25 -25
- package/www/tap-ndef/index.d.ts +2 -2
- package/www/tap-ndef/parse-ndef-message.d.ts +6 -6
- package/www/utility.d.ts +2 -2
|
@@ -5,445 +5,445 @@ 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
|
+
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');
|
|
226
226
|
}
|
|
227
227
|
|
|
228
228
|
const debug = createDebugger(`@iotize/device-com-nfc.cordova`);
|
|
229
229
|
|
|
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
|
-
}
|
|
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
|
+
}
|
|
392
392
|
}
|
|
393
393
|
|
|
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(':');
|
|
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(':');
|
|
443
443
|
}
|
|
444
444
|
|
|
445
|
-
/**
|
|
446
|
-
* Generated bundle index. Do not edit.
|
|
445
|
+
/**
|
|
446
|
+
* Generated bundle index. Do not edit.
|
|
447
447
|
*/
|
|
448
448
|
|
|
449
449
|
export { NFCComProtocol, NfcError, TNF, WellKnownType, parseNdefMessage, parseNdefMessages, parseTapNdefMessage };
|