@iotize/device-com-nfc.cordova 3.1.4 → 3.2.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.
@@ -1,83 +1,129 @@
1
- //
2
- // Copyright 2018 IoTize SAS Inc. Licensed under the MIT license.
3
- //
4
- // ble-com-protocol.ts
5
- // device-com-ble.cordova BLE Cordova Plugin
6
- //
7
- import { bufferToHexString, hexStringToBuffer } from '@iotize/common/byte-converter';
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ import { bufferToHexString, hexStringToBuffer, } from '@iotize/common/byte-converter';
8
11
  import { ConnectionState, } from '@iotize/tap/protocol/api';
9
12
  import { QueueComProtocol } from '@iotize/tap/protocol/core';
10
- import { from } from 'rxjs';
13
+ import { defer } from 'rxjs';
11
14
  import { NfcError } from './errors';
12
15
  import { debug } from './logger';
13
16
  export class NFCComProtocol extends QueueComProtocol {
14
17
  constructor(options = {
15
18
  connect: {
16
- timeout: 2000
19
+ timeout: 2000,
17
20
  },
18
21
  disconnect: {
19
- timeout: 1000
22
+ timeout: 1000,
20
23
  },
21
24
  send: {
22
- timeout: 1000
23
- }
25
+ timeout: 1000,
26
+ },
24
27
  }) {
25
28
  super();
26
29
  this.options = options;
27
30
  if (typeof nfc == undefined) {
28
- console.warn("NFC plugin has not been setup properly. Global variable NFC does not exist");
31
+ console.warn('NFC plugin has not been setup properly. Global variable NFC does not exist');
29
32
  }
30
33
  }
31
34
  static iOSProtocol() {
32
35
  return new NFCComProtocol({
33
36
  connect: {
34
- timeout: 10000 // bigger timer on connect as connect launches a reading session
37
+ timeout: 10000, // bigger timer on connect as connect launches a reading session
35
38
  },
36
39
  disconnect: {
37
- timeout: 1000
40
+ timeout: 1000,
38
41
  },
39
42
  send: {
40
- timeout: 1000
41
- }
43
+ timeout: 1000,
44
+ },
42
45
  });
43
46
  }
47
+ /**
48
+ * We force tag connection with nfc as we need to refresh tag
49
+ * @param options
50
+ * @returns
51
+ */
52
+ connect(options) {
53
+ this.connectionState = ConnectionState.CONNECTING; // Hack to force NFC tag connect call even if we are already connected
54
+ return super.connect(options);
55
+ }
56
+ /**
57
+ * Not used as we have rewrote "connect()" function
58
+ * @param options
59
+ * @returns
60
+ */
44
61
  _connect(options) {
45
- debug('_connect', options);
46
- const connectPromise = nfc.connect("android.nfc.tech.NfcV", this.options.connect.timeout);
47
- return from(connectPromise);
62
+ return defer(() => __awaiter(this, void 0, void 0, function* () {
63
+ debug('_connect', options);
64
+ try {
65
+ yield nfc.connect('android.nfc.tech.NfcV', this.options.connect.timeout);
66
+ }
67
+ catch (err) {
68
+ if (typeof err === 'string') {
69
+ if (err === 'Tag connection failed') {
70
+ throw NfcError.tagConnectionFailed();
71
+ }
72
+ else {
73
+ throw NfcError.unknownError(err);
74
+ }
75
+ }
76
+ throw err;
77
+ }
78
+ }));
48
79
  }
49
80
  _disconnect(options) {
50
- return from(nfc.close());
81
+ return defer(() => nfc.close());
51
82
  }
83
+ /**
84
+ * Not used
85
+ * @param options
86
+ * @returns
87
+ */
52
88
  write(data) {
53
- throw new Error("Method not implemented.");
89
+ return __awaiter(this, void 0, void 0, function* () {
90
+ throw new Error('Method not implemented.');
91
+ });
54
92
  }
93
+ /**
94
+ * Not used
95
+ * @param options
96
+ * @returns
97
+ */
55
98
  read() {
56
- throw new Error("Method not implemented.");
99
+ return __awaiter(this, void 0, void 0, function* () {
100
+ throw new Error('Method not implemented.');
101
+ });
57
102
  }
