adultDeviceApp/BLEPages/child/B03B.vue

409 lines
11 KiB
Vue
Raw Normal View History

2023-09-05 16:54:26 +08:00
<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>
2023-09-08 14:56:12 +08:00
<view class="desc" v-if="status==1&&!isChild">
2023-09-05 16:54:26 +08:00
<view>大人先称重</view>
<text>体重稳定后再抱着宝宝称重</text>
</view>
2023-09-08 14:56:12 +08:00
<view class="desc" v-if="status==1&&isChild">
<view>抱上宝宝称重</view>
<text>完成后会自动计算出宝宝的体重</text>
</view>
2023-09-05 16:54:26 +08:00
<view class="text">{{text}}</view>
<view class="text" v-if="status==1">{{childWeight}}</view>
<view class="image">
2023-09-08 14:56:12 +08:00
<image src="/BLEPages/static/B03B02.gif" class="image3" v-if="status!=-1"></image>
<image src="/BLEPages/static/B03B03.gif" class="image3" v-if="isChild"></image>
2023-09-05 16:54:26 +08:00
</view>
<view class="tips">
<text>提示</text>
<text>1.请确定设备是开机状态</text>
<text>2.请确定手机蓝牙位置信息已打开</text>
<text>3.ios系统需打开设置>应用>微信里的蓝牙权限</text>
</view>
</view>
<view class="wrapper" v-if="isHeight">
<view class="bg"></view>
<view class="Blue">
<view class="h4">测量结果提示</view>
<view class="Blue-box">
{{status==1?'宝宝的':'您的'}}体重为<text>{{weight}}{{unit=='jin'?'斤':unit}}</text>
</view>
<view class="Blue-box">
身高为<input v-model="height" type="digit" placeholder="请输入" style="width: 60%;" />cm
</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
let realTimeA = 0
let realTimeC = 0
export default {
data() {
return {
text: "",
weight: "",
unit: "kg",
height: "",
childWeight: "",
2023-09-08 14:56:12 +08:00
isChild: false,
2023-09-05 16:54:26 +08:00
status: -1, // 0大人1婴儿
imp: 0,
macAddr: "",
deviceId: "",
serviceId: "",
Unload: false,
stopblue: true,
isHeight: false,
isConnection: 0, //是否连接成功
}
},
computed: {
...mapState(["user", "isConnected", "isBluetoothTyle", "appTheme"]),
info() {
return this.user
}
},
onUnload: function() {
let that = this
if (!that.Unload) {
that.stopBluetoothDevicesDiscovery() //取消蓝牙搜索
clearTimeout(myTime);
that.closeBLEConnection()
that.closeBluetoothAdapter()
console.log("测量页返回1")
}
},
onLoad(options) {
let that = this
that.text = ""
that.height = that.info.height
if (options && options.deviceId) {
that.deviceId = options.deviceId
that.openBluetoothAdapter()
}
// 导航栏颜色
uni.setNavigationBarColor({
frontColor: '#ffffff',
backgroundColor: this.appTheme,
})
uni.onBluetoothAdapterStateChange(function(res) {
that.$store.commit("changeBluetooth", res.available);
})
},
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) {
that.isHeight = true
}
}
},
methods: {
// 初始化蓝牙
openBluetoothAdapter() {
let that = this
2023-09-08 14:56:12 +08:00
that.text = ""
2023-09-05 16:54:26 +08:00
realTimeA = 0
realTimeC = 0
that.childWeight = ""
2023-09-08 14:56:12 +08:00
that.isChild = false
2023-09-05 16:54:26 +08:00
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,
2023-09-08 14:56:12 +08:00
// services: [
// "F0A0",
// ],
2023-09-05 16:54:26 +08:00
success: res => {
that.isConnection = 0
that.onBluetoothDeviceFound();
},
fail: res => {
that.isConnection = 2
that.$tools.msg("请确定设备是开机状态、手机蓝牙权限已打开!")
}
});
},
// 监听蓝牙连接状态
onBLEConnectionStateChange() {
let that = this
uni.onBLEConnectionStateChange(function(res) {
console.log("监听蓝牙连接状态", res.connected)
if (!res.connected) {
clearTimeout(myTime);
that.Unload = true
2023-09-08 14:56:12 +08:00
that.text = ""
2023-09-05 16:54:26 +08:00
realTimeA = 0
realTimeC = 0
that.childWeight = ""
2023-09-08 14:56:12 +08:00
that.isChild = false
2023-09-05 16:54:26 +08:00
that.stopblue = true
that.isHeight = false
that.isConnection = 2
that.stopBluetoothDevicesDiscovery()
that.closeBLEConnection()
that.closeBluetoothAdapter()
}
that.$store.commit("changeConnected", res.connected);
})
},
/**
* 停止搜索蓝牙设备
*/
stopBluetoothDevicesDiscovery() {
uni.stopBluetoothDevicesDiscovery({
success: e => {
console.log("停止搜索蓝牙设备", e)
},
});
},
/**
* 发现外围设备
*/
onBluetoothDeviceFound() {
var that = this;
function PrefixZero(num, n) {
return (Array(n).join(0) + num).slice(-n);
}
uni.onBluetoothDeviceFound(res => {
res.devices.forEach(device => {
device.advertisData = device.advertisData ? device.advertisData : ''
//
if (!device.name && !device.localName) {
let value = that.$tools.ab2hex(device.advertisData, "")
2023-09-08 14:56:12 +08:00
let id = value.substring(12, 16)
if (value.indexOf('c0') !== -1 && id == '0a11') {
2023-09-05 16:54:26 +08:00
clearTimeout(myTime);
let buff = device.advertisData.slice(-6)
device.mac = new Uint8Array(buff) // 保存广播数据中的mac地址这是由于iOS不直接返回mac地址
let tempMac = Array.from(device.mac)
device.macAddr = that.$tools.ab2hex(tempMac, ':').toUpperCase()
2023-09-08 14:56:12 +08:00
2023-09-05 16:54:26 +08:00
if (device.deviceId.indexOf(that.deviceId) !== -1 || device.macAddr
.indexOf(that.deviceId) !== -1) {
that.isConnection = 1
let msg = parseInt(value.substring(16, 18), 16).toString(2)
let weight = parseInt(value.substring(4, 8), 16)
let type = PrefixZero(msg, 8).substring(7, 8) //0实时,1稳定
let num = PrefixZero(msg, 8).substring(5, 7) //小数点
let unit = PrefixZero(msg, 8).substring(3, 5) //单位
that.status = PrefixZero(msg, 8).substring(2, 3) //设备类型
let dw = 'kg'
console.log("value", that.status, weight)
if (unit == "10") {
that.unit = "lb"
dw = 'lb'
}
if (unit == "01") {
that.unit = "jin"
dw = "斤"
}
if (num == "00") {
weight = parseInt(value.substring(4, 8), 16) / 10
}
if (num == "10") {
if (unit == "10") {
weight = parseInt(value.substring(4, 8), 16) / 10
} else {
weight = parseInt(value.substring(4, 8), 16) / 100
}
}
if (type == 0) {
if (realTimeA == 0) {
that.text = "您的实时体重是:" + weight + dw
} else {
realTimeC++
that.childWeight = "宝宝的实时体重是:" + weight + dw
}
console.log("实时realTime", realTimeA)
}
if (that.status == 1) {
//抱婴模式
if (type == 1) {
realTimeA++
if (realTimeC == 0) {
that.text = "您的稳定体重是:" + weight + dw
2023-09-08 14:56:12 +08:00
that.isChild = true
2023-09-05 16:54:26 +08:00
} else {
that.childWeight = "宝宝的稳定体重是:" + weight + dw
that.macAddr = device.macAddr
that.deviceId = device.deviceId;
that.weight = weight
that.stopblue = false
that.stopBluetoothDevicesDiscovery()
}
console.log("稳定realTime", realTimeA, realTimeC)
}
} else {
// 身高体重模式
if (type == 1) {
that.text = "您的稳定体重是:" + weight + dw
that.macAddr = device.macAddr
that.deviceId = device.deviceId;
that.weight = weight
that.stopblue = false
that.stopBluetoothDevicesDiscovery()
console.log("测量完成", that.weight, that.unit)
}
}
2023-09-08 14:56:12 +08:00
return
2023-09-05 16:54:26 +08:00
}
return
}
}
})
});
that.handleMyTime()
},
handleMyTime() {
var that = this;
myTime = setTimeout(function() {
if (!that.macAddr) {
clearTimeout(myTime);
that.Unload = true
that.isConnection = 2
that.startBluetoothDeviceDiscovery()
that.closeBLEConnection()
that.closeBluetoothAdapter()
}
}, 30000);
},
// 保存测量结果
handleGetMeasure() {
let that = this
that.$model.getmeasurefunit({
weight: that.weight + that.unit,
imp: 0,
ecode: that.macAddr,
height: that.height,
familyid: that.info.familyid,
}).then(res => {
that.isHeight = false
if (res.code == 0) {
that.$tools.msg("测量成功")
that.$store.dispatch("getUserInfo", {
familyid: that.info.familyid,
})
that.$store.dispatch("getResult", {
2023-09-08 14:56:12 +08:00
birthday: that.info.birthday,
familyid: that.info.familyid,
height: that.height,
sex: that.info.sex,
2023-09-05 16:54:26 +08:00
});
} else {
console.log("测量失败", res.message)
that.$tools.msg(res.message)
}
that.Unload = true
setTimeout(function() {
that.closeBLEConnection()
that.closeBluetoothAdapter()
uni.switchTab({
url: "/pages/index/index"
})
}, 200)
})
},
handleBack(ind) {
let that = this
that.Unload = true
that.isHeight = false
clearTimeout(myTime)
that.stopBluetoothDevicesDiscovery() //取消蓝牙搜索
that.closeBLEConnection()
that.closeBluetoothAdapter()
if (ind == 1) {
uni.switchTab({
url: "/pages/index/index"
})
}
},
/**
* 断开蓝牙模块
*/
closeBluetoothAdapter() {
let that = this;
uni.closeBluetoothAdapter({
success: res => {
console.log('蓝牙模块关闭成功');
}
})
},
/**
* 断开蓝牙连接
*/
closeBLEConnection() {
var that = this;
uni.closeBLEConnection({
deviceId: that.deviceId,
success: res => {
console.log('断开蓝牙连接成功');
}
});
},
},
}
</script>
<style scoped lang="scss">
.image3 {
width: 200px !important;
height: 340px !important;
}
.desc {
width: 100%;
text-align: center;
font-size: 18px;
color: $uni-color-error;
text {
font-size: 14px;
}
}
</style>