Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added examples on how to write or read NDEF messages using nfccard-tool #31

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
swapped write and read
  • Loading branch information
somq committed Jan 8, 2018
commit acef2b164fe82786f4b986c8faf30aa2a6818e08
88 changes: 44 additions & 44 deletions examples/ndef.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,50 +22,6 @@ nfc.on('reader', reader => {
console.log(`card detected`, card);


/**
* Read a card
*/

try {

/**
* READ MESSAGE AND ITS RECORDS
*/

/**
* 1 - READ HEADER
* Read from block 0 to block 4 (20 bytes length) in order to parse tag information
*/
// Starts reading in block 0 until end of block 4
const cardHeader = await reader.read(0, 20);

const tag = nfcCard.parseInfo(cardHeader);
console.log('tag info:', JSON.stringify(tag));

/**
* 2 - Read the NDEF message and parse it if it's supposed there is one
*/

// There might be a NDEF message and we are able to read the tag
if(nfcCard.isFormatedAsNDEF() && nfcCard.hasReadPermissions() && nfcCard.hasNDEFMessage()) {

// Read the appropriate length to get the NDEF message as buffer
const NDEFRawMessage = await reader.read(4, nfcCard.getNDEFMessageLengthToRead()); // starts reading in block 0 until 6

// Parse the buffer as a NDEF raw message
const NDEFMessage = nfcCard.parseNDEF(NDEFRawMessage);

console.log('NDEFMessage:', NDEFMessage);

} else {
console.log('Could not parse anything from this tag: \n The tag is either empty, locked, has a wrong NDEF format or is unreadable.')
}

} catch (err) {
console.error(`error when reading data`, err);
}


/**
* Write a card
*/
Expand Down Expand Up @@ -107,6 +63,50 @@ nfc.on('reader', reader => {
});


/**
* Read a card
*/

try {

/**
* READ MESSAGE AND ITS RECORDS
*/

/**
* 1 - READ HEADER
* Read from block 0 to block 4 (20 bytes length) in order to parse tag information
*/
// Starts reading in block 0 until end of block 4
const cardHeader = await reader.read(0, 20);

const tag = nfcCard.parseInfo(cardHeader);
console.log('tag info:', JSON.stringify(tag));

/**
* 2 - Read the NDEF message and parse it if it's supposed there is one
*/

// There might be a NDEF message and we are able to read the tag
if(nfcCard.isFormatedAsNDEF() && nfcCard.hasReadPermissions() && nfcCard.hasNDEFMessage()) {

// Read the appropriate length to get the NDEF message as buffer
const NDEFRawMessage = await reader.read(4, nfcCard.getNDEFMessageLengthToRead()); // starts reading in block 0 until 6

// Parse the buffer as a NDEF raw message
const NDEFMessage = nfcCard.parseNDEF(NDEFRawMessage);

console.log('NDEFMessage:', NDEFMessage);

} else {
console.log('Could not parse anything from this tag: \n The tag is either empty, locked, has a wrong NDEF format or is unreadable.')
}

} catch (err) {
console.error(`error when reading data`, err);
}


reader.on('card.off', card => {
console.log(`${reader.reader.name} card removed`, card);
});
Expand Down