我是javascript和node.js的新手。目前正在从事医疗项目。首先,我将解释我的工作。我必须从蓝牙设备接收数据(正常血压率,脉搏率)并使用node.js在Web应用程序中显示读数。我不知道如何从蓝牙设备(病人监护仪)接收数据你们可以建议我阅读一些博客或书籍。提前谢谢。
答案 0 :(得分:6)
您可以使用“node-bluetooth”分别从设备发送数据和从设备接收数据。这是一个示例代码: -
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();
它会扫描“device”变量中给出的设备名称。
答案 1 :(得分:2)
您可以使用 node-ble 一个Node.JS库,该库利用D-Bus并避免C ++绑定。 https://github.com/chrvadala/node-ble
这是一个基本示例
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()
}
答案 2 :(得分:1)
尝试noble库。这就是我获取有关小米Mi Band 3设备的信息的方式:
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')}`))
}
})
})
})
})
}
})