@exxili/capacitor-nfc 0.0.13 → 0.0.14
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/ExxiliCapacitorNfc.podspec +17 -17
- package/Package.swift +28 -28
- package/README.md +597 -595
- package/android/build.gradle +67 -67
- package/android/src/main/AndroidManifest.xml +2 -2
- package/android/src/main/kotlin/com/exxili/capacitornfc/MainActivity.kt +11 -11
- package/android/src/main/kotlin/com/exxili/capacitornfc/NFCPlugin.kt +492 -464
- package/dist/esm/definitions.d.ts +13 -0
- package/dist/esm/definitions.js.map +1 -1
- package/dist/esm/index.js +1 -0
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/web.d.ts +4 -1
- package/dist/esm/web.js +3 -0
- package/dist/esm/web.js.map +1 -1
- package/dist/plugin.cjs.js +4 -0
- package/dist/plugin.cjs.js.map +1 -1
- package/dist/plugin.js +4 -0
- package/dist/plugin.js.map +1 -1
- package/ios/Sources/NFCPlugin/NFCPlugin.swift +181 -169
- package/ios/Sources/NFCPlugin/NFCReader.swift +507 -507
- package/ios/Sources/NFCPlugin/NFCWriter.swift +89 -89
- package/ios/Tests/NFCPluginTests/NFCPluginTests.swift +15 -15
- package/package.json +90 -90
|
@@ -1,169 +1,181 @@
|
|
|
1
|
-
import Foundation
|
|
2
|
-
import Capacitor
|
|
3
|
-
import CoreNFC
|
|
4
|
-
|
|
5
|
-
@objc(NFCPlugin)
|
|
6
|
-
public class NFCPlugin: CAPPlugin, CAPBridgedPlugin {
|
|
7
|
-
public let identifier = "NFCPlugin"
|
|
8
|
-
public let jsName = "NFC"
|
|
9
|
-
public let pluginMethods: [CAPPluginMethod] = [
|
|
10
|
-
CAPPluginMethod(name: "isSupported", returnType: CAPPluginReturnPromise),
|
|
11
|
-
CAPPluginMethod(name: "cancelWriteAndroid", returnType: CAPPluginReturnPromise),
|
|
12
|
-
CAPPluginMethod(name: "startScan", returnType: CAPPluginReturnPromise),
|
|
13
|
-
CAPPluginMethod(name: "cancelScan", returnType: CAPPluginReturnPromise),
|
|
14
|
-
CAPPluginMethod(name: "writeNDEF", returnType: CAPPluginReturnPromise)
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
private let
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
reader.
|
|
94
|
-
call.resolve()
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
@objc func
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
print("Skipping record due to missing or invalid
|
|
116
|
-
continue
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
let
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
}
|
|
1
|
+
import Foundation
|
|
2
|
+
import Capacitor
|
|
3
|
+
import CoreNFC
|
|
4
|
+
|
|
5
|
+
@objc(NFCPlugin)
|
|
6
|
+
public class NFCPlugin: CAPPlugin, CAPBridgedPlugin {
|
|
7
|
+
public let identifier = "NFCPlugin"
|
|
8
|
+
public let jsName = "NFC"
|
|
9
|
+
public let pluginMethods: [CAPPluginMethod] = [
|
|
10
|
+
CAPPluginMethod(name: "isSupported", returnType: CAPPluginReturnPromise),
|
|
11
|
+
CAPPluginMethod(name: "cancelWriteAndroid", returnType: CAPPluginReturnPromise),
|
|
12
|
+
CAPPluginMethod(name: "startScan", returnType: CAPPluginReturnPromise),
|
|
13
|
+
CAPPluginMethod(name: "cancelScan", returnType: CAPPluginReturnPromise),
|
|
14
|
+
CAPPluginMethod(name: "writeNDEF", returnType: CAPPluginReturnPromise),
|
|
15
|
+
CAPPluginMethod(name: "getStatus", returnType: CAPPluginReturnPromise)
|
|
16
|
+
]
|
|
17
|
+
|
|
18
|
+
private let reader = NFCReader()
|
|
19
|
+
private let writer = NFCWriter()
|
|
20
|
+
|
|
21
|
+
private func isNfcAvailable() -> Bool {
|
|
22
|
+
NFCNDEFReaderSession.readingAvailable
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
@objc func isSupported(_ call: CAPPluginCall) {
|
|
26
|
+
call.resolve(["supported": isNfcAvailable()])
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
@objc func cancelWriteAndroid(_ call: CAPPluginCall) {
|
|
30
|
+
call.reject("Function not implemented for iOS")
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
@objc func startScan(_ call: CAPPluginCall) {
|
|
34
|
+
print("startScan called")
|
|
35
|
+
if let preferredMode = call.getString("mode") {
|
|
36
|
+
reader.setPreferredReaderMode(preferredMode)
|
|
37
|
+
} else if call.getBool("forceFull") == true {
|
|
38
|
+
reader.setPreferredReaderMode("full")
|
|
39
|
+
} else if call.getBool("forceCompat") == true {
|
|
40
|
+
reader.setPreferredReaderMode("compat")
|
|
41
|
+
} else if call.getBool("forceNDEF") == true {
|
|
42
|
+
reader.setPreferredReaderMode("ndef")
|
|
43
|
+
}
|
|
44
|
+
reader.onNDEFMessageReceived = { messages, tagInfo in
|
|
45
|
+
var ndefMessages = [[String: Any]]()
|
|
46
|
+
|
|
47
|
+
if messages.isEmpty {
|
|
48
|
+
// If no NDEF messages but we have tag info, create a fallback record with the UID
|
|
49
|
+
if let tagInfo = tagInfo, let uid = tagInfo["uid"] as? String {
|
|
50
|
+
let payloadData = uid.data(using: .utf8)?.base64EncodedString() ?? ""
|
|
51
|
+
let records = [[
|
|
52
|
+
"type": "ID",
|
|
53
|
+
"payload": payloadData
|
|
54
|
+
]]
|
|
55
|
+
ndefMessages.append([
|
|
56
|
+
"records": records
|
|
57
|
+
])
|
|
58
|
+
}
|
|
59
|
+
} else {
|
|
60
|
+
for message in messages {
|
|
61
|
+
var records = [[String: Any]]()
|
|
62
|
+
for record in message.records {
|
|
63
|
+
let recordType = String(data: record.type, encoding: .utf8) ?? ""
|
|
64
|
+
let payloadData = record.payload.base64EncodedString()
|
|
65
|
+
|
|
66
|
+
records.append([
|
|
67
|
+
"type": recordType,
|
|
68
|
+
"payload": payloadData
|
|
69
|
+
])
|
|
70
|
+
}
|
|
71
|
+
ndefMessages.append([
|
|
72
|
+
"records": records
|
|
73
|
+
])
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
var response: [String: Any] = ["messages": ndefMessages]
|
|
78
|
+
if let tagInfo = tagInfo {
|
|
79
|
+
response["tagInfo"] = tagInfo
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
self.notifyListeners("nfcTag", data: response)
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
reader.onError = { error in
|
|
86
|
+
if let nfcError = error as? NFCReaderError {
|
|
87
|
+
if nfcError.code != .readerSessionInvalidationErrorUserCanceled {
|
|
88
|
+
self.notifyListeners("nfcError", data: ["error": nfcError.localizedDescription])
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
reader.startScanning()
|
|
94
|
+
call.resolve()
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
@objc func cancelScan(_ call: CAPPluginCall) {
|
|
98
|
+
reader.cancelScanning()
|
|
99
|
+
call.resolve()
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
@objc func writeNDEF(_ call: CAPPluginCall) {
|
|
103
|
+
print("writeNDEF called")
|
|
104
|
+
|
|
105
|
+
guard let recordsData = call.getArray("records") as? [[String: Any]] else {
|
|
106
|
+
call.reject("Records are required")
|
|
107
|
+
return
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
var ndefRecords = [NFCNDEFPayload]()
|
|
111
|
+
for recordData in recordsData {
|
|
112
|
+
guard let type = recordData["type"] as? String,
|
|
113
|
+
let payload = recordData["payload"] as? [NSNumber]
|
|
114
|
+
else {
|
|
115
|
+
print("Skipping record due to missing or invalid record")
|
|
116
|
+
continue
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
guard let payloadArray = payload as [NSNumber]? else {
|
|
120
|
+
print("Skipping record due to missing or invalid 'payload' (expected array of numbers)")
|
|
121
|
+
continue
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
var payloadBytes = [UInt8]()
|
|
125
|
+
for number in payloadArray {
|
|
126
|
+
payloadBytes.append(number.uint8Value)
|
|
127
|
+
}
|
|
128
|
+
let payloadData = Data(payloadBytes)
|
|
129
|
+
|
|
130
|
+
let format: NFCTypeNameFormat
|
|
131
|
+
let typeEncoding: String.Encoding
|
|
132
|
+
if type == "T" || type == "U" {
|
|
133
|
+
format = .nfcWellKnown
|
|
134
|
+
typeEncoding = .utf8
|
|
135
|
+
} else if type.contains("/") {
|
|
136
|
+
format = .media
|
|
137
|
+
typeEncoding = .ascii
|
|
138
|
+
} else {
|
|
139
|
+
format = .nfcExternal
|
|
140
|
+
typeEncoding = .utf8
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
guard let typeData = type.data(using: typeEncoding) else {
|
|
144
|
+
print("Skipping record due to unsupported type encoding")
|
|
145
|
+
continue
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
let ndefRecord = NFCNDEFPayload(
|
|
149
|
+
format: format,
|
|
150
|
+
type: typeData,
|
|
151
|
+
identifier: Data(),
|
|
152
|
+
payload: payloadData
|
|
153
|
+
)
|
|
154
|
+
ndefRecords.append(ndefRecord)
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
let ndefMessage = NFCNDEFMessage(records: ndefRecords)
|
|
158
|
+
|
|
159
|
+
writer.onWriteSuccess = {
|
|
160
|
+
self.notifyListeners("nfcWriteSuccess", data: ["success": true])
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
writer.onError = { error in
|
|
164
|
+
if let nfcError = error as? NFCReaderError {
|
|
165
|
+
if nfcError.code != .readerSessionInvalidationErrorUserCanceled {
|
|
166
|
+
self.notifyListeners("nfcError", data: ["error": nfcError.localizedDescription])
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
writer.startWriting(message: ndefMessage)
|
|
172
|
+
call.resolve()
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
@objc func getStatus(_ call: CAPPluginCall) {
|
|
176
|
+
let status = isNfcAvailable() ? "ENABLED" : "NOT_SUPPORTED"
|
|
177
|
+
call.resolve([
|
|
178
|
+
"status": status
|
|
179
|
+
])
|
|
180
|
+
}
|
|
181
|
+
}
|