【问题标题】:Cordova nfc plugin write text to nfc cardCordova nfc 插件将文本写入 nfc 卡
【发布时间】:2013-11-27 10:03:48
【问题描述】:

我正在使用 cordova 进行测试,以将简单的文本写入卡片。在我有一个引导按钮上

onclick="write_btn();"

我在 index.js 中添加了

    function write_btn(){
    alert("Write some to card");
    var message = [
    ndef.textRecord("hello, world of NFC"),
    ];
    var sMsg;
    nfc.write(message, 
    function(){sMsg="good";alert("Write Succes");}, 
    function(){sMsg="fals";alert("Nothing got written");}
            );
    console.log("Writing is: "+sMsg);
    alert("Writing is: "+sMsg);
}

但没有写任何东西,只有警报。如果我在函数 write_btn 中只有 alert("...") 它会被触发。 ndef 是一个全局变量,对吧?

问候

【问题讨论】:

  • 你弄明白了吗?

标签: twitter-bootstrap cordova nfc


【解决方案1】:

在 Android 上,当 NFC 标签在手机范围内时,您需要调用 ndef.write(message)。最好的方法是从 nfcEvent Handler 内部调用 write。

# www/js/index.js
var app = {
    initialize: function() {
        this.bindEvents();
    },
    bindEvents: function() {
        document.addEventListener('deviceready', this.onDeviceReady, false);
    },
    onDeviceReady: function() {
        nfc.addNdefListener(app.onNfc);
    },
    onNfc: function(nfcEvent) {
        // message is an array of records
        var message = [
            ndef.textRecord("hello, world")
        ];
        nfc.write(message, app.onWriteSuccess);
    },
    onWriteSuccess: function() {
        alert("Wrote message to tag.");
    }
};

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-10-26
    • 1970-01-01
    • 1970-01-01
    • 2016-03-29
    • 1970-01-01
    • 1970-01-01
    • 2020-03-11
    • 1970-01-01
    相关资源
    最近更新 更多