From ed708b5697bf752fa013c4aab2a6c3c66e188cd2 Mon Sep 17 00:00:00 2001 From: EddyVerbruggen Date: Thu, 26 Nov 2020 22:10:25 +0100 Subject: [PATCH] cleanup --- demo/package.json | 1 + src/nfc.android.ts | 38 ++++++++++------------------------- src/nfc.ios.ts | 50 +++++++++++++++++++++++----------------------- src/package.json | 4 ++-- 4 files changed, 38 insertions(+), 55 deletions(-) diff --git a/demo/package.json b/demo/package.json index 0ffbc06..45a71e7 100644 --- a/demo/package.json +++ b/demo/package.json @@ -7,6 +7,7 @@ }, "devDependencies": { "@nativescript/android": "7.0.0", + "@nativescript/ios": "7.0.6", "@nativescript/types": "~7.0.4", "@nativescript/webpack": "~3.0.7", "babel-traverse": "6.26.0", diff --git a/src/nfc.android.ts b/src/nfc.android.ts index 4c74f19..5f4e3d3 100644 --- a/src/nfc.android.ts +++ b/src/nfc.android.ts @@ -1,19 +1,5 @@ -import { - AndroidActivityEventData, - AndroidActivityNewIntentEventData, - AndroidApplication, - Application, - Utils -} from "@nativescript/core"; -import { - NdefListenerOptions, - NfcApi, - NfcNdefData, - NfcNdefRecord, - NfcTagData, - NfcUriProtocols, - WriteTagOptions -} from "./nfc.common"; +import { AndroidActivityEventData, AndroidActivityNewIntentEventData, AndroidApplication, Application, Utils } from "@nativescript/core"; +import { NdefListenerOptions, NfcApi, NfcNdefData, NfcNdefRecord, NfcTagData, NfcUriProtocols, WriteTagOptions } from "./nfc.common"; declare let Array: any; @@ -417,7 +403,7 @@ export class Nfc implements NfcApi { let ndefClass = android.nfc.NdefMessage as any; let ndefMessage = new ndefClass(records); - let errorMessage = this.writeNdefMessage(ndefMessage, tag); + let errorMessage = Nfc.writeNdefMessage(ndefMessage, tag); if (errorMessage === null) { resolve(); } else { @@ -456,7 +442,7 @@ export class Nfc implements NfcApi { let ndefClass = android.nfc.NdefMessage as any; let ndefMessage = new ndefClass(records); - let errorMessage = this.writeNdefMessage(ndefMessage, tag); + let errorMessage = Nfc.writeNdefMessage(ndefMessage, tag); if (errorMessage === null) { resolve(); } else { @@ -508,7 +494,7 @@ export class Nfc implements NfcApi { } } - private writeNdefMessage( + private static writeNdefMessage( message: android.nfc.NdefMessage, tag: android.nfc.Tag ): string { @@ -569,7 +555,7 @@ export class Nfc implements NfcApi { let textRecord = input.textRecords[i]; let langCode = textRecord.languageCode || "en"; - let encoded = this.stringToBytes(langCode + textRecord.text); + let encoded = Nfc.stringToBytes(langCode + textRecord.text); encoded.unshift(langCode.length); let tnf = android.nfc.NdefRecord.TNF_WELL_KNOWN; // 0x01; @@ -589,9 +575,7 @@ export class Nfc implements NfcApi { payload[n] = encoded[n]; } - let record = new android.nfc.NdefRecord(tnf, type, id, payload); - - records[recordCounter++] = record; + records[recordCounter++] = new android.nfc.NdefRecord(tnf, type, id, payload); } } @@ -612,7 +596,7 @@ export class Nfc implements NfcApi { prefix = ""; } - let encoded = this.stringToBytes(uri.slice(prefix.length)); + let encoded = Nfc.stringToBytes(uri.slice(prefix.length)); // prepend protocol code encoded.unshift(NfcUriProtocols.indexOf(prefix)); @@ -633,15 +617,13 @@ export class Nfc implements NfcApi { payload[n] = encoded[n]; } - let record = new android.nfc.NdefRecord(tnf, type, id, payload); - - records[recordCounter++] = record; + records[recordCounter++] = new android.nfc.NdefRecord(tnf, type, id, payload); } } return records; } - private stringToBytes(input: string) { + private static stringToBytes(input: string) { let bytes = []; for (let n = 0; n < input.length; n++) { let c = input.charCodeAt(n); diff --git a/src/nfc.ios.ts b/src/nfc.ios.ts index 2d561ad..d9342e4 100644 --- a/src/nfc.ios.ts +++ b/src/nfc.ios.ts @@ -177,7 +177,7 @@ class NFCNDEFReaderSessionDelegateImpl } // execute on the main thread with this trick - this.resultCallback(this.ndefToJson(firstMessage)); + this.resultCallback(NFCNDEFReaderSessionDelegateImpl.ndefToJson(firstMessage)); } readerSessionDidDetectTags( @@ -214,30 +214,30 @@ class NFCNDEFReaderSessionDelegateImpl this._owner.get().invalidateSession(); } - private ndefToJson(message: NFCNDEFMessage): NfcNdefData { + private static ndefToJson(message: NFCNDEFMessage): NfcNdefData { if (message === null) { return null; } return { - message: this.messageToJSON(message) + message: NFCNDEFReaderSessionDelegateImpl.messageToJSON(message) }; } - private messageToJSON(message: NFCNDEFMessage): Array { + private static messageToJSON(message: NFCNDEFMessage): Array { const result = []; for (let i = 0; i < message.records.count; i++) { - result.push(this.recordToJSON(message.records.objectAtIndex(i))); + result.push(NFCNDEFReaderSessionDelegateImpl.recordToJSON(message.records.objectAtIndex(i))); } return result; } - private recordToJSON(record: NFCNDEFPayload): NfcNdefRecord { - let payloadAsHexArray = this.nsdataToHexArray(record.payload); - let payloadAsString = this.nsdataToASCIIString(record.payload); + private static recordToJSON(record: NFCNDEFPayload): NfcNdefRecord { + let payloadAsHexArray = NFCNDEFReaderSessionDelegateImpl.nsdataToHexArray(record.payload); + let payloadAsString = NFCNDEFReaderSessionDelegateImpl.nsdataToASCIIString(record.payload); let payloadAsStringWithPrefix = payloadAsString; - const recordType = this.nsdataToHexArray(record.type); - const decimalType = this.hexToDec(recordType[0]); + const recordType = NFCNDEFReaderSessionDelegateImpl.nsdataToHexArray(record.type); + const decimalType = NFCNDEFReaderSessionDelegateImpl.hexToDec(recordType[0]); if (decimalType === 84) { let languageCodeLength: number = +payloadAsHexArray[0]; payloadAsString = payloadAsStringWithPrefix.substring( @@ -254,15 +254,15 @@ class NFCNDEFReaderSessionDelegateImpl return { tnf: record.typeNameFormat, // "typeNameFormat" (1 = well known) - see https://developer.apple.com/documentation/corenfc/nfctypenameformat?changes=latest_major&language=objc type: decimalType, - id: this.hexToDecArray(this.nsdataToHexArray(record.identifier)), - payload: this.hexToDecArray(payloadAsHexArray), - payloadAsHexString: this.nsdataToHexString(record.payload), + id: NFCNDEFReaderSessionDelegateImpl.hexToDecArray(NFCNDEFReaderSessionDelegateImpl.nsdataToHexArray(record.identifier)), + payload: NFCNDEFReaderSessionDelegateImpl.hexToDecArray(payloadAsHexArray), + payloadAsHexString: NFCNDEFReaderSessionDelegateImpl.nsdataToHexString(record.payload), payloadAsStringWithPrefix: payloadAsStringWithPrefix, payloadAsString: payloadAsString }; } - private hexToDec(hex) { + private static hexToDec(hex) { if (hex === undefined) { return undefined; } @@ -277,21 +277,21 @@ class NFCNDEFReaderSessionDelegateImpl return result; } - private buf2hexString(buffer) { + private static buf2hexString(buffer) { // buffer is an ArrayBuffer return Array.prototype.map .call(new Uint8Array(buffer), x => ("00" + x.toString(16)).slice(-2)) .join(""); } - private buf2hexArray(buffer) { + private static buf2hexArray(buffer) { // buffer is an ArrayBuffer return Array.prototype.map.call(new Uint8Array(buffer), x => ("00" + x.toString(16)).slice(-2) ); } - private buf2hexArrayNr(buffer) { + private static buf2hexArrayNr(buffer) { // buffer is an ArrayBuffer return Array.prototype.map.call( new Uint8Array(buffer), @@ -299,7 +299,7 @@ class NFCNDEFReaderSessionDelegateImpl ); } - private hex2a(hexx) { + private static hex2a(hexx) { const hex = hexx.toString(); // force conversion let str = ""; for (let i = 0; i < hex.length; i += 2) @@ -307,21 +307,21 @@ class NFCNDEFReaderSessionDelegateImpl return str; } - private nsdataToHexString(data): string { + private static nsdataToHexString(data): string { let b = interop.bufferFromData(data); - return this.buf2hexString(b); + return NFCNDEFReaderSessionDelegateImpl.buf2hexString(b); } - private nsdataToHexArray(data): Array { + private static nsdataToHexArray(data): Array { let b = interop.bufferFromData(data); - return this.buf2hexArray(b); + return NFCNDEFReaderSessionDelegateImpl.buf2hexArray(b); } - private nsdataToASCIIString(data): string { - return this.hex2a(this.nsdataToHexString(data)); + private static nsdataToASCIIString(data): string { + return NFCNDEFReaderSessionDelegateImpl.hex2a(NFCNDEFReaderSessionDelegateImpl.nsdataToHexString(data)); } - private hexToDecArray(hexArray): any { + private static hexToDecArray(hexArray): any { let resultArray = []; for (let i = 0; i < hexArray.length; i++) { let result = 0, diff --git a/src/package.json b/src/package.json index 10c3802..520dbde 100644 --- a/src/package.json +++ b/src/package.json @@ -6,8 +6,8 @@ "typings": "index.d.ts", "nativescript": { "platforms": { - "android": "5.4.0", - "ios": "4.0.0" + "android": "7.0.0", + "ios": "7.0.0" } }, "scripts": {