Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to receive data from bluetooth device using node.js

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 rate ) and display the readings in the web app using node.js. I don't know how to receive data from Bluetooth device (patient monitor machine) can you guys suggest me some blogs or books to read. Thanks in advance.

like image 454
Pradeep Avatar asked Dec 12 '17 03:12

Pradeep


People also ask

Does Bluetooth use API?

The app framework provides access to the Bluetooth functionality through Bluetooth APIs. These APIs let apps connect to other Bluetooth devices, enabling point-to-point and multipoint wireless features. Using the Bluetooth APIs, an app can perform the following: Scan for other Bluetooth devices.

Can a web app access Bluetooth?

The Web Bluetooth API allows websites to communicate with Bluetooth devices.

How do I remotely control a Bluetooth device?

Load the Web Bluetooth App Load the web app on your laptop or android device in the Chrome browser. Turn on Bluetooth on your smartphone or laptop. Click on the “Connect” button which will trigger device scanning. After connecting, you can see the device's CPU information and will be able to toggle the LED on or off.


2 Answers

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.

like image 53
Asim Avatar answered Sep 25 '22 02:09

Asim


You can use node-ble a Node.JS library that leverages on D-Bus and avoids C++ bindings. https://github.com/chrvadala/node-ble

Here a basic example

async function main () {
  const { bluetooth, destroy } = createBluetooth()

  // get bluetooth adapter
  const adapter = await bluetooth.defaultAdapter()
  await adapter.startDiscovery()
  console.log('discovering')

  // get device and connect
  const device = await adapter.waitDevice(TEST_DEVICE)
  console.log('got device', await device.getAddress(), await device.getName())
  await device.connect()
  console.log('connected')

  const gattServer = await device.gatt()

  // read write characteristic
  const service1 = await gattServer.getPrimaryService(TEST_SERVICE)
  const characteristic1 = await service1.getCharacteristic(TEST_CHARACTERISTIC)
  await characteristic1.writeValue(Buffer.from('Hello world'))
  const buffer = await characteristic1.readValue()
  console.log('read', buffer, buffer.toString())

  // subscribe characteristic
  const service2 = await gattServer.getPrimaryService(TEST_NOTIFY_SERVICE)
  const characteristic2 = await service2.getCharacteristic(TEST_NOTIFY_CHARACTERISTIC)
  await characteristic2.startNotifications()
  await new Promise(done => {
    characteristic2.once('valuechanged', buffer => {
      console.log('subscription', buffer)
      done()
    })
  })

  await characteristic2.stopNotifications()
  destroy()
}
like image 38
chrvadala Avatar answered Sep 25 '22 02:09

chrvadala