node.js and bluetooth barcode-scanner -
i struggling quite while solid, long-term connection bluetooth barcode scanner inateck using node.js. process running in background (linux, no input-focus) that's why configured scanner spp device.
the connection working long scanner doesn't automatically switch off save power, after 5 minutes.
my first approach use bluetooth-serial-port package. discovers scanner, reads barcodes when scanner switches off, don't know how re-connect. added interval timer check connection , try connect again if isopen() returns false (which works once). when press button on scanner switches on , can re-connect after view seconds isopen() returns false if connection established, , don't further readings. here code:
var btserial = new (require('bluetooth-serial-port')).bluetoothserialport(); var btinterval = null; btserial.on('found', function (address, name) { btserial.findserialportchannel(address, function (channel) { if (address === '00:06:11:68:15:81') { btserial.connect(address, channel, function () { console.log('connected ' + address); btinterval = setinterval(function () { if (!btserial.isopen()) { btserial.close(); clearinterval(btinterval); console.log('lost connection - try reconnect'); btserial.inquire(); } }, 5000); }, function () { console.log('cannot connect ' + address); }); } }, function () { console.log('found nothing'); }); }); btserial.on('finished', function () { console.log('finished'); }); btserial.on('data', function (buffer) { console.log(buffer.tostring('utf-8')); }); btserial.inquire();
the output looks this:
finished connected 00:06:11:68:15:81 found nothing ... scanning works ... lost connection - try reconnect finished connected 00:06:11:68:15:81 ... scanning works ... lost connection - try reconnect finished ... that's - no more scans ... ^c
an other idea use nodes fs() read directly '/dev/rfcomm0'.
scanner = fs.createreadstream('/dev/rfcomm0', {buffersize: 1}); scanner.on('open', function () { logger.info('scanner connected'); }); scanner.on('end', function () { logger.info('end of data stream'); }); scanner.on('close', function () { logger.info('scanner disconnected'); }); scanner.on('error', function (error) { logger.error('scanner error'); }); scanner.on('data', function (chunk) { logger.info(chunk.tostring('ascii', 0, 13)); } }); });
connecting done os automatically when reading device , receive codes via on('data',..). have same problem when scanner switches off after while. receive on('close',..) event reconnecting using fs.createreadstream() again doesn't work more.
maybe of had deal such problem , can give me hint how handle this. appreciate every suggestion.
thanks, max
that's not way wanted go bash script launch node app when scanner available, job:
#!/bin/bash echo "press ctrl+c stop..." while : if hcitool scan | grep -q "00:06:11:68:15:81"; # bt scanner found node . fi sleep 1 done
Comments
Post a Comment