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
Show file tree
Hide file tree
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
removed package-lock.json
  • Loading branch information
somq committed Jan 8, 2018
commit 4a2b387db1c499c59bd5ced8c67f62de1a512415
64 changes: 32 additions & 32 deletions examples/ndef.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,52 +60,52 @@ nfc.on('reader', reader => {
} catch (err) {
console.error(`error when reading data`, err);
}
});


/**
* Read a card
*/

try {

/**
* READ MESSAGE AND ITS RECORDS
* Read a card
*/

/**
* 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);
try {

const tag = nfcCard.parseInfo(cardHeader);
console.log('tag info:', JSON.stringify(tag));
/**
* READ MESSAGE AND ITS RECORDS
*/

/**
* 2 - Read the NDEF message and parse it if it's supposed there is one
*/
/**
* 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);

// There might be a NDEF message and we are able to read the tag
if(nfcCard.isFormatedAsNDEF() && nfcCard.hasReadPermissions() && nfcCard.hasNDEFMessage()) {
const tag = nfcCard.parseInfo(cardHeader);
console.log('tag info:', JSON.stringify(tag));

// 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
/**
* 2 - Read the NDEF message and parse it if it's supposed there is one
*/

// Parse the buffer as a NDEF raw message
const NDEFMessage = nfcCard.parseNDEF(NDEFRawMessage);
// There might be a NDEF message and we are able to read the tag
if(nfcCard.isFormatedAsNDEF() && nfcCard.hasReadPermissions() && nfcCard.hasNDEFMessage()) {

console.log('NDEFMessage:', NDEFMessage);
// 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

} 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.')
}
// Parse the buffer as a NDEF raw message
const NDEFMessage = nfcCard.parseNDEF(NDEFRawMessage);

} catch (err) {
console.error(`error when reading data`, err);
}
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
Loading