Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
EddyVerbruggen committed Nov 26, 2020
1 parent 6bb7c3d commit ed708b5
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 55 deletions.
1 change: 1 addition & 0 deletions demo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
38 changes: 10 additions & 28 deletions src/nfc.android.ts
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -508,7 +494,7 @@ export class Nfc implements NfcApi {
}
}

private writeNdefMessage(
private static writeNdefMessage(
message: android.nfc.NdefMessage,
tag: android.nfc.Tag
): string {
Expand Down Expand Up @@ -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;
Expand All @@ -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);
}
}

Expand All @@ -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));

Expand All @@ -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);
Expand Down
50 changes: 25 additions & 25 deletions src/nfc.ios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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<NfcNdefRecord> {
private static messageToJSON(message: NFCNDEFMessage): Array<NfcNdefRecord> {
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(
Expand All @@ -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;
}
Expand All @@ -277,51 +277,51 @@ 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),
x => +x.toString(16)
);
}

private hex2a(hexx) {
private static hex2a(hexx) {
const hex = hexx.toString(); // force conversion
let str = "";
for (let i = 0; i < hex.length; i += 2)
str += String.fromCharCode(parseInt(hex.substr(i, 2), 16));
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<string> {
private static nsdataToHexArray(data): Array<string> {
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,
Expand Down
4 changes: 2 additions & 2 deletions src/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down

0 comments on commit ed708b5

Please sign in to comment.