how to receive data from bluetooth device using node.js

前端 未结 3 1141
情话喂你
情话喂你 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 00:50

    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()
    }
    

提交回复
热议问题