Skip to content

TapTrack/NdefJS

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

NdefJS

Library for creating and parsing NDEF messages.

Installing

Bower

bower install ndef

NPM

npm install @taptrack/ndef

Creating Records

Text records

var ndef = require('@taptrack/ndef');

// language code is optional, defaults to 'en'
var textRecord = ndef.Utils.createTextRecord("sirop","fr"); 
var message = new ndef.Message([textRecord]);

// Uint8Array for storing, writing to a tag, etc.
var bytes = message.toByteArray(); 

URI records

// NDEF uri prefixes will be automatically applied
var uriRecord = ndef.Utils.createUriRecord("http://www.google.com"); 
var message = new ndef.Message([uriRecord]);

// Uint8Array for storing, writing to a tag, etc.
var bytes = message.toByteArray(); 

Custom records

// creating message to be carried inside smartposter record
var uri = ndef.Utils.createUriRecord("http://www.google.com");
var payloadMsg = new ndef.Message([uri]);

var chunked = false; // non-chunked record
var tnf = new Uint8Array([0x01]); // TNF well known
var type = new Uint8Array([0x53,0x70]);
var payload = payloadMessage.toByteArray();

var spRecord = new ndef.Record(chunked, tnf, type, id, payload);
var spMessage = new ndef.Message([record]);

// Uint8Array for storing, writing to a tag, etc.
var bytes = spMessage.toByteArray();

Parsing messages

Text records

var message = ndef.Message.fromBytes(byteArray);
var records = message.getRecords();

var recordContents = ndef.Utils.resolveTextRecord(parsedRecords[0]);
console.log("Language: " + recordContents.language);
console.log("Content: " + recordContents.content);

URI records

var message = ndef.Message.fromBytes(byteArray);
var parsedRecords = message.getRecords();

var uri = ndef.Utils.resolveUriRecordToString(parsedRecords[0]);
console.log("URI: " + uri);

Other records

// takes a Uint8Array containing a valid, complete message
var message = ndef.Message.fromBytes(byteArray);

// array of all the records
var records = message.getRecords();

for(var i = 0; i < records.length; i++) {
    var record = records[i];
    console.log("Chunked: " + (record.isChunked ? "Yes" : "No"));
    console.log("TNF: " + record.getTnf().toString());
    console.log("Type: " + record.getType().toString());
    console.log("ID: " + record.getId().toString());
    console.log("Payload: " + record.getPayload().toString());
}

About

Library for creating and parsing NDEF messages

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 3

  •  
  •  
  •