获取navigator
mounted() {
this.navigator = window.navigator;
if (!window.device) {
this.$emit("changeValue", "1");
}
// // 全局HID设备拔出事件
navigator.hid.ondisconnect = event => {
this.$emit("changeValue", "1");
};
},
连接设备
async requestDevice() {
const [device] = await this.navigator.hid.requestDevice({
filters: [
{
vendorId: 0x0590,
},
],
});
if (device && !device.opened) {
// 检查设备是否打开
this.$emit("changeValue", "2");
await device.open(); // 打开设备
}
if (device && device.opened) {
this.$emit("changeValue", "3");
window.device = device;
// 设置数据监听器
window.device.oninputreport = event => {
let { data, device, reportId } = event;
this.buffer = new Uint8Array(data.buffer);
};
}
},
测完之后,获取结果
getInfo() {
if (this.buffer) {
//交给后台解析
this.$api.postOmronBloodData({
result: btoa(String.fromCharCode.apply(null, this.buffer)),
})
.then(res => {
let { sys, pulse, dia } = res.data;
if (res.data) {
this.ruleForm.highPressure = sys;
this.ruleForm.lowPressure = dia;
this.ruleForm.heartRate = pulse;
this.$message.success("获取成功");
} else {
this.$message.error(`${res.msg}`);
}
// sys: 收缩压, pulse: 脉搏, dia: 舒张压
})
.catch(err => {
this.$message.error("请重新测量");
});
} else {
this.$message.error("暂无数据");
}
},
后台解析
@ApiOperation("欧姆龙血压计")
@PostMapping("/omronSphygmomanometer")
public AjaxResult omronSphygmomanometer(@RequestBody ReceiveInfoBo receiveInfoBo) {
byte[] bytes= Base64.getMimeDecoder().decode(receiveInfoBo.getResult());
int i = 0;
byte a=0;
byte b=0;
byte c=0;
byte d=0;
byte e=0;
byte f=0;
byte g=0;
byte h=0;
byte m=0;
byte n=0;
for (byte by : bytes) {
switch (i) {
case 5:
a=by;
break;
case 6:
b=by;
break;
case 7:
c=by;
break;
case 8:
d=by;
break;
case 9:
e=by;
break;
case 10:
f=by;
break;
case 11:
g=by;
break;
case 12:
h=by;
break;
case 13:
m=by;
break;
case 14:
n=by;
break;
}
i = i + 1;
}
byte[] byteSys = new byte[]{a,b,c,d};
byte[] byteDia =new byte[]{e,f,g,h};
byte[] bytePulse =new byte[]{m,n};
int sys = Integer.parseInt(new String(byteSys, Charset.forName("UTF-8")), 16) / 128;
int dia = Integer.parseInt(new String(byteDia, Charset.forName("UTF-8")), 16) / 128;
int pulse = Integer.parseInt(new String(bytePulse, Charset.forName("UTF-8")), 16);
JSONObject jsonObject = new JSONObject();
jsonObject.set("sys", sys);
jsonObject.set("dia", dia);
jsonObject.set("pulse", pulse);
return new AjaxResult(200,"成功",jsonObject);
}