2013-11-27 26 views
1

我正在測試與科爾多瓦寫一個簡單的文本到卡。在引導按鈕我有科爾多瓦nfc插件寫入文字到nfc卡

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是一個全局變量,對嗎?

問候

+0

你弄明白了嗎? –

回答

0

在Android上,你需要調用ndef.write(消息)當一個NFC標籤是在手機的範圍。最好的方法是從nfcEvent處理程序中調用寫入。

# 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."); 
    } 
};