@exxili/capacitor-nfc 0.0.9 → 0.0.11

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.
@@ -66,7 +66,7 @@ class NFCPlugin : Plugin() {
66
66
  writeMode = false
67
67
  recordsBuffer = null
68
68
  }
69
- else if (ACTION_NDEF_DISCOVERED == intent.action) {
69
+ else if (ACTION_NDEF_DISCOVERED == intent.action || ACTION_TAG_DISCOVERED == intent.action) {
70
70
  Log.d("NFC", "READ MODE START")
71
71
  handleReadTag(intent)
72
72
  }
@@ -288,20 +288,41 @@ class NFCPlugin : Plugin() {
288
288
  val jsResponse = JSObject()
289
289
 
290
290
  val ndefMessages = JSArray()
291
- val receivedMessages = intent.getParcelableArrayExtra(
292
- EXTRA_NDEF_MESSAGES,
293
- NdefMessage::class.java
294
- )
295
291
 
296
- receivedMessages?.also { rawMessages ->
297
- for (message in rawMessages) {
298
- val ndefRecords = JSArray()
299
- for (record in message.records) {
300
- val rec = JSObject()
301
- rec.put("type", String(record.type, Charsets.UTF_8))
302
- rec.put("payload", Base64.getEncoder().encodeToString(record.payload))
303
- ndefRecords.put(rec)
292
+ when (intent.action) {
293
+ NfcAdapter.ACTION_NDEF_DISCOVERED -> {
294
+ val receivedMessages = intent.getParcelableArrayExtra(
295
+ EXTRA_NDEF_MESSAGES,
296
+ NdefMessage::class.java
297
+ )
298
+
299
+ receivedMessages?.also { rawMessages ->
300
+ for (message in rawMessages) {
301
+ val ndefRecords = JSArray()
302
+ for (record in message.records) {
303
+ val rec = JSObject()
304
+ rec.put("type", String(record.type, Charsets.UTF_8))
305
+ rec.put("payload", Base64.getEncoder().encodeToString(record.payload))
306
+ ndefRecords.put(rec)
307
+ }
308
+
309
+ val msg = JSObject()
310
+ msg.put("records", ndefRecords)
311
+ ndefMessages.put(msg)
312
+ }
304
313
  }
314
+ }
315
+
316
+ NfcAdapter.ACTION_TAG_DISCOVERED -> {
317
+ val tagId = intent.getByteArrayExtra(NfcAdapter.EXTRA_ID)
318
+ val result = if (tagId != null) byteArrayToHexString(tagId) else ""
319
+
320
+ val rec = JSObject()
321
+ rec.put("type", "ID")
322
+ rec.put("payload", Base64.getEncoder().encodeToString(result.toByteArray()))
323
+
324
+ val ndefRecords = JSArray()
325
+ ndefRecords.put(rec)
305
326
 
306
327
  val msg = JSObject()
307
328
  msg.put("records", ndefRecords)
@@ -312,4 +333,18 @@ class NFCPlugin : Plugin() {
312
333
  jsResponse.put("messages", ndefMessages)
313
334
  this.notifyListeners("nfcTag", jsResponse)
314
335
  }
336
+
337
+ private fun byteArrayToHexString(inarray: ByteArray): String {
338
+ val hex = arrayOf("0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F")
339
+ var out = ""
340
+
341
+ for (j in inarray.size - 1 downTo 0) {
342
+ val `in` = inarray[j].toInt() and 0xff
343
+ val i1 = (`in` shr 4) and 0x0f
344
+ out += hex[i1]
345
+ val i2 = `in` and 0x0f
346
+ out += hex[i2]
347
+ }
348
+ return out
349
+ }
315
350
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@exxili/capacitor-nfc",
3
- "version": "0.0.9",
3
+ "version": "0.0.11",
4
4
  "description": "A Capacitor plugin for reading and writing NFC NDEFtags.",
5
5
  "main": "dist/plugin.cjs.js",
6
6
  "module": "dist/esm/index.js",
@@ -62,7 +62,7 @@
62
62
  "typescript": "~4.1.5"
63
63
  },
64
64
  "peerDependencies": {
65
- "@capacitor/core": "^6.0.0"
65
+ "@capacitor/core": ">=6.0.0 <9.0.0"
66
66
  },
67
67
  "prettier": "@ionic/prettier-config",
68
68
  "swiftlint": "@ionic/swiftlint-config",