0
votes

I am struggling quite a while now to get a solid, long-term connection to a bluetooth barcode scanner from Inateck using node.js. The process is running in the background (linux, no input-focus) that's why I configured the scanner as a SPP device.

The connection is basically working as long as the scanner doesn't automatically switch off to save power, which is after about 5 minutes.

My first approach was to use the bluetooth-serial-port package. It discovers the scanner, reads the barcodes but when the scanner switches off, I don't know how to re-connect. I added an interval timer to check the connection and try to connect again if isOpen() returns false (which works once). When I press the button on the scanner it switches back on and I can re-connect but after a view seconds isOpen() returns false even if the connection is established, and I don't get any further readings. Here is my 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 to ' + address);

                btInterval = setInterval(function () {
                    if (!btSerial.isOpen()) {

                        btSerial.close();
                        clearInterval(btInterval);
                        console.log('lost connection - try to reconnect');

                        btSerial.inquire();
                    }

                }, 5000);

            }, function () {
                console.log('cannot connect to ' + 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 like this:

finished
connected to 00:06:11:68:15:81
found nothing
... scanning works ...
lost connection - try to reconnect
finished
connected to 00:06:11:68:15:81
... scanning works ...
lost connection - try to reconnect
finished
... that's it - no more scans ...
^C

An other idea was to use nodes fs() an read directly from '/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 is done by the OS automatically when reading from the device and I do receive the codes via on('data',..). But I do have the same problem when the scanner switches off after a while. I do receive the on('close',..) event but reconnecting using fs.createReadStream() again doesn't work any more.

Maybe someone of you already had to deal which such a problem and can give me a hint how to handle this. I appreciate every suggestion.

Thanks, Max

1

1 Answers

0
votes

That's not the way I wanted to go for but a bash script to launch my node app when the scanner is available, does the job:

#!/bin/bash

echo "Press CTRL+C to stop..."

while :
do
   if hcitool scan | grep -q "00:06:11:68:15:81"; then
     # BT scanner found
     node .
   fi

   sleep 1
done