examTeamApp/pages/devices/devices.vue

461 lines
8.9 KiB
Vue
Raw 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="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.img"></image> -->
<text>{{item.macAddr}}</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: []
}
},
computed: {
...mapState(["user", "isConnected", "isBluetoothTyle"]),
},
onLoad(options) {
that = this
that.openBluetoothAdapter()
that.onBLEConnectionStateChange()
uni.onBluetoothAdapterStateChange(function(res) {
that.$store.commit("changeBluetooth", res.available);
})
},
onUnload() {
console.log("onUnload")
let that = this
if (!that.Unload) {
that.stopBluetoothDevicesDiscovery() //取消蓝牙搜索
that.closeBLEConnection()
that.closeBluetoothAdapter()
}
},
watch: {
isBluetoothTyle: function() {
let that = this
if (!that.isBluetoothTyle) {
that.issearch = true
that.isdevTip = true
that.devList = []
clearTimeout(myTime);
that.closeBLEConnection()
that.closeBluetoothAdapter()
that.stopBluetoothDevicesDiscovery()
}
}
},
methods: {
// 初始化蓝牙
openBluetoothAdapter() {
let that = this
uni.openBluetoothAdapter({
success: e => {
console.log("蓝牙初始化成功")
that.issearch = false
that.isdevTip = false
that.devList = []
that.startBluetoothDeviceDiscovery()
},
fail: e => {}
});
},
// 开始搜寻附近的蓝牙外围设备
startBluetoothDeviceDiscovery() {
let that = this
uni.startBluetoothDevicesDiscovery({
allowDuplicatesKey: false, //是否允许重复上报同一设备
success: res => {
that.onBluetoothDeviceFound();
},
fail: res => {}
});
},
/**
* 发现外围设备
*/
onBluetoothDeviceFound() {
var that = this;
uni.onBluetoothDeviceFound(res => {
res.devices.forEach(device => {
if (device.name.indexOf("WSD") != -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)
device.macAddr = that.$tools.ab2hex(tempMac, ':').toUpperCase()
that.handleDevice(device)
return;
}
})
});
that.handleMyTime()
},
handleDevice(device) {
let that = this
const foundDevices = that.devicesList
const idx = that.$tools.inArray(foundDevices, "deviceId", device.deviceId)
that.deviceId = device.deviceId;
console.log("111", idx, device, )
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.closeBLEConnection()
that.closeBluetoothAdapter()
that.stopBluetoothDevicesDiscovery() //取消蓝牙搜索
}, 30000);
},
/**
* 停止搜索蓝牙设备
*/
stopBluetoothDevicesDiscovery() {
uni.stopBluetoothDevicesDiscovery({
success: e => {
console.log("停止搜索蓝牙设备", e)
},
});
},
// 排查设备
handleDevType(sn) {
that = this
that.$model.getdevdetail({
sn: sn,
}).then(res => {
console.log("排查返回", res)
if (res.code == 0) {
res.data.deviceId = sn
that.devList.push(res.data);
}
})
},
handleWeight(item) {
let that = this
that.Unload = true
// bletype 0未找到1透传2广播
clearTimeout(myTime);
console.log("跳转测量", item)
// if (item.bletype != 2) {
// that.stopBluetoothDevicesDiscovery()
// }
uni.redirectTo({
url: '/pages/devices/G02'
})
},
// 监听蓝牙连接状态
onBLEConnectionStateChange() {
let that = this
uni.onBLEConnectionStateChange(function(res) {
console.log("蓝牙连接状态", res.connected)
that.$store.commit("changeConnected", res.connected);
})
},
/**
* 断开蓝牙模块
*/
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">
.text {
position: absolute;
top: 0px;
width: 100%;
text-align: center;
height: 50px;
line-height: 50px;
font-size: 16px;
color: $textcolor;
font-weight: bold;
}
.tips {
position: absolute;
width: 100%;
bottom: 15px;
line-height: 24px;
view {
font-size: 14px;
color: $textcolor;
font-weight: bold;
margin-left: 15px;
}
text {
font-size: 12px;
width: 100%;
display: block;
margin-left: 20px;
color: #999;
}
}
.list {
position: absolute;
width: 100%;
display: flex;
align-items: center;
flex-wrap: wrap;
.item {
width: 30%;
display: flex;
flex-wrap: wrap;
align-items: center;
justify-content: center;
position: absolute;
left: 55%;
top: -10px;
image {
width: 45px;
height: 45px;
border-radius: 50%;
background-color: #fff;
}
text {
display: block;
width: 100%;
font-size: 12px;
color: #666;
margin-top: 5px;
text-align: center;
}
}
.item:nth-of-type(2) {
left: calc(55% - 60px);
top: 50px;
}
.item:nth-of-type(3) {
left: calc(50% - 140px);
top: 90px;
}
.item:nth-of-type(4) {
left: calc(61% + 20px);
top: 70px;
}
.item:nth-of-type(5) {
left: 20%;
top: -100px;
}
.item:nth-of-type(6) {
left: calc(20% - 16px);
top: -38px;
}
.item:nth-of-type(7) {
left: 0;
top: 15px;
}
.item:nth-of-type(8) {
left: calc(20% + 75px);
top: -150px;
}
.item:nth-of-type(9) {
left: calc(20% + 96px);
top: -80px;
}
.item:nth-of-type(10) {
left: 37px;
top: -170px;
}
.item:nth-of-type(11) {
left: calc(20% + 75px);
top: 130px;
}
.item:nth-of-type(12) {
left: -10px;
top: -104px;
}
.item:nth-of-type(13) {
left: calc(47% + 75px);
top: -150px;
}
.item:nth-of-type(14) {
left: calc(53% + 75px);
top: -50px;
}
}
.container {
width: 100%;
height: 100vh;
display: flex;
align-items: center;
justify-content: center;
position: relative;
overflow: hidden;
}
// 中心园
.container::after {
content: "";
width: 20px;
height: 20px;
border-radius: 50%;
background-color: #fbb780;
position: absolute;
z-index: 9;
}
/* 定义范围*/
.point-area {
text-align: center;
position: relative;
width: 400rpx;
height: 400rpx;
transition: opacity 0.5s ease-out;
}
.point-10,
.point-40,
.point-80,
.point-100,
.point-120 {
width: 100%;
height: 100%;
}
.point-10:after,
.point-40:after,
.point-80:after,
.point-100:after,
.point-120:after {
content: '';
display: block;
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
border-radius: 50%;
opacity: 0;
border: 1px solid #f7cb6b;
animation-play-state: paused;
-webkit-animation-play-state: paused;
}
.point-10:after {
content: '';
animation: ripple 3000ms linear 0ms infinite;
}
.point-40:after {
content: '';
animation: ripple 3000ms linear 600ms infinite;
}
.point-80:after {
content: '';
animation: ripple 3000ms linear 1200ms infinite;
}
.point-100:after {
content: '';
animation: ripple 3000ms linear 1800ms infinite;
}
.point-120:after {
content: '';
animation: ripple 3000ms linear 2400ms infinite;
}
@keyframes ripple {
0% {
opacity: 0;
transform: scale(0.1);
}
50% {
opacity: 0.8;
transform: scale(1);
}
100% {
opacity: 0.2;
transform: scale(2.2);
}
}
</style>