Urb*_*ban 3 javascript android nfc phonegap-plugins cordova
我按照phonegap-nfc项目页面上的说明为我的phonegap项目安装了nfc插件.
在启动应用程序时,我确实看到了Waiting for NDEF tag警报.但是,在将NFC卡轻触手机时,我只能听到失败的NFC声音(您可以在此视频中听到声音).我不确定这里有什么不对.
代码与上面第一个链接的指示完全相同.为简洁起见,我也会在这里复制代码:
我的index.js有 -
onDeviceReady: function() {
app.receivedEvent('deviceready');
// Read NDEF formatted NFC Tags
nfc.addNdefListener (
function (nfcEvent) {
var tag = nfcEvent.tag,
ndefMessage = tag.ndefMessage;
// dump the raw json of the message
// note: real code will need to decode
// the payload from each record
alert(JSON.stringify(ndefMessage));
// assuming the first record in the message has
// a payload that can be converted to a string.
alert(nfc.bytesToString(ndefMessage[0].payload).substring(3));
},
function () { // success callback
alert("Waiting for NDEF tag");
},
function (error) { // error callback
alert("Error adding NDEF listener " + JSON.stringify(error));
}
);
},
Run Code Online (Sandbox Code Playgroud)
该插件只允许写入/读取NDEF标记,因为这种操作非常简单.
NFC卡可能要复杂得多,并且需要根据卡的类型遵循特定的结构和加密.有时需要向卡发送命令并等待复杂协议之后的响应.
在您的情况下,Mifare经典卡,您必须知道能够读取数据的密钥.
由于这些原因,通用插件不允许读取任何类型的NFC卡.
该chariotsolutions插件允许完全访问NDEF标签,但只让你得到其他卡的标签ID(在这种情况下使用nfc.addTagDiscoveredListener代替)
要执行更具体的操作,可以从这个开始创建自己的插件.
要查看您拥有哪种卡,您可以使用此应用程序
您还可以查看有关NFC的Google页面以获取更多参考,或者查看来自摩托罗拉的这个有趣的文档.