nativescript-nfc-fix-uid
Version:
NFC plugin for your NativeScript app
510 lines (509 loc) • 20.8 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Nfc = void 0;
var nfc_common_1 = require("./nfc.common");
var Nfc = /** @class */ (function () {
function Nfc() {
this.writeMode = false;
this.shouldUseTagReaderSession = false;
}
Nfc._available = function () {
var isIOS11OrUp = NSObject.instancesRespondToSelector("accessibilityAttributedLabel");
if (isIOS11OrUp) {
try {
return NFCNDEFReaderSession.readingAvailable;
}
catch (e) {
return false;
}
}
else {
return false;
}
};
Nfc.prototype.available = function () {
return new Promise(function (resolve, reject) {
resolve(Nfc._available());
});
};
Nfc.prototype.enabled = function () {
return new Promise(function (resolve, reject) {
resolve(Nfc._available());
});
};
Nfc.prototype.setOnTagDiscoveredListener = function (callback) {
var _this = this;
console.log("nativescript-nfc ios: setOnTagDiscoveredListener");
return new Promise(function (resolve, reject) {
if (!Nfc._available()) {
reject();
return;
}
if (callback === null) {
console.log("callback = null");
_this.invalidateSession();
resolve("res");
return;
}
_this.writeMode = false;
_this.shouldUseTagReaderSession = true;
try {
_this.startScanSession(callback, {});
resolve("res");
}
catch (e) {
reject(e);
}
});
};
Nfc.prototype.setOnNdefDiscoveredListener = function (callback, options) {
var _this = this;
return new Promise(function (resolve, reject) {
if (!Nfc._available()) {
reject();
return;
}
if (callback === null) {
_this.invalidateSession();
resolve("res");
return;
}
_this.writeMode = false;
_this.shouldUseTagReaderSession = false;
try {
_this.startScanSession(callback, options);
resolve("res");
}
catch (e) {
reject(e);
}
});
};
Nfc.prototype.invalidateSession = function () {
if (this.session) {
this.session.invalidateSession();
this.session = undefined;
}
};
Nfc.prototype.stopListening = function () {
return new Promise(function (resolve, reject) {
resolve("res");
});
};
Nfc.prototype.writeTag = function (arg) {
var _this = this;
console.log("writeTag");
return new Promise(function (resolve, reject) {
try {
_this.writeMode = true;
_this.shouldUseTagReaderSession = false;
_this.messageToWrite = NfcHelper.jsonToNdefRecords(arg);
_this.startScanSession(function () { }, {
stopAfterFirstRead: false,
scanHint: "Hold near writable NFC tag to update."
});
resolve("res");
}
catch (e) {
reject(e);
}
});
};
Nfc.prototype.eraseTag = function () {
var _this = this;
return new Promise(function (resolve, reject) {
try {
_this.writeMode = true;
_this.shouldUseTagReaderSession = false;
_this.messageToWrite = NfcHelper.ndefEmptyMessage();
_this.startScanSession(function (data) { }, {
stopAfterFirstRead: false,
scanHint: "Hold near writable NFC tag to erase."
});
resolve("res");
}
catch (e) {
reject(e);
}
});
};
/* Common Processing */
Nfc.prototype.startScanSession = function (callback, options) {
alert('sh u ss' + this.shouldUseTagReaderSession);
console.log("NFCTagReaderSessionDelegateImpl" + NFCTagReaderSessionDelegateImpl);
if (this.shouldUseTagReaderSession) {
try {
this.tagDelegate = NFCTagReaderSessionDelegateImpl.createWithOwnerResultCallbackAndOptions(new WeakRef(this), function (data) {
alert('enter 3');
if (!callback) {
alert("Tag discovered, but no listener was set via setOnTagDiscoveredListener. Ndef: " + JSON.stringify(data));
}
else {
console.log('error starting scan sessioncallback found');
// execute on the main thread with this trick, so UI updates are not broken
Promise.resolve().then(function () { return callback(data); });
}
}, {});
}
catch (err) {
console.log('error starting scan session');
console.log(JSON.stringify(err));
}
this.session = NFCTagReaderSession.alloc().initWithPollingOptionDelegateQueue(1 /* NFCPollingOption.ISO14443 */ | 2 /* NFCPollingOption.ISO15693 */, this.tagDelegate, null);
}
else {
try {
this.delegate = NFCNDEFReaderSessionDelegateImpl.createWithOwnerResultCallbackAndOptions(new WeakRef(this), function (data) {
alert("callb");
if (!callback) {
alert("Ndef discovered, but no listener was set via setOnNdefDiscoveredListener. Ndef: " + JSON.stringify(data));
}
else {
// execute on the main thread with this trick, so UI updates are not broken
Promise.resolve().then(function () { return callback(data); });
}
}, options);
}
catch (err) {
alert(JSON.stringify(err));
}
this.session = NFCNDEFReaderSession.alloc().initWithDelegateQueueInvalidateAfterFirstRead(this.delegate, null, options && options.stopAfterFirstRead);
if (options && options.scanHint) {
this.session.alertMessage = options.scanHint;
}
}
this.session.beginSession();
};
return Nfc;
}());
exports.Nfc = Nfc;
var NfcHelper = /** @class */ (function () {
function NfcHelper() {
}
NfcHelper.getTagUIDAsInt8Array = function (tag) {
return this.nsDataToInt8Array(this.getTagUID(tag));
};
NfcHelper.getTagUID = function (tag) {
var nfcNHelper = NfcNativeHelper.new();
var uid = nfcNHelper.getTagUID(tag);
return uid;
};
NfcHelper.ndefEmptyMessage = function () {
var type = this.uint8ArrayToNSData([]);
var id = this.uint8ArrayToNSData([]);
var payload = this.uint8ArrayToNSData([]);
var record = NFCNDEFPayload.alloc().initWithFormatTypeIdentifierPayload(0 /* NFCTypeNameFormat.Empty */, type, id, payload);
var records = NSMutableArray.new();
records.addObject(record);
return NFCNDEFMessage.alloc().initWithNDEFRecords(records);
};
NfcHelper.jsonToNdefRecords = function (arg) {
var _this = this;
var records = NSMutableArray.new();
if (arg.textRecords !== null) {
arg.textRecords.forEach(function (textRecord) {
var type = _this.uint8ArrayToNSData([0x54]);
var ids = [];
if (textRecord.id) {
for (var j = 0; j < textRecord.id.length; j++) {
ids.push(textRecord.id[j]);
}
}
var id = _this.uint8ArrayToNSData(ids);
var langCode = textRecord.languageCode || "en";
var encoded = _this.stringToBytes(langCode + textRecord.text);
encoded.unshift(langCode.length);
var payloads = [];
for (var n = 0; n < encoded.length; n++) {
payloads[n] = encoded[n];
}
var payload = _this.uint8ArrayToNSData(payloads);
var record = NFCNDEFPayload.alloc().initWithFormatTypeIdentifierPayload(1 /* NFCTypeNameFormat.NFCWellKnown */, type, id, payload);
records.addObject(record);
});
}
// TODO: implement for URI records
return NFCNDEFMessage.alloc().initWithNDEFRecords(records);
};
NfcHelper.ndefToJson = function (message) {
if (message === null) {
return null;
}
return {
message: this.messageToJSON(message)
};
};
NfcHelper.messageToJSON = function (message) {
var result = [];
for (var i = 0; i < message.records.count; i++) {
result.push(this.recordToJSON(message.records.objectAtIndex(i)));
}
return result;
};
NfcHelper.recordToJSON = function (record) {
var payloadAsHexArray = this.nsdataToHexArray(record.payload);
var payloadAsString = this.nsdataToASCIIString(record.payload);
var payloadAsStringWithPrefix = payloadAsString;
var recordType = this.nsdataToHexArray(record.type);
var decimalType = this.hexToDec(recordType[0]);
if (decimalType === 84) {
var languageCodeLength = +payloadAsHexArray[0];
payloadAsString = payloadAsStringWithPrefix.substring(languageCodeLength + 1);
}
else if (decimalType === 85) {
var prefix = nfc_common_1.NfcUriProtocols[payloadAsHexArray[0]];
if (!prefix) {
prefix = "";
}
payloadAsString = prefix + payloadAsString.slice(1);
}
return {
tnf: record.typeNameFormat,
type: decimalType,
id: this.hexToDecArray(this.nsdataToHexArray(record.identifier)),
payload: this.hexToDecArray(payloadAsHexArray),
payloadAsHexString: this.nsdataToHexString(record.payload),
payloadAsStringWithPrefix: payloadAsStringWithPrefix,
payloadAsString: payloadAsString
};
};
NfcHelper.stringToBytes = function (input) {
var bytes = [];
for (var n = 0; n < input.length; n++) {
var c = input.charCodeAt(n);
if (c < 128) {
bytes[bytes.length] = c;
}
else if ((c > 127) && (c < 2048)) {
bytes[bytes.length] = (c >> 6) | 192;
bytes[bytes.length] = (c & 63) | 128;
}
else {
bytes[bytes.length] = (c >> 12) | 224;
bytes[bytes.length] = ((c >> 6) & 63) | 128;
bytes[bytes.length] = (c & 63) | 128;
}
}
return bytes;
};
NfcHelper.uint8ArrayToNSData = function (array) {
var data = NSMutableData.alloc().initWithCapacity(array.count);
for (var _i = 0, array_1 = array; _i < array_1.length; _i++) {
var item = array_1[_i];
data.appendBytesLength(new interop.Reference(interop.types.uint8, item), 1);
}
return data;
};
NfcHelper.nsDataToInt8Array = function (data) {
var buffer = interop.bufferFromData(data);
return new Int8Array(buffer);
};
NfcHelper.nsdataToHexString = function (data) {
var b = interop.bufferFromData(data);
return this.buf2hexString(b);
};
NfcHelper.nsdataToHexArray = function (data) {
var b = interop.bufferFromData(data);
return this.buf2hexArray(b);
};
NfcHelper.nsdataToASCIIString = function (data) {
return this.hex2a(this.nsdataToHexString(data));
};
NfcHelper.hex2a = function (hexx) {
var hex = hexx.toString(); // force conversion
var str = '';
for (var i = 0; i < hex.length; i += 2)
str += String.fromCharCode(parseInt(hex.substr(i, 2), 16));
return str;
};
NfcHelper.hexToDecArray = function (hexArray) {
var resultArray = [];
for (var i = 0; i < hexArray.length; i++) {
var result = 0, digitValue = void 0;
var hex = hexArray[i].toLowerCase();
for (var j = 0; j < hex.length; j++) {
digitValue = '0123456789abcdefgh'.indexOf(hex[j]);
result = result * 16 + digitValue;
}
resultArray.push(result);
}
return JSON.stringify(resultArray);
};
NfcHelper.buf2hexArray = function (buffer) {
return Array.prototype.map.call(new Uint8Array(buffer), function (x) { return ('00' + x.toString(16)).slice(-2); });
};
NfcHelper.buf2hexString = function (buffer) {
return Array.prototype.map.call(new Uint8Array(buffer), function (x) { return ('00' + x.toString(16)).slice(-2); }).join('');
};
NfcHelper.hexToDec = function (hex) {
if (hex === undefined) {
return undefined;
}
var result = 0, digitValue;
hex = hex.toLowerCase();
for (var i = 0; i < hex.length; i++) {
digitValue = '0123456789abcdefgh'.indexOf(hex[i]);
result = result * 16 + digitValue;
}
return result;
};
return NfcHelper;
}());
/* NFCTagReaderSessionDelegate */
var NFCTagReaderSessionDelegateImpl = /** @class */ (function (_super) {
__extends(NFCTagReaderSessionDelegateImpl, _super);
function NFCTagReaderSessionDelegateImpl() {
return _super !== null && _super.apply(this, arguments) || this;
}
NFCTagReaderSessionDelegateImpl.new = function () {
try {
NFCTagReaderSessionDelegateImpl.ObjCProtocols.push(NFCTagReaderSessionDelegate);
}
catch (ignore) {
}
return _super.new.call(this);
};
NFCTagReaderSessionDelegateImpl.createWithOwnerResultCallbackAndOptions = function (owner, callback, options) {
console.log("createWithOwnerResultCallbackAndOptions");
var delegate = NFCTagReaderSessionDelegateImpl.new();
delegate._owner = owner;
delegate.options = options;
delegate.resultCallback = callback;
return delegate;
};
NFCTagReaderSessionDelegateImpl.prototype.tagReaderSessionDidDetectTags = function (session, tags) {
alert('detected');
console.log("tagReaderSessionDidDetectTags");
var ids = NfcHelper.getTagUIDAsInt8Array(tags[0]);
var nfcTagData = {
id: ids,
techList: []
};
session.invalidateSession();
this.resultCallback(nfcTagData);
};
NFCTagReaderSessionDelegateImpl.prototype.tagReaderSessionDidInvalidateWithError = function (session, error) {
this._owner.get().invalidateSession();
};
NFCTagReaderSessionDelegateImpl.ObjCProtocols = [];
return NFCTagReaderSessionDelegateImpl;
}(NSObject));
/* NFCNDEFReaderSessionDelegate */
var NFCNDEFReaderSessionDelegateImpl = /** @class */ (function (_super) {
__extends(NFCNDEFReaderSessionDelegateImpl, _super);
function NFCNDEFReaderSessionDelegateImpl() {
return _super !== null && _super.apply(this, arguments) || this;
}
NFCNDEFReaderSessionDelegateImpl.new = function () {
try {
NFCNDEFReaderSessionDelegateImpl.ObjCProtocols.push(NFCNDEFReaderSessionDelegate);
}
catch (ignore) {
}
return _super.new.call(this);
};
NFCNDEFReaderSessionDelegateImpl.createWithOwnerResultCallbackAndOptions = function (owner, callback, options) {
var delegate = NFCNDEFReaderSessionDelegateImpl.new();
delegate._owner = owner;
delegate.options = options;
delegate.resultCallback = callback;
return delegate;
};
NFCNDEFReaderSessionDelegateImpl.prototype.readerSessionDidBecomeActive = function (session) {
console.log("NFCNDEFReaderSessionDelegateImpl:readerSessionDidBecomeActive");
};
NFCNDEFReaderSessionDelegateImpl.prototype.readerSessionDidDetectTags = function (session, tags) {
var _this = this;
alert("NFCNDEFReaderSessionDelegateImpl:readerSessionDidDetectTags");
var tag = tags[0];
session.connectToTagCompletionHandler(tag, function (error) {
console.log("connectToTagCompletionHandler");
if (error) {
console.log(error);
session.invalidateSessionWithErrorMessage("Error connecting to tag.");
return;
}
_this.processNDEFTag(session, tag, _this._owner.get().messageToWrite);
});
};
// Called when the reader session finds a new tag
NFCNDEFReaderSessionDelegateImpl.prototype.readerSessionDidDetectNDEFs = function (session, messages) {
var _this = this;
alert("NFCNDEFReaderSessionDelegateImpl:readerSessionDidDetectNDEFs");
if (this.options && this.options.stopAfterFirstRead) {
setTimeout(function () { return _this._owner.get().invalidateSession(); });
}
if (!this._owner.get().writeMode) {
var firstMessage = messages[0];
// execute on the main thread with this trick
}
};
// Called when the reader session becomes invalid due to the specified error
NFCNDEFReaderSessionDelegateImpl.prototype.readerSessionDidInvalidateWithError = function (session /* NFCNDEFReaderSession */, error) {
this._owner.get().invalidateSession();
};
/* Helpers */
NFCNDEFReaderSessionDelegateImpl.prototype.processNDEFTag = function (session, tag, messageToWrite) {
var _this = this;
alert(NFCNDEFTag.prototype.queryNDEFStatusWithCompletionHandler);
NFCNDEFTag.prototype.queryNDEFStatusWithCompletionHandler.call(tag, function (status, number, error) {
console.log("queryNDEFStatusWithCompletionHandler");
if (error) {
console.log(error);
session.invalidateSessionWithErrorMessage("Error getting tag status.");
return;
}
if (messageToWrite) {
_this.writeNDEFTag(session, status, tag, messageToWrite);
}
else {
_this.readNDEFTag(session, status, tag);
}
});
};
NFCNDEFReaderSessionDelegateImpl.prototype.readNDEFTag = function (session, status, tag) {
var _this = this;
console.log("readNDEFTag");
NFCNDEFTag.prototype.readNDEFWithCompletionHandler.call(tag, function (message, error) {
console.log("readNDEFWithCompletionHandler");
if (_this.options && _this.options.stopAfterFirstRead) {
setTimeout(function () { return _this._owner.get().invalidateSession(); });
}
if (error && error.code != 403) {
session.invalidateSessionWithErrorMessage("Read failed.");
return;
}
else {
session.alertMessage = "Tag successfully read.";
session.invalidateSession();
// execute on the main thread with this trick
_this.resultCallback(NfcHelper.ndefToJson(message));
}
});
};
NFCNDEFReaderSessionDelegateImpl.prototype.writeNDEFTag = function (session, status, tag, message) {
console.log("writeNDEFTag");
console.log("Status: " + status);
if (status == 1 /* NFCNDEFStatus.NotSupported */) {
session.invalidateSessionWithErrorMessage("Tag is not NDEF compliant.");
}
else if (status === 3 /* NFCNDEFStatus.ReadOnly */) {
session.invalidateSessionWithErrorMessage("Tag is read only.");
}
else if (status === 2 /* NFCNDEFStatus.ReadWrite */) {
console.log(message);
NFCNDEFTag.prototype.writeNDEFCompletionHandler.call(tag, message, function (error) {
if (error) {
console.log(error);
session.invalidateSessionWithErrorMessage("Write failed.");
}
else {
session.alertMessage = "Wrote data to NFC tag.";
session.invalidateSession();
}
});
}
};
NFCNDEFReaderSessionDelegateImpl.ObjCProtocols = [];
return NFCNDEFReaderSessionDelegateImpl;
}(NSObject));