Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Communicate with Android bluetooth device via NodeJS and Termux

We are looking to communicate with a bluetooth device connected to an Android tablet. We are using Termux and have NodeJS installed. Does anyone know if it is even possible to communicate with a bluetooth device? Would we have to attempt to communicate with the device directly through the /dev folder?

It is my understanding that Android is built on top of the Linux kernel, however, it has implemented specific things on top of it to interact for other things such as connectivity. Would the device even be accessible through the /dev folder via NodejS "serialport" or another tool?

As a last resort, if this is not possible, I guess we could try to build NodeJS in the Android OS through a rooted terminal. I've heard this isn't as easy as one would think though. Through Termux I am able to access the /dev folder and see all the devices. Not sure how the permission would work though. Thanks.

enter image description here

like image 786
wayofthefuture Avatar asked Mar 26 '17 20:03

wayofthefuture


1 Answers

You can communicate through the serial port using this tool. I have never use this tool but providing this only as a reference, since android is built on a linux kernel this might work. Please note that the examples are same as documentation.

https://github.com/eelcocramer/node-bluetooth-serial-port

Basic client usage

var btSerial = new (require('bluetooth-serial-port')).BluetoothSerialPort();

btSerial.on('found', function(address, name) {
    btSerial.findSerialPortChannel(address, function(channel) {
        btSerial.connect(address, channel, function() {
            console.log('connected');

            btSerial.write(new Buffer('my data', 'utf-8'), function(err, bytesWritten) {
                if (err) console.log(err);
            });

            btSerial.on('data', function(buffer) {
                console.log(buffer.toString('utf-8'));
            });
        }, function () {
            console.log('cannot connect');
        });

        // close the connection when you're ready
        btSerial.close();
    }, function() {
        console.log('found nothing');
    });
});

btSerial.inquire();

Basic server usage (only on Linux)

var server = new(require('bluetooth-serial-port')).BluetoothSerialPortServer();

var CHANNEL = 10; // My service channel. Defaults to 1 if omitted.
var UUID = '38e851bc-7144-44b4-9cd8-80549c6f2912'; // My own service UUID. Defaults to '1101' if omitted

server.listen(function (clientAddress) {
    console.log('Client: ' + clientAddress + ' connected!');
    server.on('data', function(buffer) {
        console.log('Received data from client: ' + buffer);

        // ...

        console.log('Sending data to the client');
        server.write(new Buffer('...'), function (err, bytesWritten) {
            if (err) {
                console.log('Error!');
            } else {
                console.log('Send ' + bytesWritten + ' to the client!');
            }
        });
    });
}, function(error){
    console.error("Something wrong happened!:" + error);
}, {uuid: UUID, channel: CHANNEL} );
like image 176
Kalana Demel Avatar answered Sep 19 '22 17:09

Kalana Demel