ReedawFoodApp/pageTwo/devices/search.vue

205 lines
5.5 KiB
Vue
Raw Normal View History

2025-05-30 11:14:38 +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)">
<image :src="item.pic"></image>
<text>{{item.name}}</text>
</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: [],
devList: [],
id: 0,
isdevice: true
}
},
computed: {
...mapState(["user", "isConnected", "isBluetoothTyle"]),
},
onLoad(options) {
that = this
that.id = options ? options.id : 0
that.isdevice = options.device
that.openBluetoothAdapter()
// 监听蓝牙连接状态
that.$Bluetooth.onBLEConnectionStateChange()
uni.onBluetoothAdapterStateChange(function(res) {
that.$store.commit("changeBluetooth", res.available);
})
},
onBackPress() {
let that = this
console.log("onUnload", that.Unload)
if (!that.Unload) {
that.$Bluetooth.closeBluetoothAdapter() // 断开蓝牙模块
that.$Bluetooth.stopBluetoothDevicesDiscovery() // 取消蓝牙搜索
}
},
watch: {
isBluetoothTyle: function() {
let that = this
if (!that.isBluetoothTyle) {
that.issearch = true
that.isdevTip = true
that.devList = []
clearTimeout(myTime);
}
console.log("蓝牙是否打开", that.isBluetoothTyle)
}
},
methods: {
// 初始化蓝牙
openBluetoothAdapter() {
let that = this
uni.openBluetoothAdapter({
success: e => {
console.log("蓝牙初始化成功")
that.issearch = false
that.isdevTip = false
that.devList = []
that.startBluetoothDeviceDiscovery()
},
fail: e => {
that.$Bluetooth.getBluetoothAdapter(e)
}
});
},
// 开始搜寻附近的蓝牙外围设备
startBluetoothDeviceDiscovery() {
let that = this
uni.startBluetoothDevicesDiscovery({
allowDuplicatesKey: true, //是否允许重复上报同一设备
success: res => {
that.onBluetoothDeviceFound();
},
fail: res => {}
});
},
/**
* 发现外围设备
*/
onBluetoothDeviceFound() {
var that = this;
uni.onBluetoothDeviceFound(res => {
res.devices.forEach(device => {
if (device.name.indexOf("G02") != -1) {
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)
tempMac.reverse()
device.macAddr = that.$tools.ab2hex(tempMac, ':').toUpperCase()
that.deviceId = device.macAddr
that.handleDevice(device)
return;
}
if (device.name.indexOf("Yihejia_Lung") != -1) {
console.log("肺活量PCV02", device)
clearTimeout(myTime);
device.macAddr = device.deviceId
that.deviceId = device.deviceId
that.handleDevice(device)
return;
}
if (device.name.indexOf("YPC") != -1) {
console.log("跳绳PCT02", device.name, device)
clearTimeout(myTime);
let buff = device.name.slice(7, 19)
device.macAddr = that.$tools.str2Num(buff)
device.deviceId = that.$tools.str2Num(buff)
that.deviceId = that.$tools.str2Num(buff)
if (device.macAddr != "") {
that.handleDevice(device)
}
return
}
})
});
that.handleMyTime()
},
handleDevice(device) {
let that = this
const foundDevices = that.devicesList
const idx = that.$tools.inArray(foundDevices, "deviceId", device.deviceId)
console.log("device", device, idx)
if (idx === -1) {
that.devicesList.push(device);
if (device.macAddr != "") {
that.handleDevType(device.macAddr)
}
}
},
handleMyTime() {
var that = this;
myTime = setTimeout(function() {
if (!that.devList.length) {
that.isdevTip = true
that.devList = []
}
that.issearch = true
clearTimeout(myTime);
that.$Bluetooth.stopBluetoothDevicesDiscovery()
}, 15000);
},
// 排查设备
handleDevType(sn) {
that = this
that.$model.getdevdetail({
mac: sn,
acd_id: that.id
}).then(res => {
console.log("排查返回", res, sn, that.id)
if (res.code == 0) {
res.data.deviceId = sn
that.devList.push(res.data);
}
})
},
handleWeight(item) {
let that = this
clearTimeout(myTime);
that.Unload = true
console.log("跳转测量", item)
if (item.bluetooth_type == '透传') {
that.$Bluetooth.stopBluetoothDevicesDiscovery()
}
uni.redirectTo({
url: item.page_measure + '?deviceId=' + that.deviceId + '&isdevice=' + that.isdevice
})
},
}
}
</script>
<style scoped lang="scss">
@import "./scss/search.scss";
</style>