@iotize/device-com-nfc.cordova 3.1.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 (76) hide show
  1. package/LICENSE +31 -0
  2. package/README.md +673 -0
  3. package/bundles/iotize-device-com-nfc.cordova.umd.js +469 -0
  4. package/bundles/iotize-device-com-nfc.cordova.umd.js.map +1 -0
  5. package/esm2015/iotize-device-com-nfc.cordova.js +5 -0
  6. package/esm2015/iotize-device-com-nfc.cordova.js.map +1 -0
  7. package/esm2015/iotize-device-com-nfc.cordova.metadata.json +1 -0
  8. package/esm2015/iotize-device-com-nfc.cordova.ngsummary.json +1 -0
  9. package/esm2015/public_api.js +2 -0
  10. package/esm2015/public_api.js.map +1 -0
  11. package/esm2015/public_api.metadata.json +1 -0
  12. package/esm2015/public_api.ngsummary.json +1 -0
  13. package/esm2015/www/cordova-interface.js +2 -0
  14. package/esm2015/www/cordova-interface.js.map +1 -0
  15. package/esm2015/www/cordova-interface.metadata.json +1 -0
  16. package/esm2015/www/cordova-interface.ngsummary.json +1 -0
  17. package/esm2015/www/errors.js +28 -0
  18. package/esm2015/www/errors.js.map +1 -0
  19. package/esm2015/www/errors.metadata.json +1 -0
  20. package/esm2015/www/errors.ngsummary.json +1 -0
  21. package/esm2015/www/index.js +5 -0
  22. package/esm2015/www/index.js.map +1 -0
  23. package/esm2015/www/index.metadata.json +1 -0
  24. package/esm2015/www/index.ngsummary.json +1 -0
  25. package/esm2015/www/logger.js +3 -0
  26. package/esm2015/www/logger.js.map +1 -0
  27. package/esm2015/www/logger.metadata.json +1 -0
  28. package/esm2015/www/logger.ngsummary.json +1 -0
  29. package/esm2015/www/nfc-com-protocol.js +109 -0
  30. package/esm2015/www/nfc-com-protocol.js.map +1 -0
  31. package/esm2015/www/nfc-com-protocol.metadata.json +1 -0
  32. package/esm2015/www/nfc-com-protocol.ngsummary.json +1 -0
  33. package/esm2015/www/tap-ndef/definitions.js +2 -0
  34. package/esm2015/www/tap-ndef/definitions.js.map +1 -0
  35. package/esm2015/www/tap-ndef/definitions.metadata.json +1 -0
  36. package/esm2015/www/tap-ndef/definitions.ngsummary.json +1 -0
  37. package/esm2015/www/tap-ndef/index.js +3 -0
  38. package/esm2015/www/tap-ndef/index.js.map +1 -0
  39. package/esm2015/www/tap-ndef/index.metadata.json +1 -0
  40. package/esm2015/www/tap-ndef/index.ngsummary.json +1 -0
  41. package/esm2015/www/tap-ndef/parse-ndef-message.js +57 -0
  42. package/esm2015/www/tap-ndef/parse-ndef-message.js.map +1 -0
  43. package/esm2015/www/tap-ndef/parse-ndef-message.metadata.json +1 -0
  44. package/esm2015/www/tap-ndef/parse-ndef-message.ngsummary.json +1 -0
  45. package/fesm2015/iotize-device-com-nfc.cordova.js +196 -0
  46. package/fesm2015/iotize-device-com-nfc.cordova.js.map +1 -0
  47. package/iotize-device-com-nfc.cordova.d.ts +4 -0
  48. package/iotize-device-com-nfc.cordova.metadata.json +1 -0
  49. package/package.json +54 -0
  50. package/plugin.xml +97 -0
  51. package/public_api.d.ts +1 -0
  52. package/src/android/build.gradle +38 -0
  53. package/src/android/gradle/wrapper/gradle-wrapper.jar +0 -0
  54. package/src/android/gradle/wrapper/gradle-wrapper.properties +6 -0
  55. package/src/android/gradlew +172 -0
  56. package/src/android/gradlew.bat +84 -0
  57. package/src/android/local.properties +8 -0
  58. package/src/android/src/com/chariotsolutions/nfc/plugin/JSONBuilder.java +60 -0
  59. package/src/android/src/com/chariotsolutions/nfc/plugin/NfcPlugin.java +1192 -0
  60. package/src/android/src/com/chariotsolutions/nfc/plugin/NfcPluginError.java +15 -0
  61. package/src/android/src/com/chariotsolutions/nfc/plugin/PluginResponse.java +85 -0
  62. package/src/android/src/com/chariotsolutions/nfc/plugin/Util.java +140 -0
  63. package/src/ios/AppDelegate+NFC.swift +51 -0
  64. package/src/ios/ISO15Reader.swift +376 -0
  65. package/src/ios/NFCNDEFDelegate.swift +78 -0
  66. package/src/ios/NFCPlugin-Bridging-Header.h +7 -0
  67. package/src/ios/NFCTapPlugin.swift +251 -0
  68. package/www/cordova-interface.d.ts +18 -0
  69. package/www/errors.d.ts +16 -0
  70. package/www/index.d.ts +4 -0
  71. package/www/logger.d.ts +2 -0
  72. package/www/nfc-com-protocol.d.ts +14 -0
  73. package/www/phonegap-nfc.js +885 -0
  74. package/www/tap-ndef/definitions.d.ts +25 -0
  75. package/www/tap-ndef/index.d.ts +2 -0
  76. package/www/tap-ndef/parse-ndef-message.d.ts +6 -0
