how to receive data from bluetooth device using node.js

前端 未结 3 1101
情话喂你
情话喂你 2021-02-06 00:14

I am new to javascript and node.js. Currently am working in medical project. First i will explain my work. I have to receive data from Bluetooth device (normal BP rate ,pulse ra

3条回答
  •  一整个雨季
    2021-02-06 01:12

    You can use "node-bluetooth" to send and receive data from and to a device respectively. This is a sample code:-

    const bluetooth = require('node-bluetooth');
    
    // create bluetooth device instance
    
    const device = new bluetooth.DeviceINQ();
    
    device
        .on('finished', console.log.bind(console, 'finished'))
        .on('found', function found(address, name) {
            console.log('Found: ' + address + ' with name ' + name);
    
            device.findSerialPortChannel(address, function(channel) {
                console.log('Found RFCOMM channel for serial port on %s: ', name, channel);
    
                // make bluetooth connect to remote device
                bluetooth.connect(address, channel, function(err, connection) {
                    if (err) return console.error(err);
                    connection.write(new Buffer('Hello!', 'utf-8'));
                });
    
            });
    
            // make bluetooth connect to remote device
            bluetooth.connect(address, channel, function(err, connection) {
                if (err) return console.error(err);
    
                connection.on('data', (buffer) => {
                    console.log('received message:', buffer.toString());
                });
    
                connection.write(new Buffer('Hello!', 'utf-8'));
            });
        }).inquire();
    

    It scans for the device name given in "device" variable.

提交回复
热议问题