簡體   English   中英

Cordova NFC插件將文本寫入NFC卡

[英]Cordova nfc plugin write text to nfc card

我正在用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是全局變量,對嗎?

問候

在Android上,當NFC標簽在手機范圍內時,您需要調用ndef.write(message)。 最好的方法是從nfcEvent處理程序內部調用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.");
    }
};

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM