These instructions use PhoneGap to generate a default PhoneGap app, modify it to read NFC tags and build using PhoneGap Build.
The PhoneGap Command Line Interface phonegap-cli can be used to create, build and run multi-platform PhoneGap projects. PhoneGap CLI requires node.js.
$ npm install phonegap -g
$ cd ~
$ phonegap create nfc com.example.nfc NFC
Change to the project directory
$ cd nfc
Edit config.xml
and add a line to install the plugin
<gap:plugin name="phonegap-nfc" source="npm" />
Edit index.js
and modify onDeviceReady with the following code
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));
}
);
},
$ phonegap remote build android
Log into http://build.phonegap.com with your phone.
Download and install the app.
Run the app.
Scan an NDEF tag with your phone. If you need to put data on a tag, try writing a plain text message to a tag with NXP Tag Writer.