@@ -0,0 +1,78 @@
1
+ //
2
+ // NFCNDEFDelegate.swift
3
+ // NFC
4
+ //
5
+ // Created by dev@iotize.com on 05/08/2019.
6
+ // Copyright © 2019 dev@iotize.com. All rights reserved.
7
+ //
8
+
9
+ import Foundation
10
+ import CoreNFC
11
+
12
+ class NFCNDEFDelegate: NSObject, NFCNDEFReaderSessionDelegate {
13
+ var session: NFCNDEFReaderSession?
14
+ var completed: ([AnyHashable : Any]?, Error?) -> ()
15
+
16
+ init(completed: @escaping ([AnyHashable: Any]?, Error?) -> (), message: String?) {
17
+ self.completed = completed
18
+ super.init()
19
+ self.session = NFCNDEFReaderSession.init(delegate: self, queue: nil, invalidateAfterFirstRead: false)
20
+ if (self.session == nil) {
21
+ self.completed(nil, "NFC is not available" as? Error);
22
+ return
23
+ }
24
+ self.session!.alertMessage = message ?? ""
25
+ self.session!.begin()
26
+ }
27
+
28
+ func readerSession(_: NFCNDEFReaderSession, didDetectNDEFs messages: [NFCNDEFMessage]) {
29
+ for message in messages {
30
+ self.fireNdefEvent(message: message)
31
+ }
32
+ self.session?.invalidate()
33
+ }
34
+
35
+ func readerSession(_: NFCNDEFReaderSession, didInvalidateWithError _: Error) {
36
+ completed(nil, "NFCNDEFReaderSession error" as? Error)
37
+ }
38
+
39
+ func readerSessionDidBecomeActive(_: NFCNDEFReaderSession) {
40
+ print("NDEF Reader session active")
41
+ }
42
+
43
+ func fireNdefEvent(message: NFCNDEFMessage) {
44
+ let response = message.ndefMessageToJSON()
45
+ completed(response, nil)
46
+ }
47
+
48
+
49
+
50
+ }
51
+
52
+ extension NFCNDEFMessage {
53
+ func ndefMessageToJSON() -> [AnyHashable: Any] {
54
+ let array = NSMutableArray()
55
+ for record in self.records {
56
+ let recordDictionary = self.ndefToNSDictionary(record: record)
57
+ array.add(recordDictionary)
58
+ }
59
+ let wrapper = NSMutableDictionary()
60
+ wrapper.setObject(array, forKey: "ndefMessage" as NSString)
61
+
62
+ let returnedJSON = NSMutableDictionary()
63
+ returnedJSON.setValue("ndef", forKey: "type")
64
+ returnedJSON.setObject(wrapper, forKey: "tag" as NSString)
65
+
66
+ return returnedJSON as! [AnyHashable : Any]
67
+ }
68
+
69
+ func ndefToNSDictionary(record: NFCNDEFPayload) -> NSDictionary {
70
+ let dict = NSMutableDictionary()
71
+ dict.setObject([record.typeNameFormat.rawValue], forKey: "tnf" as NSString)
72
+ dict.setObject([UInt8](record.type), forKey: "type" as NSString)
73
+ dict.setObject([UInt8](record.identifier), forKey: "id" as NSString)
74
+ dict.setObject([UInt8](record.payload), forKey: "payload" as NSString)
75
+
76
+ return dict
77
+ }
78
+ }
@@ -0,0 +1,7 @@
1
+ //
2
+ // NFCPlugin-Bridging-Header.h
3
+ // PhoneGap NFC - Cordova Plugin
4
+ //
5
+ // (c) 2018 IoTize Solutions
6
+
7
+ #import "../../Classes/AppDelegate.h"
@@ -0,0 +1,251 @@
1
+ //
2
+ // NFCTapPlugin.swift
3
+ // NFC
4
+ //
5
+ // Created by dev@iotize.com on 23/07/2019.
6
+ // Copyright © 2019 dev@iotize.com. All rights reserved.
7
+ //
8
+
9
+ import Foundation
10
+ import UIKit
11
+ import CoreNFC
12
+
13
+ // Main class handling the plugin functionalities.
14
+ @objc(NfcPlugin) class NfcPlugin: CDVPlugin {
15
+ var nfcController: NSObject? // ST25DVReader downCast as NSObject for iOS version compatibility
16
+ var ndefController: NFCNDEFDelegate?
17
+ var lastError: Error?
18
+ var channelCommand: CDVInvokedUrlCommand?
19
+ var isListeningNDEF = false
20
+
21
+ // helper to return a string
22
+ func sendSuccess(command: CDVInvokedUrlCommand, result: String) {
23
+ let pluginResult = CDVPluginResult(
24
+ status: CDVCommandStatus_OK,
25
+ messageAs: result
26
+ )
27
+ commandDelegate!.send(pluginResult, callbackId: command.callbackId)
28
+ }
29
+
30
+ // helper to return a boolean
31
+ private func sendSuccess(command: CDVInvokedUrlCommand, result: Bool) {
32
+ let pluginResult = CDVPluginResult(
33
+ status: CDVCommandStatus_OK,
34
+ messageAs: result
35
+ )
36
+ commandDelegate!.send(pluginResult, callbackId: command.callbackId)
37
+ }
38
+
39
+ // helper to return a String with keeping the callback
40
+ func sendSuccessWithResponse(command: CDVInvokedUrlCommand, result: String) {
41
+ let pluginResult = CDVPluginResult(
42
+ status: CDVCommandStatus_OK,
43
+ messageAs: result
44
+ )
45
+ pluginResult!.setKeepCallbackAs(true)
46
+ commandDelegate!.send(pluginResult, callbackId: command.callbackId)
47
+ }
48
+
49
+ // helper to send back an error
50
+ func sendError(command: CDVInvokedUrlCommand, result: String) {
51
+ let pluginResult = CDVPluginResult(
52
+ status: CDVCommandStatus_ERROR,
53
+ messageAs: result
54
+ )
55
+ commandDelegate!.send(pluginResult, callbackId: command.callbackId)
56
+ }
57
+
58
+ @objc(connect:)
59
+ func connect(command: CDVInvokedUrlCommand) {
60
+ guard #available(iOS 13.0, *) else {
61
+ sendError(command: command, result: "connect is only available on iOS 13+")
62
+ return
63
+ }
64
+ DispatchQueue.main.async {
65
+ print("Begin session \(self.nfcController)")
66
+ if self.nfcController == nil {
67
+ self.nfcController = ST25DVReader()
68
+ }
69
+
70
+ (self.nfcController as! ST25DVReader).initSession(alertMessage: "Bring your phone close to the Tap.", completed: {
71
+ (error: Error?) -> Void in
72
+
73
+ DispatchQueue.main.async {
74
+ if error != nil {
75
+ self.sendError(command: command, result: error!.localizedDescription)
76
+ } else {
77
+ self.sendSuccess(command: command, result: "")
78
+ }
79
+ }
80
+ })
81
+ }
82
+ }
83
+
84
+ @objc(close:)
85
+ func close(command: CDVInvokedUrlCommand) {
86
+ guard #available(iOS 13.0, *) else {
87
+ sendError(command: command, result: "close is only available on iOS 13+")
88
+ return
89
+ }
90
+ DispatchQueue.main.async {
91
+ if self.nfcController == nil {
92
+ self.sendError(command: command, result: "no session to terminate")
93
+ return
94
+ }
95
+
96
+ (self.nfcController as! ST25DVReader).invalidateSession(message: "Sesssion Ended!")
97
+ self.nfcController = nil
98
+ }
99
+ }
100
+
101
+ @objc(transceive:)
102
+ func transceive(command: CDVInvokedUrlCommand) {
103
+ guard #available(iOS 13.0, *) else {
104
+ sendError(command: command, result: "transceive is only available on iOS 13+")
105
+ return
106
+ }
107
+ DispatchQueue.main.async {
108
+ print("sending ...")
109
+ if self.nfcController == nil {
110
+ self.sendError(command: command, result: "no session available")
111
+ return
112
+ }
113
+
114
+ // we need data to send
115
+ if command.arguments.count <= 0 {
116
+ self.sendError(command: command, result: "SendRequest parameter error")
117
+ return
118
+ }
119
+
120
+ guard let data: NSData = command.arguments[0] as? NSData else {
121
+ self.sendError(command: command, result: "Tried to transceive empty string")
122
+ return
123
+ }
124
+ let request = data.map { String(format: "%02x", $0) }
125
+ .joined()
126
+ print("send request - \(request)")
127
+
128
+ (self.nfcController as! ST25DVReader).send(request: request, completed: {
129
+ (response: Data?, error: Error?) -> Void in
130
+
131
+ DispatchQueue.main.async {
132
+ if error != nil {
133
+ self.lastError = error
134
+ self.sendError(command: command, result: error!.localizedDescription)
135
+ } else {
136
+ print("responded \(response!.hexEncodedString())")
137
+ self.sendSuccess(command: command, result: response!.hexEncodedString())
138
+ }
139
+ }
140
+ })
141
+ }
142
+ }
143
+
144
+ @objc(registerNdef:)
145
+ func registerNdef(command: CDVInvokedUrlCommand) {
146
+ print("Registered NDEF Listener")
147
+ isListeningNDEF = true // Flag for the AppDelegate
148
+ sendSuccess(command: command, result: "NDEF Listener is on")
149
+ }
150
+
151
+ @objc(registerMimeType:)
152
+ func registerMimeType(command: CDVInvokedUrlCommand) {
153
+ print("Registered Mi Listener")
154
+ sendSuccess(command: command, result: "NDEF Listener is on")
155
+ }
156
+
157
+ @objc(beginNDEFSession:)
158
+ func beginNDEFSession(command: CDVInvokedUrlCommand) {
159
+ DispatchQueue.main.async {
160
+ print("Begin NDEF reading session")
161
+
162
+ if self.ndefController == nil {
163
+ var message: String?
164
+ if command.arguments.count != 0 {
165
+ message = command.arguments[0] as? String ?? ""
166
+ }
167
+ self.ndefController = NFCNDEFDelegate(completed: {
168
+ (response: [AnyHashable: Any]?, error: Error?) -> Void in
169
+ DispatchQueue.main.async {
170
+ print("handle NDEF")
171
+ if error != nil {
172
+ self.lastError = error
173
+ self.sendError(command: command, result: error!.localizedDescription)
174
+ } else {
175
+ // self.sendSuccess(command: command, result: response ?? "")
176
+ self.sendThroughChannel(jsonDictionary: response ?? [:])
177
+ }
178
+ self.ndefController = nil
179
+ }
180
+ }, message: message)
181
+ }
182
+ }
183
+ }
184
+
185
+ @objc(invalidateNDEFSession:)
186
+ func invalidateNDEFSession(command: CDVInvokedUrlCommand) {
187
+ guard #available(iOS 11.0, *) else {
188
+ sendError(command: command, result: "close is only available on iOS 13+")
189
+ return
190
+ }
191
+ DispatchQueue.main.async {
192
+ guard let session = self.ndefController?.session else {
193
+ self.sendError(command: command, result: "no session to terminate")
194
+ return
195
+ }
196
+
197
+ session.invalidate()
198
+ self.nfcController = nil
199
+ self.sendSuccess(command: command, result: "Session Ended!")
200
+ }
201
+ }
202
+
203
+ @objc(channel:)
204
+ func channel(command: CDVInvokedUrlCommand) {
205
+ DispatchQueue.main.async {
206
+ print("Creating NDEF Channel")
207
+ self.channelCommand = command
208
+ self.sendThroughChannel(message: "Did create NDEF Channel")
209
+ }
210
+ }
211
+
212
+ func sendThroughChannel(message: String) {
213
+ guard let command: CDVInvokedUrlCommand = self.channelCommand else {
214
+ print("Channel is not set")
215
+ return
216
+ }
217
+ guard let response = CDVPluginResult(status: CDVCommandStatus_OK, messageAs: message) else {
218
+ print("sendThroughChannel Did not create CDVPluginResult")
219
+ return
220
+ }
221
+
222
+ response.setKeepCallbackAs(true)
223
+ commandDelegate!.send(response, callbackId: command.callbackId)
224
+ }
225
+
226
+ func sendThroughChannel(jsonDictionary: [AnyHashable: Any]) {
227
+ guard let command: CDVInvokedUrlCommand = self.channelCommand else {
228
+ print("Channel is not set")
229
+ return
230
+ }
231
+ guard let response = CDVPluginResult(status: CDVCommandStatus_OK, messageAs: jsonDictionary) else {
232
+ print("sendThroughChannel Did not create CDVPluginResult")
233
+ return
234
+ }
235
+
236
+ response.setKeepCallbackAs(true)
237
+ commandDelegate!.send(response, callbackId: command.callbackId)
238
+
239
+ // self.sendSuccessWithResponse(command: command, result: message)
240
+ }
241
+
242
+ @objc(enabled:)
243
+ func enabled(command: CDVInvokedUrlCommand) {
244
+ guard #available(iOS 11.0, *) else {
245
+ sendError(command: command, result: "enabled is only available on iOS 11+")
246
+ return
247
+ }
248
+ let enabled = NFCReaderSession.readingAvailable
249
+ sendSuccess(command: command, result: enabled)
250
+ }
251
+ }
@@ -0,0 +1,18 @@
1
+ /**
2
+ * Partial typings for phonegap nfc cordova interface class
3
+ */
4
+ export interface CordovaInterface {
5
+ /**
6
+ * Close current nfc tag
7
+ */
8
+ close(): Promise<void>;
9
+ /**
10
+ * Connect to current nfc tag
11
+ */
12
+ connect(tech: string, timeout: number): Promise<void>;
13
+ /**
14
+ * Transeive data using
15
+ * @param data ArrayBuffer or string of hex data for transcieve
16
+ */
17
+ transceive(data: ArrayBuffer | string): Promise<string>;
18
+ }
@@ -0,0 +1,16 @@
1
+ export declare class NfcError extends Error {
2
+ code: NfcError.ErrorCode;
3
+ constructor(code: NfcError.ErrorCode, message: string);
4
+ static tagLostError(): NfcError;
5
+ static notConnectedError(): NfcError;
6
+ static unknownError(errString: string): NfcError;
7
+ static internalError(message: string): void;
8
+ }
9
+ export declare namespace NfcError {
10
+ enum ErrorCode {
11
+ Unknown = "NfcUnknownError",
12
+ InternalError = "NfcInternalError",
13
+ TagLostError = "NfcTagLostError",
14
+ NotConnectedError = "NfcNotConnectedError"
15
+ }
16
+ }
package/www/index.d.ts ADDED
@@ -0,0 +1,4 @@
1
+ export * from './nfc-com-protocol';
2
+ export * from './cordova-interface';
3
+ export * from './errors';
4
+ export * from './tap-ndef';
@@ -0,0 +1,2 @@
1
+ /// <reference types="@iotize/common/debug/@types/debug" />
2
+ export declare const debug: import("debug").DebugFct;
@@ -0,0 +1,14 @@
1
+ import { ComProtocolConnectOptions, ComProtocolDisconnectOptions, ComProtocolOptions, ComProtocolSendOptions } from '@iotize/tap/protocol/api';
2
+ import { QueueComProtocol } from '@iotize/tap/protocol/core';
3
+ import { Observable } from 'rxjs';
4
+ import { NfcError } from './errors';
5
+ export declare class NFCComProtocol extends QueueComProtocol {
6
+ constructor(options?: ComProtocolOptions);
7
+ static iOSProtocol(): NFCComProtocol;
8
+ _connect(options?: ComProtocolConnectOptions): Observable<any>;
9
+ _disconnect(options?: ComProtocolDisconnectOptions): Observable<any>;
10
+ write(data: Uint8Array): Promise<any>;
11
+ read(): Promise<Uint8Array>;
12
+ send(data: Uint8Array, options?: ComProtocolSendOptions): Observable<Uint8Array>;
13
+ _onConnectionLost(error: NfcError): void;
14
+ }