examTeamApp/pageTwo/devices/search.vue

205 lines
5.5 KiB
Vue
Raw Normal View History

2024-06-13 18:03:50 +08:00
<template>
<view class="container">
<view class="text" @click="openBluetoothAdapter" v-if="issearch">没有搜到想要的点击重新搜索</view>
<view class="point-area">
<view class="point point-10"></view>
<view class="point point-40"></view>
<view class="point point-80"></view>
<view class="point point-100"></view>
<view class="point point-120"></view>
</view>
<view class="list">
<view class="item" v-for="(item,index) in devList" :key="index" @click="handleWeight(item)">
2024-07-08 10:50:07 +08:00
<image :src="item.pic"></image>
<text>{{item.name}}</text>
2024-06-13 18:03:50 +08:00
</view>
</view>
<view class="tips" v-if="isdevTip">
<view>提示</view>
<text>1.请确定设备已绑定</text>
<text>2.请确定设备已开机</text>
<text>3.请确定手机蓝牙及位置信息已打开</text>
</view>
</view>
</template>
<script>
let that;
let myTime;
import {
mapState
} from "vuex";
export default {
data() {
return {
Unload: false, //是否返回上一页
issearch: false, //是否重新搜索
isdevTip: false, //是否有提示
devicesList: [],
2024-07-08 10:50:07 +08:00
devList: [],
id: 0,
isdevice: true
2024-06-13 18:03:50 +08:00
}
},
computed: {
...mapState(["user", "isConnected", "isBluetoothTyle"]),
},
onLoad(options) {
that = this
2024-07-08 10:50:07 +08:00
that.id = options ? options.id : 0
that.isdevice = options.device
2024-06-13 18:03:50 +08:00
that.openBluetoothAdapter()
2024-07-22 14:13:19 +08:00
// 监听蓝牙连接状态
that.$Bluetooth.onBLEConnectionStateChange()
2024-06-13 18:03:50 +08:00
uni.onBluetoothAdapterStateChange(function(res) {
that.$store.commit("changeBluetooth", res.available);
})
},
2024-07-25 16:55:39 +08:00
onBackPress() {
2024-06-13 18:03:50 +08:00
let that = this
2024-07-22 14:13:19 +08:00
console.log("onUnload", that.Unload)
2024-06-13 18:03:50 +08:00
if (!that.Unload) {
2024-07-22 14:13:19 +08:00
that.$Bluetooth.closeBluetoothAdapter() // 断开蓝牙模块
that.$Bluetooth.stopBluetoothDevicesDiscovery() // 取消蓝牙搜索
2024-06-13 18:03:50 +08:00
}
},
watch: {
isBluetoothTyle: function() {
let that = this
if (!that.isBluetoothTyle) {
that.issearch = true
that.isdevTip = true
that.devList = []
clearTimeout(myTime);
}
2024-07-22 14:13:19 +08:00
console.log("蓝牙是否打开", that.isBluetoothTyle)
2024-06-13 18:03:50 +08:00
}
},
methods: {
// 初始化蓝牙
openBluetoothAdapter() {
let that = this
uni.openBluetoothAdapter({
success: e => {
console.log("蓝牙初始化成功")
that.issearch = false
that.isdevTip = false
that.devList = []
that.startBluetoothDeviceDiscovery()
},
2024-07-22 14:13:19 +08:00
fail: e => {
that.$Bluetooth.getBluetoothAdapter(e)
}
2024-06-13 18:03:50 +08:00
});
},
// 开始搜寻附近的蓝牙外围设备
startBluetoothDeviceDiscovery() {
let that = this
uni.startBluetoothDevicesDiscovery({
2024-07-08 10:50:07 +08:00
allowDuplicatesKey: true, //是否允许重复上报同一设备
2024-06-13 18:03:50 +08:00
success: res => {
that.onBluetoothDeviceFound();
},
fail: res => {}
});
},
/**
* 发现外围设备
*/
onBluetoothDeviceFound() {
var that = this;
uni.onBluetoothDeviceFound(res => {
res.devices.forEach(device => {
2024-07-08 10:50:07 +08:00
if (device.name.indexOf("G02") != -1) {
2024-06-13 18:03:50 +08:00
console.log("G02", device)
clearTimeout(myTime);
let buff = device.advertisData.slice(3, 9)
device.mac = new Uint8Array(buff) // 保存广播数据中的mac地址这是由于iOS不直接返回mac地址
let tempMac = Array.from(device.mac)
2024-07-22 14:13:19 +08:00
tempMac.reverse()
2024-06-13 18:03:50 +08:00
device.macAddr = that.$tools.ab2hex(tempMac, ':').toUpperCase()
2024-07-22 14:13:19 +08:00
that.deviceId = device.macAddr
2024-06-13 18:03:50 +08:00
that.handleDevice(device)
return;
}
2024-07-22 14:13:19 +08:00
if (device.name.indexOf("Yihejia_Lung") != -1) {
console.log("肺活量PCV02", device)
2024-07-08 10:50:07 +08:00
clearTimeout(myTime);
2024-07-22 14:13:19 +08:00
device.macAddr = device.deviceId
that.deviceId = device.deviceId
2024-07-08 10:50:07 +08:00
that.handleDevice(device)
return;
}
if (device.name.indexOf("YPC") != -1) {
2024-07-22 14:13:19 +08:00
console.log("跳绳PCT02", device.name, device)
2024-07-08 10:50:07 +08:00
clearTimeout(myTime);
let buff = device.name.slice(7, 19)
device.macAddr = that.$tools.str2Num(buff)
2024-07-22 14:13:19 +08:00
device.deviceId = that.$tools.str2Num(buff)
that.deviceId = that.$tools.str2Num(buff)
2024-07-08 10:50:07 +08:00
if (device.macAddr != "") {
that.handleDevice(device)
}
return
}
2024-06-13 18:03:50 +08:00
})
});
that.handleMyTime()
},
handleDevice(device) {
let that = this
const foundDevices = that.devicesList
const idx = that.$tools.inArray(foundDevices, "deviceId", device.deviceId)
2024-07-22 14:13:19 +08:00
console.log("device", device, idx)
2024-06-13 18:03:50 +08:00
if (idx === -1) {
that.devicesList.push(device);
2024-07-08 10:50:07 +08:00
if (device.macAddr != "") {
2024-09-12 11:20:52 +08:00
that.handleDevType(device.macAddr)
2024-07-08 10:50:07 +08:00
}
2024-06-13 18:03:50 +08:00
}
},
handleMyTime() {
var that = this;
myTime = setTimeout(function() {
if (!that.devList.length) {
that.isdevTip = true
that.devList = []
}
that.issearch = true
clearTimeout(myTime);
2024-07-22 14:13:19 +08:00
that.$Bluetooth.stopBluetoothDevicesDiscovery()
}, 15000);
2024-06-13 18:03:50 +08:00
},
// 排查设备
handleDevType(sn) {
that = this
that.$model.getdevdetail({
2024-07-08 10:50:07 +08:00
mac: sn,
acd_id: that.id
2024-06-13 18:03:50 +08:00
}).then(res => {
2024-07-22 14:13:19 +08:00
console.log("排查返回", res, sn, that.id)
2024-06-13 18:03:50 +08:00
if (res.code == 0) {
res.data.deviceId = sn
that.devList.push(res.data);
}
})
},
handleWeight(item) {
let that = this
clearTimeout(myTime);
2024-07-08 10:50:07 +08:00
that.Unload = true
2024-06-13 18:03:50 +08:00
console.log("跳转测量", item)
2024-07-08 10:50:07 +08:00
if (item.bluetooth_type == '透传') {
2024-07-22 14:13:19 +08:00
that.$Bluetooth.stopBluetoothDevicesDiscovery()
2024-07-08 10:50:07 +08:00
}
2024-06-13 18:03:50 +08:00
uni.redirectTo({
2024-09-12 11:20:52 +08:00
url: item.page_measure + '?deviceId=' + that.deviceId + '&isdevice=' + that.isdevice
2024-06-13 18:03:50 +08:00
})
},
}
}
</script>
<style scoped lang="scss">
@import "./scss/search.scss";
2024-06-13 18:03:50 +08:00
</style>