examTeamApp/pageTwo/devices/F01PRO.vue

489 lines
17 KiB
Vue
Raw Normal View History

2025-08-06 15:49:20 +08:00
<template>
2025-08-18 10:58:58 +08:00
<view>
<view class="content weightPages">
<view class="title" v-if="isConnection == 0">连接中请稍后</view>
<view class="title" v-if="isConnection == 1">连接成功请开始测量</view>
<view class="title" v-if="isConnection == 2" @click="openBluetoothAdapter">连接失败点击重新连接</view>
<view class="text">{{textW}}</view>
<view class="text">{{textH}}</view>
<view class="text" v-if="textImp != ''">{{textImp}}</view>
<view class="image" style="margin-top: 200rpx;">
<image src="/pageTwo/static/F018P01.gif" mode="widthFix"></image>
</view>
<view class="tips">
<text>请确保</text>
<text>1.请确定设备是开机状态</text>
<text>2.请确定手机蓝牙位置信息已打开</text>
<text>3.ios系统需打开设置>应用>微信里的蓝牙权限</text>
</view>
</view>
</view>
2025-08-06 15:49:20 +08:00
</template>
<script>
2025-08-18 10:58:58 +08:00
import {
mapState
} from "vuex";
var myTime;
const plugin = requirePlugin("sdkPlugin").AiLink;
export default {
data() {
return {
textW: "",
textH: "",
2025-08-06 15:49:20 +08:00
textImp: "",
2025-08-18 10:58:58 +08:00
height: "",
weight: "",
imp: 0,
macAddr: "",
deviceId: "",
serviceId: "",
readId: "",
writeId: "",
notifyId: "",
isSend: true,
Unload: false,
isConnection: 0,
data01: {},
data02: {},
BLEResult: {}
}
},
computed: {
...mapState(["user", "isConnected", "isBluetoothTyle"]),
userInfo() {
return this.user
}
},
onUnload: async function() {
let that = this
if (!that.Unload) {
clearTimeout(myTime)
2025-08-06 15:49:20 +08:00
that.$store.state.devicesList2 = []
2025-08-18 10:58:58 +08:00
that.closeBLEConnection()
that.closeBluetoothAdapter()
if(that.isConnection != 1) {
let pages = getCurrentPages();
pages[pages.length - 2].$vm.islink = -1;
pages[pages.length - 2].$vm.textLink = '开始连接';
}
}
},
watch: {
isConnected: function() {
let that = this
if (!that.isConnected) {
2025-08-06 15:49:20 +08:00
console.log('F01PROisConnected-' + that.isConnected)
2025-08-18 10:58:58 +08:00
that.handleBack()
that.isConnection = 2
}
},
isBluetoothTyle: function() {
let that = this
if (!that.isBluetoothTyle) {
2025-08-06 15:49:20 +08:00
console.log('F01PROisBluetoothTyle-' + that.isBluetoothTyle)
2025-08-18 10:58:58 +08:00
that.handleBack()
that.isConnection = 2
}
},
},
onLoad(options) {
let that = this
that.textW = ""
that.textH = ""
console.log("options", options)
if (options && options.deviceId) {
that.deviceId = options.deviceId
that.createBLEConnection()
}
2025-08-06 15:49:20 +08:00
2025-08-18 10:58:58 +08:00
that.onBLEConnectionStateChange()
uni.onBluetoothAdapterStateChange(function(res) {
that.$store.commit("changeBluetooth", res.available);
})
},
methods: {
// 重新连接
openBluetoothAdapter() {
let that = this
that.textW = ""
that.textH = ""
that.isSend = true
2025-08-06 15:49:20 +08:00
that.Unload = false
2025-08-18 10:58:58 +08:00
uni.openBluetoothAdapter({
success: e => {
that.isConnection = 0
that.startBluetoothDeviceDiscovery()
console.log('初始化蓝牙成功:' + e.errMsg);
},
fail: e => {
that.isConnection = 2
that.$tools.msg("请确定设备是开机状态、手机蓝牙权限已打开!")
}
});
},
// 监听蓝牙连接状态
onBLEConnectionStateChange() {
let that = this
uni.onBLEConnectionStateChange(function(res) {
console.log('F01PROonBLEConnectionStateChange-' + res.connected)
if (!res.connected) {
that.Unload = true
that.isConnection = 2
clearTimeout(myTime);
that.closeBLEConnection()
that.closeBluetoothAdapter()
}
that.$store.commit("changeConnected", res.connected);
})
},
// 开始搜寻附近的蓝牙外围设备
startBluetoothDeviceDiscovery() {
let that = this
uni.startBluetoothDevicesDiscovery({
allowDuplicatesKey: false,
interval: 500, //上报设备的间隔
success: res => {
that.isConnection = 0
that.onBluetoothDeviceFound();
},
fail: res => {
that.isConnection = 2
that.$tools.msg("请确定设备是开机状态、手机蓝牙权限已打开!")
}
});
},
/**
* 发现外围设备
*/
onBluetoothDeviceFound() {
var that = this;
that.isConnection = 0
uni.onBluetoothDeviceFound(res => {
res.devices.forEach(device => {
if (!device.name && !device.localName) {
return
}
if (device.name.indexOf('AiLink_') != -1 || device.name.indexOf('PCF01') != -1 || (device
.localName && (device.localName
.indexOf('AiLink_') != -1 || device.localName
.indexOf('PCF01') != -1))) {
clearTimeout(myTime);
if (device.deviceId.indexOf(that.deviceId) != -1) {
that.stopBluetoothDevicesDiscovery()
that.deviceId = device.deviceId
const foundDevices = that.$store.state.devicesList2
const idx = that.$tools.inArray(foundDevices, "deviceId", device.deviceId)
if (idx === -1) {
that.$store.state.devicesList2.push(device);
} else {
that.$store.state.devicesList2[idx] = device
}
that.createBLEConnection()
return;
}
}
})
});
},
/**
* 停止搜索蓝牙设备
*/
stopBluetoothDevicesDiscovery() {
uni.stopBluetoothDevicesDiscovery({
success: e => {
console.log("停止搜索蓝牙设备", e)
},
});
},
// 连接蓝牙
createBLEConnection() {
let that = this;
uni.createBLEConnection({
deviceId: that.deviceId,
success: res => {
console.log("设备连接成功获取设备的services", res);
that.isConnection = 0
that.getBLEDeviceServices();
},
fail: res => {
that.isConnection = 2
console.log("设备连接失败,请重新连接", res);
}
});
},
/**
* 获取设备的UUID
*/
getBLEDeviceServices() {
let serviceList = [];
let that = this;
uni.getBLEDeviceServices({
deviceId: that.deviceId,
success: res => {
console.log("获取设备的UUID成功", res)
serviceList = res.services;
for (let i = 0; i < serviceList.length; i++) {
let service = serviceList[i];
if (service.uuid.indexOf('FFE0') != -1) {
that.serviceId = service.uuid;
that.isConnection = 1
that.getBLEDeviceCharacteristics(that.deviceId, service.uuid);
console.log("设备的FFE0的serviceId ", that.serviceId);
break;
}
}
},
fail: res => {
console.log('获取设备的UUID失败:', res)
}
});
},
/**
* 获取指定服务的特征值
*/
getBLEDeviceCharacteristics(deviceId, serviceId) {
let characteristicsList = [];
let that = this;
uni.getBLEDeviceCharacteristics({
deviceId: deviceId,
serviceId: serviceId,
success: res => {
console.log("服务的特征值成功", res)
characteristicsList = res.characteristics;
for (let i = 0; i < characteristicsList.length; i++) {
let item = characteristicsList[i];
if (item.uuid.indexOf('0000FFE1') != -1) {
that.uuid1 = item.uuid //下发数据
} else if (item.uuid.indexOf('0000FFE2') != -1) {
that.uuid2 = item.uuid //监听数据
} else if (item.uuid.indexOf('0000FFE3') != -1) {
that.uuid3 = item.uuid //写入设置
}
}
// 打开监听
uni.notifyBLECharacteristicValueChange({
deviceId,
serviceId,
characteristicId: that.uuid2,
state: true,
})
uni.notifyBLECharacteristicValueChange({
deviceId,
serviceId,
characteristicId: that.uuid3,
state: true,
})
// 初始化插件
console.log("初始化插件", that.$store.state.devicesList2)
that.$store.state.devicesList2[0].serviceId = that.serviceId
plugin.initPlugin(res.characteristics, that.$store.state.devicesList2[0])
uni.onBLECharacteristicValueChange((characteristic) => {
let bleData = plugin.parseBleData(characteristic.value)
let dw1 = "kg"
console.log("bleData", bleData)
if (bleData.status == 0) {
let sex0 = that.userInfo.gender == 1 ? 1 : 0
let sex = "0x0" + sex0.toString(16)
let age = "0x" + that.userInfo.age.toString(16)
let height = "0x" + that.userInfo.height.toString(16)
let arr = [0x01, parseInt(sex), parseInt(age), parseInt(height), 0x00]
plugin.sendDataOfA7(arr)
console.log("握手成功", arr)
} else if (bleData.status == 1) {
let payload = that.$tools.ab2hex(bleData.data, '')
let typeInfo = payload.substring(4, 6)
let type = payload.substring(0, 2)
console.log("开始测量", payload)
if (type == '03') {
2025-08-06 15:49:20 +08:00
console.log('MCU主动请求用户数据')
let sex0 = that.userInfo.gender == 1 ? 1 : 0
let sex = "0x0" + sex0.toString(16)
let age = "0x" + that.userInfo.age.toString(16)
let height = "0x" + that.userInfo.height.toString(16)
let arr = [0x01, parseInt(sex), parseInt(age), parseInt(height), 0x00]
plugin.sendDataOfA7(arr)
}
2025-08-18 10:58:58 +08:00
if (type == "10" || type == "30" || type == "40") { //体脂模式
let data = parseInt(payload.substring(6, 12), 16)
let msg = parseInt(payload.substring(12, 14), 16).toString(2)
let dw = that.$tools.PrefixZero(msg, 8).substring(4, 8)
let num = that.$tools.PrefixZero(msg, 8).substring(0, 4)
let dw1 = "kg"
if (dw == "0001") {
dw1 = '斤'
}
if (dw == "0110") {
dw1 = 'lb'
}
if (num == "0001") {
data = data / 10
}
if (num == "0010") {
data = data / 100
}
if (num == "0011") {
data = data / 1000
}
if (typeInfo == "01") {
that.textW = "您的实时体重是:" + data + dw1
}
if (typeInfo == "02") {
that.textW = "您的体重是:" + data + dw1
that.weight = data + dw1
}
}
if (type == "14" || type == "41") { //身高模式
let height = parseInt(payload.substring(4, 8), 16)
let numH = parseInt(payload.substring(10, 12), 16)
if (numH == "1") {
height = height / 10
}
if (numH == "2") {
height = height / 100
}
that.textH = "您的身高是:" + height + "cm"
that.height = height
console.log("身高模式:", that.height)
}
if (type == "11") { //阻抗模式
2025-08-06 15:49:20 +08:00
if (typeInfo == "01") { //测量中
2025-08-18 10:58:58 +08:00
that.textImp = '正在测量体脂:请双手紧握扶手'
2025-08-06 15:49:20 +08:00
}
2025-08-18 10:58:58 +08:00
if (typeInfo == "02") {
that.imp = 0
2025-08-06 15:49:20 +08:00
that.textImp = ""
2025-08-18 10:58:58 +08:00
}
if (typeInfo == "04") {
2025-08-06 15:49:20 +08:00
that.textImp = ""
2025-08-18 10:58:58 +08:00
that.imp = parseInt(payload.substring(8, 12), 16)
}
console.log("阻抗:", that.imp)
}
if (type == '15') {
if (typeInfo == "01") {
that.data01 = {
bodyage: parseInt(payload.substring(26, 28),
16),
fat_r: parseInt(payload.substring(6, 10),
16) / 10,
muscle: parseInt(payload.substring(18, 22),
16) / 10,
kcal: parseInt(payload.substring(22, 26),
16),
visceral: parseInt(payload.substring(14,
18), 16),
sfr: parseInt(payload.substring(10, 14),
16) / 10,
}
}
if (typeInfo == '02') {
that.data02 = {
water: parseInt(payload.substring(10, 14),
16) / 10,
bone: parseInt(payload.substring(6, 10),
16) / 10,
fatlevlval: parseInt(payload.substring(24,
26), 16) /
10,
protein: parseInt(payload.substring(14, 18),
16) / 10,
bmi: parseInt(payload.substring(18, 22),
16) / 10,
}
}
console.log("体脂数据", that.data01, that.data02)
that.BLEResult.info = Object.assign(that.data01, that.data02)
}
if (type == "80") { //测量结束
that.BLEResult.weight = that.weight
that.BLEResult.adc = that.imp ? that.imp : 0
that.BLEResult.height = (that.height ? that.height : that.userInfo.height) + 'cm'
that.BLEResult.aud_id = that.userInfo.id
console.log("体脂成功:", that.BLEResult)
that.handleGetMeasure(that.BLEResult)
}
}
})
},
fail: res => {
console.log('获取特征值失败:', JSON.stringify(res))
}
})
},
// 保存测量结果
handleGetMeasure(data) {
console.log("保存结果")
let that = this
that.$model.getmeasurefunit(data).then(res => {
if (res.code == 0) {
that.$store.dispatch('getUserInfo', {
aud_id: uni.getStorageSync('userid')
})
that.$store.dispatch("getResult", {
aud_id: uni.getStorageSync('userid')
})
2025-08-06 15:49:20 +08:00
setTimeout(() => {
uni.showToast({
title: '测量成功'
})
2025-08-18 10:58:58 +08:00
}, 500)
} else {
2025-08-06 15:49:20 +08:00
uni.showToast({
title: res.message,
2025-08-18 10:58:58 +08:00
icon: "error"
2025-08-06 15:49:20 +08:00
})
2025-08-18 10:58:58 +08:00
}
that.Unload = true
setTimeout(function() {
2025-08-06 15:49:20 +08:00
that.closeBLEConnection()
that.closeBluetoothAdapter()
that.$store.state.devicesList2 = []
2025-08-18 10:58:58 +08:00
uni.switchTab({
url: "/pages/home/home"
})
}, 1000)
})
},
/**
* 断开蓝牙模块
*/
closeBluetoothAdapter() {
let that = this;
uni.closeBluetoothAdapter({
success: res => {
console.log('F01PRO蓝牙模块关闭成功');
}
})
},
handleBack() {
let that = this
that.Unload = true
2025-08-06 15:49:20 +08:00
console.log('F01PROhandleBack');
2025-08-18 10:58:58 +08:00
that.stopBluetoothDevicesDiscovery() //取消蓝牙搜索
that.closeBLEConnection()
that.closeBluetoothAdapter()
2025-08-06 15:49:20 +08:00
that.$store.state.devicesList2 = []
2025-08-18 10:58:58 +08:00
},
/**
* 断开蓝牙连接
*/
closeBLEConnection() {
var that = this;
uni.closeBLEConnection({
deviceId: that.deviceId,
success: res => {
console.log('F01PRO断开蓝牙连接成功');
}
});
},
},
}
2025-08-06 15:49:20 +08:00
</script>
<style scoped lang="scss">
</style>