how to receive data from bluetooth device using node.js

前端 未结 3 1172
情话喂你
情话喂你 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:09

    Try noble library. That's how I get information about my Xiaomi Mi Band 3 device:

    const arrayBufferToHex = require('array-buffer-to-hex')
    const noble = require('noble')
    
    const DEVICE_INFORMATION_SERVICE_UUID = '180a'
    
    noble.on('stateChange', state => {
      console.log(`State changed: ${state}`)
      if (state === 'poweredOn') {
        noble.startScanning()
      }
    })
    
    noble.on('discover', peripheral => {
      console.log(`Found device, name: ${peripheral.advertisement.localName}, uuid: ${peripheral.uuid}`)
    
      if (peripheral.advertisement.localName === 'Mi Band 3') {
        noble.stopScanning()
    
        peripheral.on('connect', () => console.log('Device connected'))
        peripheral.on('disconnect', () => console.log('Device disconnected'))
    
        peripheral.connect(error => {
          peripheral.discoverServices([DEVICE_INFORMATION_SERVICE_UUID], (error, services) => {
            console.log(`Found service, name: ${services[0].name}, uuid: ${services[0].uuid}, type: ${services[0].type}`)
    
            const service = services[0]
    
            service.discoverCharacteristics(null, (error, characteristics) => {
              characteristics.forEach(characteristic => {
                console.log(`Found characteristic, name: ${characteristic.name}, uuid: ${characteristic.uuid}, type: ${characteristic.type}, properties: ${characteristic.properties.join(',')}`)
              })
    
              characteristics.forEach(characteristic => {
                if (characteristic.name === 'System ID' || characteristic.name === 'PnP ID') {
                  characteristic.read((error, data) => console.log(`${characteristic.name}: 0x${arrayBufferToHex(data)}`))
                } else {
                  characteristic.read((error, data) => console.log(`${characteristic.name}: ${data.toString('ascii')}`))
                }
              })
            })
          })
        })
      }
    })
    

提交回复
热议问题