Previously, we showed how you can easily start developing NFC applications on NodeJS with a Tappy device using the TappyTcmpJs library. While that project was very short, much of the code we had to write was boilerplate for composing commands and making sense of the Tappy’s responses. This is necessary in order to take advantage of the full power of the Tappy family of NFC readers with all of their advanced commands and any custom commands we may develop for your use, but in a lot of applications, you only really need to detect tag UIDs as well as read and write NDEF messages. For these applications, we have another library – the TcmpTappyWrapperJs.
Using the Wrapper
The wrapper is completely API compatible with the base Tappy SDK found in the TappyTcmpJs library, so using this wrapper doesn’t limit restrict you to just basic operations in any way. Creating/connecting to Tappies also works the same way as in the base library, so please refer to the previous post if you want details of how that works. To recap, here is the code to connect to a TappyUSB attached to /dev/ttyUSB0
.
var Tappy = require("@taptrack/tappy-wrapper");
var SerialCommunicator = require("@taptrack/tappy-nodeserialcommunicator");
var connectTappy = function(path) {
var comm = new SerialCommunicator({path: path})
var tappy = new Tappy({communicator: comm});
tappy.connect(function() {
console.log("Tappy connected!");
tappy.disconnect(function() {
console.log("Tappy disconnected!");
process.exit(0);
});
});
}
connectTappy("/dev/ttyUSB0");
At this point, with the basic TappyTcmpJs library, we had to start composing commands and defining command family resolvers. However, now to duplicate our previous utility’s tag detection capability, we need to do much less:
var Tappy = require("@taptrack/tappy-wrapper");
var SerialCommunicator = require("@taptrack/tappy-nodeserialcommunicator");
var streamTags = function(path,timeout) {
var comm = new SerialCommunicator({path: path})
var tappy = new Tappy({communicator: comm});
tappy.on('tag_found',function(ev) {
console.log("UID: %s",ev.tagCodeStr);
});
tappy.on('timeout_reached',function() {
console.log("Timeout reached");
});
tappy.connect(function() {
console.log("Tappy connected!");
tappy.detectTag(true,timeout);
});
}
streamTags("/dev/ttyUSB0",5);
And that’s it! Now we have a command line application that will connect to a TappyUSB and stream tag codes for five seconds in just 22 lines of code.
Dec 8, 2016