Cordova NFC插件无法正常工作

时间:2015-06-19 11:28:05

标签: cordova phonegap-plugins nfc phonegap-build

我正在尝试构建一个测试cordova应用程序来读取和写入针对Windows手机和Android手机的NFC标签。 当测试应用程序部署到设备时,在android上我在尝试注册监听器时收到一条错误消息,告诉“未找到类”。在Windows手机上,我什么都没得到。

以下是我使用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)。电话间隙构建使用PhoneGap 3.7.0构建应用程序

当我尝试执行应用程序时,在尝试注册侦听器时,Android抱怨“找不到类”。 Windows手机没有丢失任何错误(至少我看不到任何错误),但没有识别任何提供给它的NFC卡。 在\ nfcReaderB \ platforms \ android \ src \ com \ chariotsolutions \ nfc \ plugin下,我可以看到android平台所需的java源文件

手机上的NFC功能正常运行

该代码位于https://github.com/cmeegamarachchi/nfc

非常感谢帮助解决这个问题

1 个答案:

答案 0 :(得分:1)

The problem is that your using PhoneGap Build but didn't configure the plugin properly. The command line tool is used to install plugins when building locally. For PhoneGap build, you need to define plugins in config.xml.

<gap:plugin name="phonegap-nfc" source="npm" />

Use the phonegap command to upload the project to the PhoneGap Build servers.

phonegap remote build android

Login to http://build.phonegap.com to download the app to your phone.

An updated version of the code is available https://github.com/don/phonegap-nfc-issue-190

For more info on PhoneGap Build plugins see http://docs.build.phonegap.com/en_US/configuring_plugins.md.html#Plugins