examTeamApp/pageTwo/business/search.vue

296 lines
7.0 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="tips">{{$t("linkBluetooth.onBluetoothTips")}}</view>
<view class="item" @click="openBluetoothAdapter">{{$t("linkBluetooth.startSearchBluetooth")}}</view>
<view class="devices_summary">{{$t("linkBluetooth.onBluetoothHaveFound")}}
{{devices.length}}{{$t("linkBluetooth.onBluetoothDevice")}}
</view>
<view>
<scroll-view class="device_list" scroll-y scroll-with-animation v-if="popup">
<view v-for="(item,index) in devices" :key="index" @tap="createBLEConnection(item)" class="device_item">
<view>
<text>{{item.localName ||item.name}}</text>
</view>
<view>mac地址{{$t("linkBluetooth.onBluetoothMac")}}:{{item.macAddr || item.deviceId}}</view>
</view>
</scroll-view>
</view>
<view class="tishi">
<view class="text">
<icon class="t-icon t-icon-tishi"></icon> {{$t("linkBluetooth.onBluetoothBind")}}
</view>
<view class="dv">
<text class="size14">1、{{$t("linkBluetooth.openDeviceeMeasureTips")}}</text>
<text class="size14">2、{{$t("linkBluetooth.onDeviceMeasureTips")}}</text>
<text class="size14">3、{{$t("linkBluetooth.onBluetoothSelect")}}</text>
</view>
</view>
</view>
</template>
<script>
let that;
let myTime;
import {
mapState
} from "vuex";
export default {
data() {
return {
macAddr: "",
code: "",
deviceId: "",
popup: false,
devices: [],
id: 0
}
},
computed: {
...mapState(["user", "isConnected", "isBluetoothTyle"]),
},
onLoad(options) {
that = this
that.id = options.id
that.$Bluetooth.onBLEConnectionStateChange()
uni.onBluetoothAdapterStateChange(function(res) {
that.$store.commit("changeBluetooth", res.available);
})
},
onUnload() {
console.log("onUnload")
let that = this
if (!that.Unload) {
uni.hideLoading()
that.$Bluetooth.closeBluetoothAdapter() // 断开蓝牙模块
that.$Bluetooth.stopBluetoothDevicesDiscovery() // 取消蓝牙搜索
}
},
methods: {
// 初始化蓝牙
openBluetoothAdapter() {
let that = this
uni.openBluetoothAdapter({
success: e => {
console.log("蓝牙初始化成功")
that.startBluetoothDeviceDiscovery()
},
fail: e => {
console.log('初始化蓝牙失败:' + e.errMsg);
that.$Bluetooth.getBluetoothAdapter(e)
}
});
},
// 开始搜寻附近的蓝牙外围设备
startBluetoothDeviceDiscovery() {
let that = this
uni.startBluetoothDevicesDiscovery({
allowDuplicatesKey: true, //是否允许重复上报同一设备
success: res => {
that.onBluetoothDeviceFound();
},
fail: res => {}
});
},
/**
* 发现外围设备
*/
onBluetoothDeviceFound() {
var that = this;
const foundDevices = []
wx.showLoading({
title: that.$t("linkBluetooth.searchBluetooth"),
})
uni.onBluetoothDeviceFound(res => {
res.devices.forEach(device => {
if (device.name.indexOf("YPC") != -1) {
clearTimeout(myTime);
let buff = device.name.slice(7, 19)
device.macAddr = that.$Bluetooth.str2Num(buff)
device.deviceId = that.$Bluetooth.str2Num(buff)
that.handleDevice(device)
return
}
if (device.name.indexOf("G02") != -1) {
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.handleDevice(device)
return
}
if (device.name.indexOf("Yihejia_Lung") != -1) {
console.log("肺活量", device, '04:0D:84:48:E0:9B')
device.macAddr = device.deviceId
clearTimeout(myTime);
that.handleDevice(device)
return
}
})
});
that.handleMyTime()
},
handleDevice(device) {
let that = this
const foundDevices = that.devices
const idx = that.$tools.inArray(foundDevices, "deviceId", device.deviceId)
that.deviceId = device.deviceId;
if (idx === -1) {
that.devices.push(device);
} else {
that.devices[idx] = device
}
that.popup = true
},
handleMyTime() {
var that = this;
myTime = setTimeout(function() {
if (!that.devices.length) {
that.islink = -1
that.$tools.showModal(that.$t("linkBluetooth.Nodevicefound"))
}
uni.hideLoading()
clearTimeout(myTime);
that.$Bluetooth.stopBluetoothDevicesDiscovery() //取消蓝牙搜索
}, 15000);
},
// 连接蓝牙
createBLEConnection(e) {
let that = this;
that.$Bluetooth.stopBluetoothDevicesDiscovery()
that.macAddr = e.macAddr
uni.showModal({
title: that.$t("tips.msgTitle"),
content: that.$t("tips.verifybindt"),
cancelText: that.$t("tips.btnSancellation"),
confirmText: that.$t("tips.btnConfirm"),
success: (res) => {
if (res.confirm) {
that.getActive()
} else {
that.$tools.msg(that.$t("tips.msgCancel"))
}
}
})
},
getActive() {
let that = this
that.$model.getBinding({
device_id: that.id,
device_mac: that.macAddr
}).then(res => {
that.$Bluetooth.closeBluetoothAdapter()
that.devices = []
if (res.code == 0) {
that.$tools.msg(that.$t("tips.msgbind"))
that.$store.dispatch('getUserDeviceList')
that.$store.dispatch('getUserInfo', {
aud_id: uni.getStorageSync('userid')
})
setTimeout(function() {
uni.switchTab({
url: "/pages/home/home"
})
}, 500)
} else {
that.$tools.msg(res.msg)
}
})
},
}
}
</script>
<style scoped lang="scss">
.content {
min-height: calc(100vh - 66px);
padding: 0;
border-top: 66px solid #F9FAFC;
background-color: #fff;
}
.tishi {
width: 100%;
font-size: 28rpx;
line-height: 25px;
font-weight: bold;
position: absolute;
bottom: 20px;
padding-left: 15px;
.text {
display: flex;
align-items: center;
icon {
margin-right: 5px;
}
}
text {
font-weight: 500;
font-size: 32rpx;
color: #999;
width: 100%;
display: block;
}
}
.item {
width: 70%;
height: 40px;
line-height: 38px;
text-align: center;
background: #f7f7f7;
border: 1px solid #dfdfdf;
font-weight: bold;
margin: auto;
border-radius: 15px;
margin-top: 15px;
}
.devices_summary {
width: 100%;
height: 40px;
line-height: 40px;
text-align: center;
color: #666;
}
.device_list {
flex: 1;
width: 100%;
height: auto;
max-height: 340px;
margin-top: 0;
margin-bottom: 10px;
position: absolute;
bottom: 160px;
top: 170px;
.device_item {
font-size: 32rpx;
padding: 7px 10px;
color: #999;
border-bottom: 1px solid #dfdfdf;
text {
display: inline-block;
font-size: 28rpx;
font-weight: bold;
color: #666;
margin-bottom: 5px;
}
}
}
.tips {
font-size: 28rpx;
text-align: center;
color: #e83a1e;
background: #f7e4c8;
padding: 5px 0;
margin-top: 15px;
}
</style>