examTeamApp/pageTwo/devices/G02 -广播.vue

285 lines
7.0 KiB
Vue
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<template>
<view class="weightPages">
<view class="content ">
<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">{{text}}</view>
<view class="image">
<image src="/static/HC.png" class="image3"></image>
</view>
<view class="tips">
<view>提示:</view>
<text>1.请确定设备已开机</text>
<text>2.请确定手机蓝牙及位置信息已打开</text>
</view>
</view>
<!-- 手动记录 -->
<view class="wrapper" v-if="isHeight">
<view class="bg"></view>
<view class="Blue">
<view class="h4">测量结果提示</view>
<view class="Blue-box">
本次测量身高为:<text>{{height}}{{unit}}</text>
</view>
<view class="Blue-box">
上次测量体重为:<input v-model="weight" type="digit" placeholder="请输入体重" />kg
</view>
<view class="Blue-btn Blue-close" @click="handleBack(1)">取消</view>
<view class="Blue-btn" @click="handleGetMeasure">保存测量结果</view>
</view>
</view>
</view>
</template>
<script>
import {
mapState
} from "vuex";
let myTime;
export default {
data() {
return {
text: "",
weight: "",
height: "",
deviceId: "",
macAddr: "",
unit: "cm",
Unload: false,
stopblue: true,
isHeight: false,
isConnection: 0,
isdevice: false
}
},
computed: {
...mapState(["user", "isConnected", "isBluetoothTyle"]),
info() {
return this.user
},
endDate() {
return this.$tools.getDate("start")
},
startDate() {
return this.$tools.GetDateStr(-90);
},
},
onUnload: function() {
let that = this
if (!that.Unload) {
clearTimeout(myTime)
that.stopBluetoothDevicesDiscovery() //取消蓝牙搜索
that.closeBluetoothAdapter()
console.log("页面返回onUnload")
}
},
onLoad(options) {
let that = this
that.text = ""
that.isdevice = options.isdevice
that.openBluetoothAdapter()
},
watch: {
isConnected: function() {
let that = this
if (!that.isConnected) {
that.handleBack()
that.isConnection = 2
}
},
isBluetoothTyle: function() {
let that = this
if (!that.isBluetoothTyle) {
that.handleBack()
that.isConnection = 2
}
},
stopblue: function() {
let that = this
if (!that.stopblue) {
clearTimeout(myTime);
that.isHeight = true
}
}
},
methods: {
// 重新连接
openBluetoothAdapter() {
let that = this
that.text = ""
that.stopblue = true
that.isHeight = false
uni.openBluetoothAdapter({
success: e => {
that.isConnection = 0
that.startBluetoothDeviceDiscovery()
},
fail: e => {
that.isConnection = 2
that.$tools.msg("请确定设备是开机状态、手机蓝牙权限已打开!")
}
});
},
// 开始搜寻附近的蓝牙外围设备
startBluetoothDeviceDiscovery() {
let that = this
uni.startBluetoothDevicesDiscovery({
allowDuplicatesKey: true,
success: res => {
that.onBluetoothDeviceFound();
},
fail: res => {
that.isConnection = 2
that.$tools.msg("请确定设备是开机状态、手机蓝牙权限已打开!")
}
});
},
/**
* 发现外围设备
*/
onBluetoothDeviceFound() {
var that = this;
uni.onBluetoothDeviceFound(res => {
res.devices.forEach(device => {
if (!device.name && !device.localName) {
return
}
if (device.name.indexOf("G02") !== -1) {
let value = that.$tools.ab2hex(device.advertisData, "")
let type = value.substring(22, 24)
let num = value.substring(28, 29)
let dw = value.substring(29, 30)
let data = parseInt(value.substring(24, 28), 16)
that.isConnection = 1
if (dw == "1") {
that.unit = "FT"
data = data * 2.54
}
if (num == "1") {
data = data / 10
}
if (num == "2") {
data = data / 100
}
if (num == "3") {
data = data / 1000
}
if (type == "01") {
clearTimeout(myTime);
that.text = "您的身高是:" + data + that.unit
let buffer = device.advertisData.slice(3, 9)
device.mac = new Uint8Array(buffer) // 保存广播数据中的mac地址这是由于iOS不直接返回mac地址
let tempMac = Array.from(device.mac)
tempMac.reverse()
device.macAddr = that.$tools.ab2hex(tempMac, ':').toUpperCase()
that.deviceId = device.deviceId
that.macAddr = device.macAddr
that.height = data
that.stopblue = false
return
}
return;
}
})
});
that.handleMyTime()
},
handleMyTime() {
var that = this;
myTime = setTimeout(function() {
if (!that.macAddr) {
clearTimeout(myTime);
that.text = ""
that.Unload = true
that.stopblue = true
that.isHeight = false
that.isConnection = 2
that.startBluetoothDeviceDiscovery()
that.closeBluetoothAdapter()
}
}, 30000);
},
/**
* 停止搜索蓝牙设备
*/
stopBluetoothDevicesDiscovery() {
uni.stopBluetoothDevicesDiscovery({
success: e => {
console.log("停止搜索蓝牙设备", e)
},
});
},
// 保存测量结果
handleGetMeasure() {
let that = this
if (!that.weight) {
this.$tools.msg("请输入体重")
return
}
that.$model.getmeasurefunit({
adc: 0,
weight: that.weight,
height: that.height,
aud_id: that.info.id
}).then(res => {
that.isHeight = false
if (res.code == 0) {
that.$store.dispatch('getUserInfo', {
aud_id: uni.getStorageSync('userid')
})
that.$store.dispatch("getResult", {
aud_id: uni.getStorageSync('userid')
})
that.$store.dispatch("GetBodyTrendList", {
aud_id: uni.getStorageSync('userid'),
s_time: that.startDate,
e_time: that.endDate
})
that.$tools.msg("测量成功")
} else {
that.$tools.msg("测量失败")
}
that.Unload = true
setTimeout(function() {
that.closeBluetoothAdapter()
uni.redirectTo({
url: "/pages/body/body?acd_id=2" + '&device=' + that.isdevice
})
}, 200)
})
},
//
handleBack(ind) {
let that = this
that.text = ""
that.Unload = true
that.stopBluetoothDevicesDiscovery()
that.closeBluetoothAdapter()
if (ind == 1) {
uni.redirectTo({
url: "/pages/body/body?acd_id=2" + '&device=' + that.isdevice
})
}
},
/**
* 断开蓝牙模块
*/
closeBluetoothAdapter() {
let that = this;
uni.closeBluetoothAdapter({
success: res => {
console.log('蓝牙模块关闭成功');
}
})
},
},
}
</script>
<style scoped lang="scss">
.image3 {
width: 200px !important;
height: 340px !important;
}
</style>