58
103
  send(data, options) {
59
- const promise = nfc
60
- .transceive(bufferToHexString(data))
61
- .then((response) => {
62
- if (typeof response != "string") {
63
- throw NfcError.internalError(`Internal error. Plugin should respond a hexadecimal string`);
64
- }
65
- debug('NFC plugin response: ', response);
66
- return hexStringToBuffer(response);
67
- })
68
- .catch((errString) => {
69
- if (typeof errString === "string") {
70
- const error = stringToError(errString);
71
- if (error.code === NfcError.ErrorCode.NotConnectedError || error.code === NfcError.ErrorCode.TagLostError) {
72
- this._onConnectionLost(error);
104
+ return defer(() => __awaiter(this, void 0, void 0, function* () {
105
+ try {
106
+ const response = yield nfc.transceive(bufferToHexString(data));
107
+ if (typeof response != 'string') {
108
+ throw NfcError.internalError(`Internal error. Plugin should respond a hexadecimal string`);
73
109
  }
74
- throw error;
110
+ debug('NFC plugin response: ', response);
111
+ return hexStringToBuffer(response);
75
112
  }
76
- else {
77
- throw errString;
113
+ catch (errString) {
114
+ if (typeof errString === 'string') {
115
+ const error = stringToError(errString);
116
+ if (error.code === NfcError.ErrorCode.NotConnectedError ||
117
+ error.code === NfcError.ErrorCode.TagLostError) {
118
+ this._onConnectionLost(error);
119
+ }
120
+ throw error;
121
+ }
122
+ else {
123
+ throw errString;
124
+ }
78
125
  }
79
- });
80
- return from(promise);
126
+ }));
81
127
  }
82
128
  _onConnectionLost(error) {
83
129
  if (this.connectionState !== ConnectionState.DISCONNECTED) {
@@ -85,7 +131,6 @@ export class NFCComProtocol extends QueueComProtocol {
85
131
  }
86
132
  }
87
133
  }
88
- ;
89
134
  /**
90
135
  * Convert error string returned by the plugin into an error object
91
136
  * It only checks a few Android error string for now
@@ -1 +1 @@
1
- {"version":3,"file":"nfc-com-protocol.js","sourceRoot":"","sources":["../../../../../src/www/nfc-com-protocol.ts"],"names":[],"mappings":"AAAA,EAAE;AACF,oEAAoE;AACpE,EAAE;AACF,uBAAuB;AACvB,6CAA6C;AAC7C,EAAE;AACF,OAAO,EAAE,iBAAiB,EAAE,iBAAiB,EAAE,MAAM,+BAA+B,CAAC;AACrF,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AAiB;IACpC,MAAM,WAAW,GAAG,SAAS,CAAC,WAAW,EAAE,CAAC;IAC5C,IAAI,WAAW,CAAC,OAAO,CAAC,cAAc,CAAC,IAAI,CAAC,EAAE;QAC1C,OAAO,QAAQ,CAAC,YAAY,EAAE,CAAC;KAClC;SACI,IAAI,WAAW,CAAC,OAAO,CAAC,eAAe,CAAC,IAAI,CAAC,EAAE;QAChD,OAAO,QAAQ,CAAC,iBAAiB,EAAE,CAAC;KACvC;SACI;QACD,OAAO,QAAQ,CAAC,YAAY,CAAC,SAAS,CAAC,CAAC;KAC3C;AACL,CAAC","sourcesContent":["//\r\n// Copyright 2018 IoTize SAS Inc. Licensed under the MIT license. \r\n//\r\n// ble-com-protocol.ts\r\n// device-com-ble.cordova BLE Cordova Plugin\r\n//\r\nimport { bufferToHexString, hexStringToBuffer } from '@iotize/common/byte-converter';\r\nimport {\r\n ComProtocolConnectOptions,\r\n ComProtocolDisconnectOptions,\r\n ComProtocolOptions,\r\n ComProtocolSendOptions,\r\n ConnectionState,\r\n} from '@iotize/tap/protocol/api';\r\nimport { QueueComProtocol } from '@iotize/tap/protocol/core';\r\nimport { from, Observable } from 'rxjs';\r\n\r\nimport { CordovaInterface } from './cordova-interface';\r\nimport { NfcError } from './errors';\r\nimport { debug } from './logger';\r\n\r\n\r\ndeclare var nfc: CordovaInterface;\r\n\r\nexport class NFCComProtocol extends QueueComProtocol {\r\n\r\n constructor(options: ComProtocolOptions = {\r\n connect: {\r\n timeout: 2000\r\n },\r\n disconnect: {\r\n timeout: 1000\r\n },\r\n send: {\r\n timeout: 1000\r\n }\r\n }) {\r\n super();\r\n this.options = options;\r\n if (typeof nfc == undefined) {\r\n console.warn(\"NFC plugin has not been setup properly. Global variable NFC does not exist\");\r\n }\r\n }\r\n\r\n public static iOSProtocol(): NFCComProtocol {\r\n return new NFCComProtocol({\r\n connect: {\r\n timeout: 10000 // bigger timer on connect as connect launches a reading session\r\n },\r\n disconnect: {\r\n timeout: 1000\r\n },\r\n send: {\r\n timeout: 1000\r\n }\r\n })\r\n }\r\n\r\n _connect(options?: ComProtocolConnectOptions): Observable<any> {\r\n debug('_connect', options);\r\n const connectPromise = nfc.connect(\"android.nfc.tech.NfcV\", this.options.connect.timeout)\r\n return from(connectPromise);\r\n }\r\n\r\n _disconnect(options?: ComProtocolDisconnectOptions): Observable<any> {\r\n return from(nfc.close());\r\n }\r\n\r\n write(data: Uint8Array): Promise<any> {\r\n throw new Error(\"Method not implemented.\");\r\n }\r\n\r\n read(): Promise<Uint8Array> {\r\n throw new Error(\"Method not implemented.\");\r\n }\r\n\r\n send(data: Uint8Array, options?: ComProtocolSendOptions): Observable<Uint8Array> {\r\n const promise = nfc\r\n .transceive(bufferToHexString(data))\r\n .then((response: string) => {\r\n if (typeof response != \"string\") {\r\n throw NfcError.internalError(`Internal error. Plugin should respond a hexadecimal string`);\r\n }\r\n debug('NFC plugin response: ', response)\r\n return hexStringToBuffer(response)\r\n })\r\n .catch((errString) => {\r\n if (typeof errString === \"string\") {\r\n const error = stringToError(errString);\r\n if (error.code === NfcError.ErrorCode.NotConnectedError || error.code === NfcError.ErrorCode.TagLostError) {\r\n this._onConnectionLost(error);\r\n }\r\n throw error;\r\n }\r\n else {\r\n throw errString;\r\n }\r\n });\r\n return from(promise);\r\n }\r\n\r\n _onConnectionLost(error: NfcError) {\r\n if (this.connectionState !== ConnectionState.DISCONNECTED) {\r\n this.setConnectionState(ConnectionState.DISCONNECTED);\r\n }\r\n }\r\n\r\n};\r\n\r\n/**\r\n * Convert error string returned by the plugin into an error object\r\n * It only checks a few Android error string for now\r\n * \r\n * TODO complete implementation with other error types\r\n * \r\n * @param errString \r\n */\r\nfunction stringToError(errString: string): NfcError {\r\n const errStringLc = errString.toLowerCase();\r\n if (errStringLc.indexOf('tag was lost') >= 0) {\r\n return NfcError.tagLostError();\r\n }\r\n else if (errStringLc.indexOf('not connected') >= 0) {\r\n return NfcError.notConnectedError();\r\n }\r\n else {\r\n return NfcError.unknownError(errString);\r\n }\r\n}\r\n"]}
1
+ {"version":3,"file":"nfc-com-protocol.js","sourceRoot":"","sources":["../../../../../src/www/nfc-com-protocol.ts"],"names":[],"mappings":";;;;;;;;;AAAA,OAAO,EACL,iBAAiB,EACjB,iBAAiB,GAClB,MAAM,+BAA+B,CAAC;AACvC,OAAO,EAKL,eAAe,GAChB,MAAM,0BAA0B,CAAC;AAClC,OAAO,EAAE,gBAAgB,EAAE,MAAM,2BAA2B,CAAC;AAC7D,OAAO,EAAE,KAAK,EAAc,MAAM,MAAM,CAAC;AAGzC,OAAO,EAAE,QAAQ,EAAE,MAAM,UAAU,CAAC;AACpC,OAAO,EAAE,KAAK,EAAE,MAAM,UAAU,CAAC;AAIjC,MAAM,OAAO,cAAe,SAAQ,gBAAgB;IAClD,YACE,UAA8B;QAC5B,OAAO,EAAE;YACP,OAAO,EAAE,IAAI;SACd;QACD,UAAU,EAAE;YACV,OAAO,EAAE,IAAI;SACd;QACD,IAAI,EAAE;YACJ,OAAO,EAAE,IAAI;SACd;KACF;QAED,KAAK,EAAE,CAAC;QACR,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;QACvB,IAAI,OAAO,GAAG,IAAI,SAAS,EAAE;YAC3B,OAAO,CAAC,IAAI,CACV,4EAA4E,CAC7E,CAAC;SACH;IACH,CAAC;IAEM,MAAM,CAAC,WAAW;QACvB,OAAO,IAAI,cAAc,CAAC;YACxB,OAAO,EAAE;gBACP,OAAO,EAAE,KAAK,EAAE,gEAAgE;aACjF;YACD,UAAU,EAAE;gBACV,OAAO,EAAE,IAAI;aACd;YACD,IAAI,EAAE;gBACJ,OAAO,EAAE,IAAI;aACd;SACF,CAAC,CAAC;IACL,CAAC;IAED;;;;OAIG;IACH,OAAO,CAAC,OAAmC;QACzC,IAAI,CAAC,eAAe,GAAG,eAAe,CAAC,UAAU,CAAC,CAAC,sEAAsE;QACzH,OAAO,KAAK,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;IAChC,CAAC;IAED;;;;OAI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CAAC,QAAQ,CAAC,CAAC;aACpC;YAAC,OAAO,SAAS,EAAE;gBAClB,IAAI,OAAO,SAAS,KAAK,QAAQ,EAAE;oBACjC,MAAM,KAAK,GAAG,aAAa,CAAC,SAAS,CAAC,CAAC;oBACvC,IACE,KAAK,CAAC,IAAI,KAAK,QAAQ,CAAC,SAAS,CAAC,iBAAiB;wBACnD,KAAK,CAAC,IAAI,KAAK,QAAQ,CAAC,SAAS,CAAC,YAAY,EAC9C;wBACA,IAAI,CAAC,iBAAiB,CAAC,KAAK,CAAC,CAAC;qBAC/B;oBACD,MAAM,KAAK,CAAC;iBACb;qBAAM;oBACL,MAAM,SAAS,CAAC;iBACjB;aACF;QACH,CAAC,CAAA,CAAC,CAAC;IACL,CAAC;IAED,iBAAiB,CAAC,KAAe;QAC/B,IAAI,IAAI,CAAC,eAAe,KAAK,eAAe,CAAC,YAAY,EAAE;YACzD,IAAI,CAAC,kBAAkB,CAAC,eAAe,CAAC,YAAY,CAAC,CAAC;SACvD;IACH,CAAC;CACF;AAED;;;;;;;GAOG;AACH,SAAS,aAAa,CAAC,SAAiB;IACtC,MAAM,WAAW,GAAG,SAAS,CAAC,WAAW,EAAE,CAAC;IAC5C,IAAI,WAAW,CAAC,OAAO,CAAC,cAAc,CAAC,IAAI,CAAC,EAAE;QAC5C,OAAO,QAAQ,CAAC,YAAY,EAAE,CAAC;KAChC;SAAM,IAAI,WAAW,CAAC,OAAO,CAAC,eAAe,CAAC,IAAI,CAAC,EAAE;QACpD,OAAO,QAAQ,CAAC,iBAAiB,EAAE,CAAC;KACrC;SAAM;QACL,OAAO,QAAQ,CAAC,YAAY,CAAC,SAAS,CAAC,CAAC;KACzC;AACH,CAAC","sourcesContent":["import {\r\n bufferToHexString,\r\n hexStringToBuffer,\r\n} from '@iotize/common/byte-converter';\r\nimport {\r\n ComProtocolConnectOptions,\r\n ComProtocolDisconnectOptions,\r\n ComProtocolOptions,\r\n ComProtocolSendOptions,\r\n ConnectionState,\r\n} from '@iotize/tap/protocol/api';\r\nimport { QueueComProtocol } from '@iotize/tap/protocol/core';\r\nimport { defer, Observable } from 'rxjs';\r\n\r\nimport { CordovaInterface } from './cordova-interface';\r\nimport { NfcError } from './errors';\r\nimport { debug } from './logger';\r\n\r\ndeclare var nfc: CordovaInterface;\r\n\r\nexport class NFCComProtocol extends QueueComProtocol {\r\n constructor(\r\n options: ComProtocolOptions = {\r\n connect: {\r\n timeout: 2000,\r\n },\r\n disconnect: {\r\n timeout: 1000,\r\n },\r\n send: {\r\n timeout: 1000,\r\n },\r\n }\r\n ) {\r\n super();\r\n this.options = options;\r\n if (typeof nfc == undefined) {\r\n console.warn(\r\n 'NFC plugin has not been setup properly. Global variable NFC does not exist'\r\n );\r\n }\r\n }\r\n\r\n public static iOSProtocol(): NFCComProtocol {\r\n return new NFCComProtocol({\r\n connect: {\r\n timeout: 10000, // bigger timer on connect as connect launches a reading session\r\n },\r\n disconnect: {\r\n timeout: 1000,\r\n },\r\n send: {\r\n timeout: 1000,\r\n },\r\n });\r\n }\r\n\r\n /**\r\n * We force tag connection with nfc as we need to refresh tag\r\n * @param options\r\n * @returns\r\n */\r\n connect(options?: ComProtocolConnectOptions): Observable<void> {\r\n this.connectionState = ConnectionState.CONNECTING; // Hack to force NFC tag connect call even if we are already connected\r\n return super.connect(options);\r\n }\r\n\r\n /**\r\n * Not used as we have rewrote \"connect()\" function\r\n * @param options\r\n * @returns\r\n */\r\n _connect(options?: ComProtocolConnectOptions): Observable<any> {\r\n return defer(async () => {\r\n debug('_connect', options);\r\n try {\r\n await nfc.connect(\r\n 'android.nfc.tech.NfcV',\r\n this.options.connect.timeout\r\n );\r\n } catch (err) {\r\n if (typeof err === 'string') {\r\n if (err === 'Tag connection failed') {\r\n throw NfcError.tagConnectionFailed();\r\n } else {\r\n throw NfcError.unknownError(err);\r\n }\r\n }\r\n throw err;\r\n }\r\n });\r\n }\r\n\r\n _disconnect(options?: ComProtocolDisconnectOptions): Observable<any> {\r\n return defer(() => nfc.close());\r\n }\r\n\r\n /**\r\n * Not used\r\n * @param options\r\n * @returns\r\n */\r\n async write(data: Uint8Array): Promise<any> {\r\n throw new Error('Method not implemented.');\r\n }\r\n\r\n /**\r\n * Not used\r\n * @param options\r\n * @returns\r\n */\r\n async read(): Promise<Uint8Array> {\r\n throw new Error('Method not implemented.');\r\n }\r\n\r\n send(\r\n data: Uint8Array,\r\n options?: ComProtocolSendOptions\r\n ): Observable<Uint8Array> {\r\n return defer(async () => {\r\n try {\r\n const response = await nfc.transceive(bufferToHexString(data));\r\n if (typeof response != 'string') {\r\n throw NfcError.internalError(\r\n `Internal error. Plugin should respond a hexadecimal string`\r\n );\r\n }\r\n debug('NFC plugin response: ', response);\r\n return hexStringToBuffer(response);\r\n } catch (errString) {\r\n if (typeof errString === 'string') {\r\n const error = stringToError(errString);\r\n if (\r\n error.code === NfcError.ErrorCode.NotConnectedError ||\r\n error.code === NfcError.ErrorCode.TagLostError\r\n ) {\r\n this._onConnectionLost(error);\r\n }\r\n throw error;\r\n } else {\r\n throw errString;\r\n }\r\n }\r\n });\r\n }\r\n\r\n _onConnectionLost(error: NfcError) {\r\n if (this.connectionState !== ConnectionState.DISCONNECTED) {\r\n this.setConnectionState(ConnectionState.DISCONNECTED);\r\n }\r\n }\r\n}\r\n\r\n/**\r\n * Convert error string returned by the plugin into an error object\r\n * It only checks a few Android error string for now\r\n *\r\n * TODO complete implementation with other error types\r\n *\r\n * @param errString\r\n */\r\nfunction stringToError(errString: string): NfcError {\r\n const errStringLc = errString.toLowerCase();\r\n if (errStringLc.indexOf('tag was lost') >= 0) {\r\n return NfcError.tagLostError();\r\n } else if (errStringLc.indexOf('not connected') >= 0) {\r\n return NfcError.notConnectedError();\r\n } else {\r\n return NfcError.unknownError(errString);\r\n }\r\n}\r\n"]}
@@ -1 +1 @@
1
- [{"__symbolic":"module","version":4,"metadata":{"NFCComProtocol":{"__symbolic":"class","extends":{"__symbolic":"reference","module":"@iotize/tap/protocol/core","name":"QueueComProtocol","line":24,"character":36},"members":{"__ctor__":[{"__symbolic":"constructor","parameters":[{"__symbolic":"reference","module":"@iotize/tap/protocol/api","name":"ComProtocolOptions","line":26,"character":25}]}],"_connect":[{"__symbolic":"method"}],"_disconnect":[{"__symbolic":"method"}],"write":[{"__symbolic":"method"}],"read":[{"__symbolic":"method"}],"send":[{"__symbolic":"method"}],"_onConnectionLost":[{"__symbolic":"method"}]},"statics":{"iOSProtocol":{"__symbolic":"function","parameters":[],"value":{"__symbolic":"new","expression":{"__symbolic":"reference","name":"NFCComProtocol"},"arguments":[{"connect":{"timeout":10000},"disconnect":{"timeout":1000},"send":{"timeout":1000}}]}}}}}}]
1
+ [{"__symbolic":"module","version":4,"metadata":{"NFCComProtocol":{"__symbolic":"class","extends":{"__symbolic":"reference","module":"@iotize/tap/protocol/core","name":"QueueComProtocol","line":20,"character":36},"members":{"__ctor__":[{"__symbolic":"constructor","parameters":[{"__symbolic":"reference","module":"@iotize/tap/protocol/api","name":"ComProtocolOptions","line":22,"character":13}]}],"connect":[{"__symbolic":"method"}],"_connect":[{"__symbolic":"method"}],"_disconnect":[{"__symbolic":"method"}],"write":[{"__symbolic":"method"}],"read":[{"__symbolic":"method"}],"send":[{"__symbolic":"method"}],"_onConnectionLost":[{"__symbolic":"method"}]},"statics":{"iOSProtocol":{"__symbolic":"function","parameters":[],"value":{"__symbolic":"new","expression":{"__symbolic":"reference","name":"NFCComProtocol"},"arguments":[{"connect":{"timeout":10000},"disconnect":{"timeout":1000},"send":{"timeout":1000}}]}}}}}}]
@@ -1 +1 @@
1
- {"moduleName":null,"summaries":[{"symbol":{"__symbol":0,"members":[]},"metadata":{"__symbolic":"class","extends":{"__symbol":1,"members":[]},"members":{"__ctor__":[{"__symbolic":"constructor","parameters":[{"__symbol":2,"members":[]}]}],"_connect":[{"__symbolic":"method"}],"_disconnect":[{"__symbolic":"method"}],"write":[{"__symbolic":"method"}],"read":[{"__symbolic":"method"}],"send":[{"__symbolic":"method"}],"_onConnectionLost":[{"__symbolic":"method"}]},"statics":{"iOSProtocol":{"__symbolic":"function","parameters":[],"value":{"__symbolic":"new","expression":{"__symbol":0,"members":[]},"arguments":[{"connect":{"timeout":10000},"disconnect":{"timeout":1000},"send":{"timeout":1000}}]}}}}}],"symbols":[{"__symbol":0,"name":"NFCComProtocol","filePath":"./nfc-com-protocol"},{"__symbol":1,"name":"QueueComProtocol","filePath":"@iotize/tap/protocol/core/iotize-tap-protocol-core"},{"__symbol":2,"name":"ComProtocolOptions","filePath":"@iotize/tap/protocol/api/iotize-tap-protocol-api"}]}
1
+ {"moduleName":null,"summaries":[{"symbol":{"__symbol":0,"members":[]},"metadata":{"__symbolic":"class","extends":{"__symbol":1,"members":[]},"members":{"__ctor__":[{"__symbolic":"constructor","parameters":[{"__symbol":2,"members":[]}]}],"connect":[{"__symbolic":"method"}],"_connect":[{"__symbolic":"method"}],"_disconnect":[{"__symbolic":"method"}],"write":[{"__symbolic":"method"}],"read":[{"__symbolic":"method"}],"send":[{"__symbolic":"method"}],"_onConnectionLost":[{"__symbolic":"method"}]},"statics":{"iOSProtocol":{"__symbolic":"function","parameters":[],"value":{"__symbolic":"new","expression":{"__symbol":0,"members":[]},"arguments":[{"connect":{"timeout":10000},"disconnect":{"timeout":1000},"send":{"timeout":1000}}]}}}}}],"symbols":[{"__symbol":0,"name":"NFCComProtocol","filePath":"./nfc-com-protocol"},{"__symbol":1,"name":"QueueComProtocol","filePath":"@iotize/tap/protocol/core/iotize-tap-protocol-core"},{"__symbol":2,"name":"ComProtocolOptions","filePath":"@iotize/tap/protocol/api/iotize-tap-protocol-api"}]}
@@ -1,7 +1,7 @@
1
1
  import { bufferToHexString, hexStringToBuffer, bufferToAsciiString } from '@iotize/common/byte-converter';
2
2
  import { ConnectionState } from '@iotize/tap/protocol/api';
3
3
  import { QueueComProtocol } from '@iotize/tap/protocol/core';
4
- import { from } from 'rxjs';
4
+ import { defer } from 'rxjs';
5
5
  import { createDebugger } from '@iotize/common/debug';
6
6
 
7
7
  class NfcError extends Error {
@@ -18,6 +18,9 @@ class NfcError extends Error {
18
18
  static unknownError(errString) {
19
19
  throw new NfcError(NfcError.ErrorCode.Unknown, errString);
20
20
  }
21
+ static tagConnectionFailed() {
22
+ throw new NfcError(NfcError.ErrorCode.NotConnectedError, `Tag connection failed`);
23
+ }
21
24
  static internalError(message) {
22
25
  throw new NfcError(NfcError.ErrorCode.InternalError, message);
23
26
  }
@@ -34,75 +37,126 @@ class NfcError extends Error {
34
37
 
35
38
  const debug = createDebugger(`@iotize/device-com-nfc.cordova`);
36
39
 
37
- //
40
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
41
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
42
+ return new (P || (P = Promise))(function (resolve, reject) {
43
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
44
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
45
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
46
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
47
+ });
48
+ };
38
49
  class NFCComProtocol extends QueueComProtocol {
39
50
  constructor(options = {
40
51
  connect: {
41
- timeout: 2000
52
+ timeout: 2000,
42
53
  },
43
54
  disconnect: {
44
- timeout: 1000
55
+ timeout: 1000,
45
56
  },
46
57
  send: {
47
- timeout: 1000
48
- }
58
+ timeout: 1000,
59
+ },
49
60
  }) {
50
61
  super();
51
62
  this.options = options;
52
63
  if (typeof nfc == undefined) {
53
- console.warn("NFC plugin has not been setup properly. Global variable NFC does not exist");
64
+ console.warn('NFC plugin has not been setup properly. Global variable NFC does not exist');
54
65
  }
55
66
  }
56
67
  static iOSProtocol() {
57
68
  return new NFCComProtocol({
58
69
  connect: {
59
- timeout: 10000 // bigger timer on connect as connect launches a reading session
70
+ timeout: 10000, // bigger timer on connect as connect launches a reading session
60
71
  },
61
72
  disconnect: {
62
- timeout: 1000
73
+ timeout: 1000,
63
74
  },
64
75
  send: {
65
- timeout: 1000
66
- }
76
+ timeout: 1000,
77
+ },
67
78
  });
68
79
  }
80
+ /**
81
+ * We force tag connection with nfc as we need to refresh tag
82
+ * @param options
83
+ * @returns
84
+ */
85
+ connect(options) {
86
+ this.connectionState = ConnectionState.CONNECTING; // Hack to force NFC tag connect call even if we are already connected
87
+ return super.connect(options);
88
+ }
89
+ /**
90
+ * Not used as we have rewrote "connect()" function
91
+ * @param options
92
+ * @returns
93
+ */
69
94
  _connect(options) {
70
- debug('_connect', options);
71
- const connectPromise = nfc.connect("android.nfc.tech.NfcV", this.options.connect.timeout);
72
- return from(connectPromise);
95
+ return defer(() => __awaiter(this, void 0, void 0, function* () {
96
+ debug('_connect', options);
97
+ try {
98
+ yield nfc.connect('android.nfc.tech.NfcV', this.options.connect.timeout);
99
+ }
100
+ catch (err) {
101
+ if (typeof err === 'string') {
102
+ if (err === 'Tag connection failed') {
103
+ throw NfcError.tagConnectionFailed();
104
+ }
105
+ else {
106
+ throw NfcError.unknownError(err);
107
+ }
108
+ }
109
+ throw err;
110
+ }
111
+ }));
73
112
  }
74
113
  _disconnect(options) {
75
- return from(nfc.close());
114
+ return defer(() => nfc.close());
76
115
  }
116
+ /**
117
+ * Not used
118
+ * @param options
119
+ * @returns
120
+ */
77
121
  write(data) {
78
- throw new Error("Method not implemented.");
122
+ return __awaiter(this, void 0, void 0, function* () {
123
+ throw new Error('Method not implemented.');
124
+ });
79
125
  }
126
+ /**
127
+ * Not used
128
+ * @param options
129
+ * @returns
130
+ */
80
131
  read() {
81
- throw new Error("Method not implemented.");
132
+ return __awaiter(this, void 0, void 0, function* () {
133
+ throw new Error('Method not implemented.');
134
+ });
82
135
  }
83
136
  send(data, options) {
84
- const promise = nfc
85
- .transceive(bufferToHexString(data))
86
- .then((response) => {
87
- if (typeof response != "string") {
88
- throw NfcError.internalError(`Internal error. Plugin should respond a hexadecimal string`);
89
- }
90
- debug('NFC plugin response: ', response);
91
- return hexStringToBuffer(response);
92
- })
93
- .catch((errString) => {
94
- if (typeof errString === "string") {
95
- const error = stringToError(errString);
96
- if (error.code === NfcError.ErrorCode.NotConnectedError || error.code === NfcError.ErrorCode.TagLostError) {
97
- this._onConnectionLost(error);
137
+ return defer(() => __awaiter(this, void 0, void 0, function* () {
138
+ try {
139
+ const response = yield nfc.transceive(bufferToHexString(data));
140
+ if (typeof response != 'string') {
141
+ throw NfcError.internalError(`Internal error. Plugin should respond a hexadecimal string`);
98
142
  }
99
- throw error;
143
+ debug('NFC plugin response: ', response);
144
+ return hexStringToBuffer(response);
100
145
  }
101
- else {
102
- throw errString;
146
+ catch (errString) {
147
+ if (typeof errString === 'string') {
148
+ const error = stringToError(errString);
149
+ if (error.code === NfcError.ErrorCode.NotConnectedError ||
150
+ error.code === NfcError.ErrorCode.TagLostError) {
151
+ this._onConnectionLost(error);
152
+ }
153
+ throw error;
154
+ }
155
+ else {
156
+ throw errString;
157
+ }
103
158
  }
104
- });
105
- return from(promise);
159
+ }));
106
160
  }
107
161
  _onConnectionLost(error) {
108
162
  if (this.connectionState !== ConnectionState.DISCONNECTED) {
@@ -110,7 +164,6 @@ class NFCComProtocol extends QueueComProtocol {
110
164
  }
111
165
  }
112
166
  }
113
- ;
114
167
  /**
115
168
  * Convert error string returned by the plugin into an error object
116
169
  * It only checks a few Android error string for now
@@ -1 +1 @@
1
- {"version":3,"file":"iotize-device-com-nfc.cordova.js","sources":["../../../../src/www/errors.ts","../../../../src/www/logger.ts","../../../../src/www/nfc-com-protocol.ts","../../../../src/www/tap-ndef/parse-ndef-message.ts","../../../../src/iotize-device-com-nfc.cordova.ts"],"sourcesContent":["export class NfcError extends Error {\r\n\r\n constructor(public code: NfcError.ErrorCode, message: string){\r\n super(message);\r\n }\r\n\r\n public static tagLostError(){\r\n return new NfcError(\r\n NfcError.ErrorCode.TagLostError,\r\n 'NFC tag lost'\r\n );\r\n }\r\n\r\n public static notConnectedError(){\r\n return new NfcError(\r\n NfcError.ErrorCode.NotConnectedError,\r\n 'NFC tag is not connected'\r\n );\r\n } \r\n\r\n public static unknownError(errString: string): NfcError {\r\n throw new NfcError(\r\n NfcError.ErrorCode.Unknown,\r\n errString\r\n )\r\n }\r\n \r\n public static internalError(message: string) {\r\n throw new NfcError(\r\n NfcError.ErrorCode.InternalError,\r\n message\r\n )\r\n }\r\n}\r\n\r\nexport namespace NfcError {\r\n export enum ErrorCode {\r\n Unknown = \"NfcUnknownError\",\r\n InternalError = \"NfcInternalError\",\r\n TagLostError = \"NfcTagLostError\",\r\n NotConnectedError = \"NfcNotConnectedError\"\r\n }\r\n}","import { createDebugger } from '@iotize/common/debug';\r\n\r\nexport const debug = createDebugger(`@iotize/device-com-nfc.cordova`);","//\r\n// Copyright 2018 IoTize SAS Inc. Licensed under the MIT license. \r\n//\r\n// ble-com-protocol.ts\r\n// device-com-ble.cordova BLE Cordova Plugin\r\n//\r\nimport { bufferToHexString, hexStringToBuffer } from '@iotize/common/byte-converter';\r\nimport {\r\n ComProtocolConnectOptions,\r\n ComProtocolDisconnectOptions,\r\n ComProtocolOptions,\r\n ComProtocolSendOptions,\r\n ConnectionState,\r\n} from '@iotize/tap/protocol/api';\r\nimport { QueueComProtocol } from '@iotize/tap/protocol/core';\r\nimport { from, Observable } from 'rxjs';\r\n\r\nimport { CordovaInterface } from './cordova-interface';\r\nimport { NfcError } from './errors';\r\nimport { debug } from './logger';\r\n\r\n\r\ndeclare var nfc: CordovaInterface;\r\n\r\nexport class NFCComProtocol extends QueueComProtocol {\r\n\r\n constructor(options: ComProtocolOptions = {\r\n connect: {\r\n timeout: 2000\r\n },\r\n disconnect: {\r\n timeout: 1000\r\n },\r\n send: {\r\n timeout: 1000\r\n }\r\n }) {\r\n super();\r\n this.options = options;\r\n if (typeof nfc == undefined) {\r\n console.warn(\"NFC plugin has not been setup properly. Global variable NFC does not exist\");\r\n }\r\n }\r\n\r\n public static iOSProtocol(): NFCComProtocol {\r\n return new NFCComProtocol({\r\n connect: {\r\n timeout: 10000 // bigger timer on connect as connect launches a reading session\r\n },\r\n disconnect: {\r\n timeout: 1000\r\n },\r\n send: {\r\n timeout: 1000\r\n }\r\n })\r\n }\r\n\r\n _connect(options?: ComProtocolConnectOptions): Observable<any> {\r\n debug('_connect', options);\r\n const connectPromise = nfc.connect(\"android.nfc.tech.NfcV\", this.options.connect.timeout)\r\n return from(connectPromise);\r\n }\r\n\r\n _disconnect(options?: ComProtocolDisconnectOptions): Observable<any> {\r\n return from(nfc.close());\r\n }\r\n\r\n write(data: Uint8Array): Promise<any> {\r\n throw new Error(\"Method not implemented.\");\r\n }\r\n\r\n read(): Promise<Uint8Array> {\r\n throw new Error(\"Method not implemented.\");\r\n }\r\n\r\n send(data: Uint8Array, options?: ComProtocolSendOptions): Observable<Uint8Array> {\r\n const promise = nfc\r\n .transceive(bufferToHexString(data))\r\n .then((response: string) => {\r\n if (typeof response != \"string\") {\r\n throw NfcError.internalError(`Internal error. Plugin should respond a hexadecimal string`);\r\n }\r\n debug('NFC plugin response: ', response)\r\n return hexStringToBuffer(response)\r\n })\r\n .catch((errString) => {\r\n if (typeof errString === \"string\") {\r\n const error = stringToError(errString);\r\n if (error.code === NfcError.ErrorCode.NotConnectedError || error.code === NfcError.ErrorCode.TagLostError) {\r\n this._onConnectionLost(error);\r\n }\r\n throw error;\r\n }\r\n else {\r\n throw errString;\r\n }\r\n });\r\n return from(promise);\r\n }\r\n\r\n _onConnectionLost(error: NfcError) {\r\n if (this.connectionState !== ConnectionState.DISCONNECTED) {\r\n this.setConnectionState(ConnectionState.DISCONNECTED);\r\n }\r\n }\r\n\r\n};\r\n\r\n/**\r\n * Convert error string returned by the plugin into an error object\r\n * It only checks a few Android error string for now\r\n * \r\n * TODO complete implementation with other error types\r\n * \r\n * @param errString \r\n */\r\nfunction stringToError(errString: string): NfcError {\r\n const errStringLc = errString.toLowerCase();\r\n if (errStringLc.indexOf('tag was lost') >= 0) {\r\n return NfcError.tagLostError();\r\n }\r\n else if (errStringLc.indexOf('not connected') >= 0) {\r\n return NfcError.notConnectedError();\r\n }\r\n else {\r\n return NfcError.unknownError(errString);\r\n }\r\n}\r\n","import { bufferToAsciiString } from '@iotize/common/byte-converter';\r\n\r\nimport { NdefRecord, TapNfcTagPayload } from './definitions';\r\n\r\n/**\r\n * We manage only on NDEF message with 3 records:\r\n * record 0 = URI; record 1 = AAR; record 2 = BLE MAC ADDRESS / BSSID; record 3: universal link\r\n */\r\nexport function parseTapNdefMessage(messages: NdefRecord[]): TapNfcTagPayload {\r\n let result: TapNfcTagPayload = {};\r\n if (messages.length >= 1) {\r\n let asciiUri = messages[0].payload;\r\n result.uri = toAsciiString(asciiUri);\r\n }\r\n if (messages.length >= 2) {\r\n result.aar = toAsciiString(messages[1].payload);\r\n }\r\n if (messages.length >= 3) {\r\n let payload3 = messages[2].payload;\r\n let type = payload3[0];\r\n let content = payload3.slice(1);\r\n result.type = type;\r\n\r\n switch (type) {\r\n case TapNdefProtocolType.BLE:\r\n result.macAddress = convertBytesToBLEAddress(content);\r\n break;\r\n case TapNdefProtocolType.WiFi:\r\n result.ssid = toAsciiString(content);\r\n break;\r\n }\r\n }\r\n if (messages.length >= 4) {\r\n result.name = toAsciiString(messages[3].payload);\r\n }\r\n return result;\r\n}\r\n\r\nenum TapNdefProtocolType {\r\n WiFi = 0x20,\r\n BLE = 0x40\r\n}\r\n\r\nfunction toAsciiString(payload: number[]): string {\r\n if (payload.length > 0 && payload[0] === 0) {\r\n payload = payload.slice(1);\r\n }\r\n return bufferToAsciiString(payload as any as Uint8Array);\r\n}\r\n\r\nfunction convertBytesToBLEAddress(bytes: number[]): string {\r\n return bytes.map(byte => {\r\n if (byte < 0) {\r\n byte += 256;\r\n }\r\n let byteString = '0' + byte.toString(16).toUpperCase();\r\n byteString = byteString.slice(-2);\r\n return byteString;\r\n })\r\n .reverse()\r\n .join(':')\r\n}","/**\n * Generated bundle index. Do not edit.\n */\n\nexport * from './public_api';\n"],"names":[],"mappings":";;;;;;AAAM,MAAO,QAAS,SAAQ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AAI,IAAI,GAAG,CAAC;AACf,SAAA;AACD,QAAA,IAAI,UAAU,GAAG,GAAG,GAAG,IAAI,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC,WAAW,EAAE,CAAC;QACvD,UAAU,GAAG,UAAU,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC;AAClC,QAAA,OAAO,UAAU,CAAC;AACtB,KAAC,CAAC;AACG,SAAA,OAAO,EAAE;SACT,IAAI,CAAC,GAAG,CAAC,CAAA;AAClB;;AC7DA;;AAEG;;;;"}
1
+ {"version":3,"file":"iotize-device-com-nfc.cordova.js","sources":["../../../../src/www/errors.ts","../../../../src/www/logger.ts","../../../../src/www/nfc-com-protocol.ts","../../../../src/www/tap-ndef/parse-ndef-message.ts","../../../../src/iotize-device-com-nfc.cordova.ts"],"sourcesContent":["export class NfcError extends Error {\r\n constructor(public code: NfcError.ErrorCode, message: string) {\r\n super(message);\r\n }\r\n\r\n public static tagLostError() {\r\n return new NfcError(NfcError.ErrorCode.TagLostError, 'NFC tag lost');\r\n }\r\n\r\n public static notConnectedError() {\r\n return new NfcError(\r\n NfcError.ErrorCode.NotConnectedError,\r\n 'NFC tag is not connected'\r\n );\r\n }\r\n\r\n public static unknownError(errString: string): NfcError {\r\n throw new NfcError(NfcError.ErrorCode.Unknown, errString);\r\n }\r\n\r\n public static tagConnectionFailed() {\r\n throw new NfcError(\r\n NfcError.ErrorCode.NotConnectedError,\r\n `Tag connection failed`\r\n );\r\n }\r\n\r\n public static internalError(message: string) {\r\n throw new NfcError(NfcError.ErrorCode.InternalError, message);\r\n }\r\n}\r\n\r\nexport namespace NfcError {\r\n export enum ErrorCode {\r\n Unknown = 'NfcUnknownError',\r\n InternalError = 'NfcInternalError',\r\n TagLostError = 'NfcTagLostError',\r\n NotConnectedError = 'NfcNotConnectedError',\r\n }\r\n}\r\n","import { createDebugger } from '@iotize/common/debug';\r\n\r\nexport const debug = createDebugger(`@iotize/device-com-nfc.cordova`);","import {\r\n bufferToHexString,\r\n hexStringToBuffer,\r\n} from '@iotize/common/byte-converter';\r\nimport {\r\n ComProtocolConnectOptions,\r\n ComProtocolDisconnectOptions,\r\n ComProtocolOptions,\r\n ComProtocolSendOptions,\r\n ConnectionState,\r\n} from '@iotize/tap/protocol/api';\r\nimport { QueueComProtocol } from '@iotize/tap/protocol/core';\r\nimport { defer, Observable } from 'rxjs';\r\n\r\nimport { CordovaInterface } from './cordova-interface';\r\nimport { NfcError } from './errors';\r\nimport { debug } from './logger';\r\n\r\ndeclare var nfc: CordovaInterface;\r\n\r\nexport class NFCComProtocol extends QueueComProtocol {\r\n constructor(\r\n options: ComProtocolOptions = {\r\n connect: {\r\n timeout: 2000,\r\n },\r\n disconnect: {\r\n timeout: 1000,\r\n },\r\n send: {\r\n timeout: 1000,\r\n },\r\n }\r\n ) {\r\n super();\r\n this.options = options;\r\n if (typeof nfc == undefined) {\r\n console.warn(\r\n 'NFC plugin has not been setup properly. Global variable NFC does not exist'\r\n );\r\n }\r\n }\r\n\r\n public static iOSProtocol(): NFCComProtocol {\r\n return new NFCComProtocol({\r\n connect: {\r\n timeout: 10000, // bigger timer on connect as connect launches a reading session\r\n },\r\n disconnect: {\r\n timeout: 1000,\r\n },\r\n send: {\r\n timeout: 1000,\r\n },\r\n });\r\n }\r\n\r\n /**\r\n * We force tag connection with nfc as we need to refresh tag\r\n * @param options\r\n * @returns\r\n */\r\n connect(options?: ComProtocolConnectOptions): Observable<void> {\r\n this.connectionState = ConnectionState.CONNECTING; // Hack to force NFC tag connect call even if we are already connected\r\n return super.connect(options);\r\n }\r\n\r\n /**\r\n * Not used as we have rewrote \"connect()\" function\r\n * @param options\r\n * @returns\r\n */\r\n _connect(options?: ComProtocolConnectOptions): Observable<any> {\r\n return defer(async () => {\r\n debug('_connect', options);\r\n try {\r\n await nfc.connect(\r\n 'android.nfc.tech.NfcV',\r\n this.options.connect.timeout\r\n );\r\n } catch (err) {\r\n if (typeof err === 'string') {\r\n if (err === 'Tag connection failed') {\r\n throw NfcError.tagConnectionFailed();\r\n } else {\r\n throw NfcError.unknownError(err);\r\n }\r\n }\r\n throw err;\r\n }\r\n });\r\n }\r\n\r\n _disconnect(options?: ComProtocolDisconnectOptions): Observable<any> {\r\n return defer(() => nfc.close());\r\n }\r\n\r\n /**\r\n * Not used\r\n * @param options\r\n * @returns\r\n */\r\n async write(data: Uint8Array): Promise<any> {\r\n throw new Error('Method not implemented.');\r\n }\r\n\r\n /**\r\n * Not used\r\n * @param options\r\n * @returns\r\n */\r\n async read(): Promise<Uint8Array> {\r\n throw new Error('Method not implemented.');\r\n }\r\n\r\n send(\r\n data: Uint8Array,\r\n options?: ComProtocolSendOptions\r\n ): Observable<Uint8Array> {\r\n return defer(async () => {\r\n try {\r\n const response = await nfc.transceive(bufferToHexString(data));\r\n if (typeof response != 'string') {\r\n throw NfcError.internalError(\r\n `Internal error. Plugin should respond a hexadecimal string`\r\n );\r\n }\r\n debug('NFC plugin response: ', response);\r\n return hexStringToBuffer(response);\r\n } catch (errString) {\r\n if (typeof errString === 'string') {\r\n const error = stringToError(errString);\r\n if (\r\n error.code === NfcError.ErrorCode.NotConnectedError ||\r\n error.code === NfcError.ErrorCode.TagLostError\r\n ) {\r\n this._onConnectionLost(error);\r\n }\r\n throw error;\r\n } else {\r\n throw errString;\r\n }\r\n }\r\n });\r\n }\r\n\r\n _onConnectionLost(error: NfcError) {\r\n if (this.connectionState !== ConnectionState.DISCONNECTED) {\r\n this.setConnectionState(ConnectionState.DISCONNECTED);\r\n }\r\n }\r\n}\r\n\r\n/**\r\n * Convert error string returned by the plugin into an error object\r\n * It only checks a few Android error string for now\r\n *\r\n * TODO complete implementation with other error types\r\n *\r\n * @param errString\r\n */\r\nfunction stringToError(errString: string): NfcError {\r\n const errStringLc = errString.toLowerCase();\r\n if (errStringLc.indexOf('tag was lost') >= 0) {\r\n return NfcError.tagLostError();\r\n } else if (errStringLc.indexOf('not connected') >= 0) {\r\n return NfcError.notConnectedError();\r\n } else {\r\n return NfcError.unknownError(errString);\r\n }\r\n}\r\n","import { bufferToAsciiString } from '@iotize/common/byte-converter';\r\n\r\nimport { NdefRecord, TapNfcTagPayload } from './definitions';\r\n\r\n/**\r\n * We manage only on NDEF message with 3 records:\r\n * record 0 = URI; record 1 = AAR; record 2 = BLE MAC ADDRESS / BSSID; record 3: universal link\r\n */\r\nexport function parseTapNdefMessage(messages: NdefRecord[]): TapNfcTagPayload {\r\n let result: TapNfcTagPayload = {};\r\n if (messages.length >= 1) {\r\n let asciiUri = messages[0].payload;\r\n result.uri = toAsciiString(asciiUri);\r\n }\r\n if (messages.length >= 2) {\r\n result.aar = toAsciiString(messages[1].payload);\r\n }\r\n if (messages.length >= 3) {\r\n let payload3 = messages[2].payload;\r\n let type = payload3[0];\r\n let content = payload3.slice(1);\r\n result.type = type;\r\n\r\n switch (type) {\r\n case TapNdefProtocolType.BLE:\r\n result.macAddress = convertBytesToBLEAddress(content);\r\n break;\r\n case TapNdefProtocolType.WiFi:\r\n result.ssid = toAsciiString(content);\r\n break;\r\n }\r\n }\r\n if (messages.length >= 4) {\r\n result.name = toAsciiString(messages[3].payload);\r\n }\r\n return result;\r\n}\r\n\r\nenum TapNdefProtocolType {\r\n WiFi = 0x20,\r\n BLE = 0x40\r\n}\r\n\r\nfunction toAsciiString(payload: number[]): string {\r\n if (payload.length > 0 && payload[0] === 0) {\r\n payload = payload.slice(1);\r\n }\r\n return bufferToAsciiString(payload as any as Uint8Array);\r\n}\r\n\r\nfunction convertBytesToBLEAddress(bytes: number[]): string {\r\n return bytes.map(byte => {\r\n if (byte < 0) {\r\n byte += 256;\r\n }\r\n let byteString = '0' + byte.toString(16).toUpperCase();\r\n byteString = byteString.slice(-2);\r\n return byteString;\r\n })\r\n .reverse()\r\n .join(':')\r\n}","/**\n * Generated bundle index. Do not edit.\n */\n\nexport * from './public_api';\n"],"names":[],"mappings":";;;;;;AAAM,MAAO,QAAS,SAAQ,KAAK,CAAA;IACjC,WAAmB,CAAA,IAAwB,EAAE,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olic":"class","extends":{"__symbolic":"reference","module":"@iotize/tap/protocol/core","name":"QueueComProtocol","line":24,"character":36},"members":{"__ctor__":[{"__symbolic":"constructor","parameters":[{"__symbolic":"reference","module":"@iotize/tap/protocol/api","name":"ComProtocolOptions","line":26,"character":25}]}],"_connect":[{"__symbolic":"method"}],"_disconnect":[{"__symbolic":"method"}],"write":[{"__symbolic":"method"}],"read":[{"__symbolic":"method"}],"send":[{"__symbolic":"method"}],"_onConnectionLost":[{"__symbolic":"method"}]},"statics":{"iOSProtocol":{"__symbolic":"function","parameters":[],"value":{"__symbolic":"new","expression":{"__symbolic":"reference","name":"NFCComProtocol"},"arguments":[{"connect":{"timeout":10000},"disconnect":{"timeout":1000},"send":{"timeout":1000}}]}}}},"CordovaInterface":{"__symbolic":"interface"},"NfcError":{"__symbolic":"class","extends":{"__symbolic":"reference","name":"Error"},"members":{"__ctor__":[{"__symbolic":"constructor","parameters":[{"__symbolic":"select","expression":{"__symbolic":"reference","name":"NfcError"},"member":"ErrorCode"},{"__symbolic":"reference","name":"string"}]}]},"statics":{"tagLostError":{"__symbolic":"function","parameters":[],"value":{"__symbolic":"new","expression":{"__symbolic":"reference","name":"NfcError"},"arguments":[{"__symbolic":"select","expression":{"__symbolic":"select","expression":{"__symbolic":"reference","name":"NfcError"},"member":"ErrorCode"},"member":"TagLostError"},"NFC tag lost"]}},"notConnectedError":{"__symbolic":"function","parameters":[],"value":{"__symbolic":"new","expression":{"__symbolic":"reference","name":"NfcError"},"arguments":[{"__symbolic":"select","expression":{"__symbolic":"select","expression":{"__symbolic":"reference","name":"NfcError"},"member":"ErrorCode"},"member":"NotConnectedError"},"NFC tag is not connected"]}}}}},"origins":{"NFCComProtocol":"./www/nfc-com-protocol","CordovaInterface":"./www/cordova-interface","NfcError":"./www/errors"},"importAs":"@iotize/device-com-nfc.cordova"}
1
+ {"__symbolic":"module","version":4,"exports":[{"from":"./www/definitions"},{"from":"./www/parse-ndef-message"}],"metadata":{"NFCComProtocol":{"__symbolic":"class","extends":{"__symbolic":"reference","module":"@iotize/tap/protocol/core","name":"QueueComProtocol","line":20,"character":36},"members":{"__ctor__":[{"__symbolic":"constructor","parameters":[{"__symbolic":"reference","module":"@iotize/tap/protocol/api","name":"ComProtocolOptions","line":22,"character":13}]}],"connect":[{"__symbolic":"method"}],"_connect":[{"__symbolic":"method"}],"_disconnect":[{"__symbolic":"method"}],"write":[{"__symbolic":"method"}],"read":[{"__symbolic":"method"}],"send":[{"__symbolic":"method"}],"_onConnectionLost":[{"__symbolic":"method"}]},"statics":{"iOSProtocol":{"__symbolic":"function","parameters":[],"value":{"__symbolic":"new","expression":{"__symbolic":"reference","name":"NFCComProtocol"},"arguments":[{"connect":{"timeout":10000},"disconnect":{"timeout":1000},"send":{"timeout":1000}}]}}}},"CordovaInterface":{"__symbolic":"interface"},"NfcError":{"__symbolic":"class","extends":{"__symbolic":"reference","name":"Error"},"members":{"__ctor__":[{"__symbolic":"constructor","parameters":[{"__symbolic":"select","expression":{"__symbolic":"reference","name":"NfcError"},"member":"ErrorCode"},{"__symbolic":"reference","name":"string"}]}]},"statics":{"tagLostError":{"__symbolic":"function","parameters":[],"value":{"__symbolic":"new","expression":{"__symbolic":"reference","name":"NfcError"},"arguments":[{"__symbolic":"select","expression":{"__symbolic":"select","expression":{"__symbolic":"reference","name":"NfcError"},"member":"ErrorCode"},"member":"TagLostError"},"NFC tag lost"]}},"notConnectedError":{"__symbolic":"function","parameters":[],"value":{"__symbolic":"new","expression":{"__symbolic":"reference","name":"NfcError"},"arguments":[{"__symbolic":"select","expression":{"__symbolic":"select","expression":{"__symbolic":"reference","name":"NfcError"},"member":"ErrorCode"},"member":"NotConnectedError"},"NFC tag is not connected"]}}}}},"origins":{"NFCComProtocol":"./www/nfc-com-protocol","CordovaInterface":"./www/cordova-interface","NfcError":"./www/errors"},"importAs":"@iotize/device-com-nfc.cordova"}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@iotize/device-com-nfc.cordova",
3
3
  "main": "bundles/iotize-device-com-nfc.cordova.umd.js",
4
- "version": "3.1.4",
4
+ "version": "3.2.0",
5
5
  "description": "Near Field Communication (NFC) Plugin. Read and write NDEF messages to NFC tags and share NDEF messages with peers.",
6
6
  "cordova": {
7
7
  "id": "@iotize/device-com-nfc.cordova",
@@ -602,7 +602,11 @@ public class NfcPlugin extends CordovaPlugin implements NfcAdapter.OnNdefPushCom
602
602
  Activity activity = getActivity();
603
603
  Intent intent = new Intent(activity, activity.getClass());
604
604
  intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
605
- pendingIntent = PendingIntent.getActivity(activity, 0, intent, 0);
605
+ int pendingIntentFlags = 0;
606
+ if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
607
+ pendingIntentFlags = PendingIntent.FLAG_MUTABLE;
608
+ }
609
+ pendingIntent = PendingIntent.getActivity(activity, 0, intent, pendingIntentFlags);
606
610
  }
607
611
  }
608
612
 
@@ -1093,27 +1097,12 @@ public class NfcPlugin extends CordovaPlugin implements NfcAdapter.OnNdefPushCom
1093
1097
 
1094
1098
  nfcProtocol.connect();
1095
1099
  setTimeout(timeout);
1096
- Log.d(TAG, "NFC Connection succesful");
1100
+ Log.d(TAG, "NFC Connection successful");
1097
1101
  callbackContext.success();
1098
-
1099
1102
  } catch (IOException ex) {
1100
1103
  Log.e(TAG, "Tag connection failed", ex);
1101
1104
  callbackContext.error("Tag connection failed");
1102
-
1103
- // Users should never get these reflection errors
1104
- } catch (ClassNotFoundException e) {
1105
- Log.e(TAG, e.getMessage(), e);
1106
- callbackContext.error(e.getMessage());
1107
- } catch (NoSuchMethodException e) {
1108
- Log.e(TAG, e.getMessage(), e);
1109
- callbackContext.error(e.getMessage());
1110
- } catch (IllegalAccessException e) {
1111
- Log.e(TAG, e.getMessage(), e);
1112
- callbackContext.error(e.getMessage());
1113
- } catch (InvocationTargetException e) {
1114
- Log.e(TAG, e.getMessage(), e);
1115
- callbackContext.error(e.getMessage());
1116
- } catch (Exception e) {
1105
+ } catch (Throwable e) {
1117
1106
  Log.e(TAG, e.getMessage(), e);
1118
1107
  callbackContext.error(e.getMessage());
1119
1108
  }
package/www/errors.d.ts CHANGED
@@ -4,6 +4,7 @@ export declare class NfcError extends Error {
4
4
  static tagLostError(): NfcError;
5
5
  static notConnectedError(): NfcError;
6
6
  static unknownError(errString: string): NfcError;
7
+ static tagConnectionFailed(): void;
7
8
  static internalError(message: string): void;
8
9
  }
9
10
  export declare namespace NfcError {
@@ -5,9 +5,30 @@ import { NfcError } from './errors';
5
5
  export declare class NFCComProtocol extends QueueComProtocol {
6
6
  constructor(options?: ComProtocolOptions);
7
7
  static iOSProtocol(): NFCComProtocol;
8
+ /**
9
+ * We force tag connection with nfc as we need to refresh tag
10
+ * @param options
11
+ * @returns
12
+ */
13
+ connect(options?: ComProtocolConnectOptions): Observable<void>;
14
+ /**
15
+ * Not used as we have rewrote "connect()" function
16
+ * @param options
17
+ * @returns
18
+ */
8
19
  _connect(options?: ComProtocolConnectOptions): Observable<any>;
9
20
  _disconnect(options?: ComProtocolDisconnectOptions): Observable<any>;
21
+ /**
22
+ * Not used
23
+ * @param options
24
+ * @returns
25
+ */
10
26
  write(data: Uint8Array): Promise<any>;
27
+ /**
28
+ * Not used
29
+ * @param options
30
+ * @returns
31
+ */
11
32
  read(): Promise<Uint8Array>;
12
33
  send(data: Uint8Array, options?: ComProtocolSendOptions): Observable<Uint8Array>;
13
34
  _onConnectionLost(error: NfcError): void;