问题描述
我正在尝试使用 noble(https://github.com/abandonware/noble) BLE 读取我的血压值。 服务是1810,特征是2a35,但是buffer是空的。属性为:['指示']
我知道我必须订阅才能读取缓冲区,但我不知道该怎么做。有人可以帮忙吗? 我使用此代码:
const noble = require('@abandonware/noble');
noble.on('stateChange',async (state) => {
if (state === 'poweredOn') {
console.log('Scanning');
await noble.startScanningAsync([],false);
} else {
noble.stopScanning();
}
});
noble.on('discover',peripheral => {
// connect to the first peripheral that is scanned
noble.stopScanning();
const name = peripheral.advertisement.localName;
console.log(`Connecting to '${name}' ${peripheral.id}`);
connectAndSetUp(peripheral);
});
function connectAndSetUp(peripheral) {
peripheral.connect(error => {
console.log('Connected to',peripheral.id);
// specify the services and characteristics to discover
const serviceUUIDs = ['1810'];
const characteristicUUIDs = ['2a35'];
peripheral.discoverSomeServicesAndCharacteristics(
serviceUUIDs,characteristicUUIDs,onServicesAndCharacteristicsDiscovered
);
});
peripheral.on('disconnect',() => console.log('disconnected'));
}
function onServicesAndCharacteristicsDiscovered(error,services,characteristics) {
console.log('Discovered services and characteristics');
const echoCharacteristic = characteristics[0];
console.log(echoCharacteristic);
// console.log( echoCharacteristic.read());
// data callback receives notifications
echoCharacteristic.on('data',(data,isNotification) => {
console.log(`Received: "${data}"`);
});
// echoCharacteristic.once('data',data => console.log("oii "+data));
// subscribe to be notified whenever the peripheral update the characteristic
echoCharacteristic.subscribe( (error) => {
if (error) {
console.error('Error subscribing to echoCharacteristic');
} else {
// console.log( echoCharacteristic.Buffer);
console.log('Subscribed for echoCharacteristic notifications');
}
});
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)