Search code examples
cordovaphonegap-pluginsnfcphonegap-build

Cordova NFC plugin not working


I am trying to get build a test cordova application to read and write NFC tags targeting windows phones and android phone. When the test application is deployed to devices, on android I get an error message telling “Class Not found” while trying to register the listener. On windows phone I get nothing.

Following are the steps I used to create the application using the cordova CLI

Created a cordova application

Added platforms

Added the plugin (https://github.com/chariotsolutions/phonegap-nfc.git)

Added the following code replacing deviceready in index.js file

    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');

}

Compressed the project folder and uploaded it to phone-gap-build.

Got the project build and deployed to android and windows 8.1 phones (nfc was enabled on both devices). Phone-gap build has used PhoneGap 3.7.0 to build the application

When I tried to execute the app, while trying to register the listener, Android complained ‘Class not found’. Windows phone did not throw any errors (at least nothing I could see), but was not recognizing any of the NFC cards presented to it. Under \nfcReaderB\platforms\android\src\com\chariotsolutions\nfc\plugin, I can see the required java source files for android platform

NFC functions on the phones are working fine

The code is available at https://github.com/cmeegamarachchi/nfc

And help on resolving this is much appreciated


Solution

  • 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