无法获得NFC在NativeScript 7或8上编写工作



我分叉了插件NativeScript - NFC,并为iOS实现了NFC写入功能,它在使用NativeScript 6时运行良好。但是如果我将插件升级到7或8,则写作不工作,NFCNDEFTag.prototype.queryNDEFStatusWithCompletionHandler总是未定义的。

写入NFC的实现如下所示:

readerSessionDidDetectTags(
session: NFCNDEFReaderSession,
tags: NSArray<NFCNDEFTag> | NFCNDEFTag[]
): void {
const tag = tags[0];
session.connectToTagCompletionHandler(tag, (error: NSError) => {
console.log("connectToTagCompletionHandler");
if (error) {
console.log(error);
session.invalidateSessionWithErrorMessage("Error connecting to tag.");
this.errorCallback(error);
return;
}
const ndefTag: NFCNDEFTag = new interop.Reference<NFCNDEFTag>(
interop.types.id,
tag
).value;
try {
NFCNDEFTag.prototype.queryNDEFStatusWithCompletionHandler.call(
ndefTag,
(status: NFCNDEFStatus, number: number, error: NSError) => {
console.log("queryNDEFStatusWithCompletionHandler");
if (status == NFCNDEFStatus.NotSupported) {
var errMessage = "Tag is not NDEF compliant.";
session.invalidateSessionWithErrorMessage(errMessage);
} else if (status === NFCNDEFStatus.ReadOnly) {
var errMessage = "Tag is read only.";
session.invalidateSessionWithErrorMessage(errMessage);
} else if (status === NFCNDEFStatus.ReadWrite) {
const ndefMessage = this._owner.get().message;
NFCNDEFTag.prototype.writeNDEFCompletionHandler.call(
ndefTag,
ndefMessage,
(error: NSError) => {
if (error) {
console.log(error);
session.invalidateSessionWithErrorMessage("Write failed.");
this.errorCallback(error);
} else {
if (
ndefMessage.records[0].typeNameFormat ==
NFCTypeNameFormat.Empty
) {
session.alertMessage = "Erased data from NFC tag.";
} else {
if (this.options.writeHint) {
session.alertMessage = this.options.writeHint;
}
this.resultCallback(NfcHelper.ndefToJson(ndefMessage));
}
session.invalidateSession();
}
}
);
}
}
);
} catch (e) {
session.alertMessage = "error";
}
});
}

我不确定我是否以正确的方式为NativeScript 7或8实现它,但至少它可以在6上工作,我实际上不相信这是NativeScript在7和8中的错误。我们曾经用了很长时间的6只是为了使用这个插件,但真的需要升级到7或8为了其他功能。

任何帮助,建议或评论将非常感谢!下面是不同NativeScript版本的repo和分支。

回购:https://github.com/nordsense/nativescript-nfc

分支feature/upgrade-nativescript:这是NativeScript 8,只读作品

分支nordsense:这是NativeScript 6和读取和写入工作

您可以使用演示项目进行测试,set ndef listener用于读取,write Text用于写入

我设法让它在ns8上工作。参考下面的代码片段,如果在第2行之前访问NFCNDEFTag.prototype,就像第1行的代码一样,或者通过使用console.dir(NFCNDEFTag.prototype),第3行将转储属于正确协议NFCNDEFTag的正确数据,其余代码也可以正常工作;如果注释掉第1行,第3行将转储属于其他协议/对象(如NSKeyValueCodingUIAccessibilityAction)的错误数据。我仍然不知道为什么会发生这种情况,感觉像一个NativeScript问题,我在这里提出了一个问题https://github.com/NativeScript/NativeScript/issues/9609

我重新构建了插件并在这里发布了包https://github.com/cloudhx/nativescript-plugins

readerSessionDidDetectTags(session: NFCNDEFReaderSession, tags: NSArray<NFCNDEFTag> | NFCNDEFTag[]): void {
const prototype = NFCNDEFTag.prototype; // Line 1
const tag = (<NSArray<NFCNDEFTag>>tags).firstObject; // Line 2
console.dir(NFCNDEFTag.prototype); // Line 3
session.connectToTagCompletionHandler(tag, (error: NSError) => {
console.log('connectToTagCompletionHandler');
if (error) {
console.log(error);
session.invalidateSessionWithErrorMessage('Unable to connect to tag.');
this.errorCallback(error);
return;
}
const ndefTag: NFCNDEFTag = new interop.Reference<NFCNDEFTag>(interop.types.id, tag).value;
try {
NFCNDEFTag.prototype.queryNDEFStatusWithCompletionHandler.call(ndefTag, (status: NFCNDEFStatus, number: number, error: NSError) => {
console.log('queryNDEFStatusWithCompletionHandler');
// Code omitted for brevity

最新更新