我正在尝试构建一个测试cordova应用程序来读取和写入针对Windows手机和Android手机的NFC标签。 当测试应用程序部署到设备时,在 Android 上,我在尝试注册监听器时收到一条错误消息,告知“未找到类”。在 Windows Phone 上我什么也没得到。
以下是我使用 cordova CLI 创建应用程序的步骤
创建了cordova应用程序
添加的平台
添加了插件 ( https://github.com/chariotsolutions/phonegap-nfc.git )
添加了以下代码来替换index.js文件中的deviceready
try {
// 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));
}
);
} catch (ex) {
alert(ex.message);
}
app.receivedEvent('deviceready');
}
压缩项目文件夹并将其上传到phone-gap-build。
构建项目并将其部署到 Android 和 Windows 8.1 手机(两台设备上均启用了 NFC)。 Phone-gap build 使用 PhoneGap 3.7.0 来构建应用程序
当我尝试执行应用程序并尝试注册监听器时,Android 提示“找不到类”。 Windows Phone 没有抛出任何错误(至少我看不到任何错误),但无法识别提供给它的任何 NFC 卡。 在\nfcReaderB\platforms\android\src\com\chariotsolutions\nfc\plugin下,我可以看到android平台所需的java源文件
手机上的 NFC 功能运行良好
代码可在https://github.com/cmeegamarachchi/nfc获取
非常感谢您帮助解决这个问题
最佳答案
问题是您使用 PhoneGap Build 但没有正确配置插件。命令行工具用于在本地构建时安装插件。对于PhoneGap构建,您需要在config.xml中定义插件。
<gap:plugin name="phonegap-nfc" source="npm" />
使用phonegap
命令将项目上传到PhoneGap Build服务器。
phonegap remote build android
登录http://build.phonegap.com将应用程序下载到您的手机上。
代码的更新版本已发布 https://github.com/don/phonegap-nfc-issue-190
有关 PhoneGap Build 插件的更多信息,请参阅 http://docs.build.phonegap.com/en_US/configuring_plugins.md.html#Plugins
关于Cordova NFC 插件不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30937025/