adultDeviceApp/BLEPages/child/F01B.vue

446 lines
14 KiB
Vue
Raw Normal View History

2022-05-03 21:35:39 +08:00
<template>
2022-05-13 09:22:34 +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>
2022-05-27 17:20:31 +08:00
<view class="text">{{textW}}</view>
<view class="text">{{textH}}</view>
2022-05-13 09:22:34 +08:00
<view class="image">
<image src="/BLEPages/static/F018P01.gif" v-if="type==1"></image>
<image src="/BLEPages/static/F018P01.gif" v-if="type==2"></image>
</view>
<view class="tips">
<text>提示</text>
<text>1.请确定设备是开机状态</text>
<text>2.请确定手机蓝牙位置信息已打开</text>
<text>3.ios系统需打开设置>应用>微信里的蓝牙权限</text>
</view>
2022-05-03 21:35:39 +08:00
</view>
2022-05-13 09:22:34 +08:00
</view>
2022-05-03 21:35:39 +08:00
</template>
<script>
2022-05-13 09:22:34 +08:00
import {
mapState
} from "vuex";
var myTime;
export default {
data() {
return {
2022-05-27 17:20:31 +08:00
textW: "",
textH: "",
2022-05-13 09:22:34 +08:00
height: "",
weight: "",
imp: 0,
type: 1,
uuid1: "",
uuid2: "",
uuid3: "",
macAddr: "",
deviceId: "",
serviceId: "",
Unload: 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()
2022-05-27 17:20:31 +08:00
uni.switchTab({
url: "/pages/index/index"
})
2022-05-13 09:22:34 +08:00
console.log("返回上一个页面")
}
},
onLoad(options) {
let that = this
// 导航栏颜色
uni.setNavigationBarColor({
frontColor: '#ffffff',
backgroundColor: this.appTheme,
})
//
2022-05-27 17:20:31 +08:00
that.textW = ""
that.textH = ""
2022-05-13 09:22:34 +08:00
if (options && options.deviceId) {
that.deviceId = options.deviceId
2022-05-27 17:20:31 +08:00
that.closeBLEConnection()
that.closeBluetoothAdapter()
that.openBluetoothAdapter()
2022-05-13 09:22:34 +08:00
}
2022-06-11 16:21:11 +08:00
2022-05-13 09:22:34 +08:00
that.onBLEConnectionStateChange()
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
}
},
},
methods: {
// 初始化蓝牙
openBluetoothAdapter() {
let that = this
that.type = 1
2022-05-27 17:20:31 +08:00
that.textW = ""
that.textH = ""
2022-05-13 09:22:34 +08:00
uni.openBluetoothAdapter({
success: e => {
that.isConnection = 0
that.startBluetoothDeviceDiscovery()
},
fail: e => {
that.isConnection = 2
that.$tools.msg("请确定设备是开机状态、手机蓝牙权限已打开!")
}
});
},
// 开始搜寻附近的蓝牙外围设备
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
2022-05-03 21:35:39 +08:00
}
2023-09-05 16:54:26 +08:00
if (device.name.indexOf('AiLink_') != -1 || (device.localName && device.localName.indexOf('AiLink_') != -1)) {
2022-05-13 09:22:34 +08:00
clearTimeout(myTime);
let buff = device.advertisData.slice(-6)
device.mac = new Uint8Array(buff) // 保存广播数据中的mac地址这是由于iOS不直接返回mac地址
let tempMac = Array.from(device.mac)
tempMac.reverse()
device.macAddr = that.$tools.ab2hex(tempMac, ':').toUpperCase()
if (device.deviceId.indexOf(that.deviceId) != -1 || device.macAddr.indexOf(that.deviceId) != -1) {
that.stopBluetoothDevicesDiscovery()
that.macAddr = device.macAddr
that.deviceId = device.deviceId;
that.createBLEConnection()
return;
}
2022-05-03 21:35:39 +08:00
}
2022-05-13 09:22:34 +08:00
})
});
that.handleMyTime()
},
handleMyTime() {
var that = this;
myTime = setTimeout(function() {
if (!that.macAddr) {
clearTimeout(myTime);
that.Unload = true
that.isConnection = 2
that.closeBLEConnection()
that.closeBluetoothAdapter()
}
}, 20000);
},
/**
* 停止搜索蓝牙设备
*/
stopBluetoothDevicesDiscovery() {
uni.stopBluetoothDevicesDiscovery({
success: e => {
console.log("停止搜索蓝牙设备", e)
},
});
},
// 监听蓝牙连接状态
onBLEConnectionStateChange() {
let that = this
uni.onBLEConnectionStateChange(function(res) {
console.log("监听蓝牙连接状态", res.connected)
if (!res.connected) {
clearTimeout(myTime);
that.Unload = true
2022-05-27 17:20:31 +08:00
that.type = 1
2022-05-13 09:22:34 +08:00
that.isConnection = 2
that.closeBLEConnection()
that.closeBluetoothAdapter()
}
that.$store.commit("changeConnected", res.connected);
})
},
// 连接蓝牙
createBLEConnection() {
let that = this;
uni.createBLEConnection({
deviceId: that.deviceId,
success: 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.isConnection = 1
that.serviceId = service.uuid;
that.getBLEDeviceCharacteristics();
console.log("设备的FFE0的serviceId " + that.serviceId);
break;
}
2022-05-03 21:35:39 +08:00
}
2022-05-13 09:22:34 +08:00
},
fail: res => {
console.log('获取设备的UUID失败:', res)
}
});
},
/**
* 获取指定服务的特征值
*/
getBLEDeviceCharacteristics() {
let characteristicsList = [];
let that = this;
uni.getBLEDeviceCharacteristics({
deviceId: that.deviceId,
serviceId: that.serviceId,
success: res => {
console.log("服务的特征值成功", res)
for (let i = 0; i < res.characteristics.length; i++) {
let item = res.characteristics[i];
if (item.uuid.indexOf('0000FFE1') != -1) {
that.uuid1 = item.uuid //下发数据
} else if (item.uuid.indexOf('0000FFE2') != -1) {
that.uuid2 = item.uuid //监听数据
that.notifyBLECharacteristicValue()
} else if (item.uuid.indexOf('0000FFE3') != -1) {
that.uuid3 = item.uuid //写入设置
}
2022-05-03 21:35:39 +08:00
}
2022-05-13 09:22:34 +08:00
},
fail: res => {
console.log('获取特征值失败:', JSON.stringify(res))
}
})
},
/**
* 开启订阅特征值
* read: true, //读,write: true, //写,notify: true, //广播
*/
notifyBLECharacteristicValue() {
let that = this;
uni.notifyBLECharacteristicValueChange({
state: true, // 启用 notify 功能
deviceId: that.deviceId,
serviceId: that.serviceId,
characteristicId: that.uuid2,
success(res) {
uni.onBLECharacteristicValueChange(function(res) {
let value = that.$tools.ab2hex(res.value, "");
let num = value.substring(18, 19)
let dw = value.substring(19, 20)
let type = value.substring(8, 10)
let typeInfo = value.substring(10, 12)
if (type == "10") {
let data = parseInt(value.substring(13, 18), 16)
let dw1 = "kg"
let dw2 = "kg"
if (dw == "1") {
dw1 = "斤"
dw2 = "jin"
2022-05-03 21:35:39 +08:00
}
2022-05-13 09:22:34 +08:00
if (dw == "4") {
dw1 = "st:lb"
dw2 = "st"
data = 1 * data + 5
2022-05-03 21:35:39 +08:00
}
2022-05-13 09:22:34 +08:00
if (dw == "6") {
dw1 = "lb"
dw2 = "lb"
}
if (num == "1") {
data = parseInt(value.substring(13, 18), 16) / 10
}
if (num == "2") {
data = parseInt(value.substring(13, 18), 16) / 100
}
if (num == "3") {
data = parseInt(value.substring(13, 18), 16) / 1000
}
if (typeInfo == "01") {
2022-05-27 17:20:31 +08:00
that.textW = "您的实时体重是:" + data + dw1
2022-05-13 09:22:34 +08:00
}
if (typeInfo == "02") {
2022-05-27 17:20:31 +08:00
that.textW = "您的体重是:" + data + dw1
2022-05-13 09:22:34 +08:00
that.weight = data + dw2
console.log("稳定体重:", value, that.weight)
}
}
if (type == "11") {
that.type = 2
if (typeInfo == "03" || typeInfo == "04") {
that.imp = parseInt(value.substring(17, 22), 16)
}
console.log("体脂:", that.imp)
}
if (type == "14") { //身高模式
2022-05-27 17:20:31 +08:00
that.textH = "您的身高是:" + parseInt(value.substring(10, 14), 16) / 10
2022-05-13 09:22:34 +08:00
that.height = parseInt(value.substring(10, 14), 16) / 10
console.log("身高模式:", that.height)
}
if (type == "30") {
console.log("测量完成", that.weight, that.height, that.imp)
if (that.imp == 0) {
uni.showModal({
title: '提示',
content: "体脂测量失败,是否保存本次测量结果?",
cancelText: "放弃",
confirmText: "保存",
2022-05-03 21:35:39 +08:00
success(res) {
2022-05-13 09:22:34 +08:00
if (res.confirm) {
that.imp = 0
that.handleGetMeasure()
} else {
that.Unload = true
2022-05-03 21:35:39 +08:00
that.closeBLEConnection()
that.closeBluetoothAdapter()
2022-05-27 17:20:31 +08:00
uni.switchTab({
url: "/pages/index/index"
2022-05-03 21:35:39 +08:00
})
2022-05-13 09:22:34 +08:00
}
2022-05-03 21:35:39 +08:00
}
2022-05-13 09:22:34 +08:00
})
} else {
that.handleGetMeasure()
}
}
});
},
fail(res) {
console.log("测量失败", res.value);
}
});
},
// 保存测量结果
handleGetMeasure() {
let that = this
that.$model.getmeasure({
weight: that.weight,
imp: that.imp,
ecode: that.macAddr,
height: that.height ? that.height : that.info.height,
familyid: that.info.familyid,
}).then(res => {
if (res.code == 0) {
that.$tools.msg("测量成功")
that.$store.dispatch("getUserInfo", {
familyid: that.info.familyid,
});
that.$store.dispatch("getResult", {
birthday: that.info.birthday,
familyid: that.info.familyid,
height: that.height ? that.height : that.info.height,
sex: that.info.sex,
});
} 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.stopBluetoothDevicesDiscovery() //取消蓝牙搜索
that.closeBLEConnection()
that.closeBluetoothAdapter()
clearTimeout(myTime)
},
/**
* 断开蓝牙模块
*/
closeBluetoothAdapter() {
let that = this;
uni.closeBluetoothAdapter({
success: res => {
console.log('蓝牙模块关闭成功');
}
})
},
/**
* 断开蓝牙连接
*/
closeBLEConnection() {
var that = this;
uni.closeBLEConnection({
deviceId: that.deviceId,
success: res => {
console.log('断开蓝牙连接成功');
}
});
},
},
}
2022-05-03 21:35:39 +08:00
</script>
<style scoped lang="scss">
</style>