123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233 |
- <script>
- import ble_store from "@/store/bluetooth";
- import Dialog from "../../../../static/vant/dialog/dialog";
- import Toast from "../../../../static/vant/toast/toast";
- import WechatLog from "@/utils/wechat_log";
- let $this;
- export default {
- name: "Connecting",
- props: ['status'],
- data() {
- return {
-
- device_status: 0,
-
- code: "AI00000000",
- bleFoundTimeOut: undefined
- }
- },
- created() {
- $this = this;
- },
- mounted() {
- $this.code = ble_store.getters.getDeviceSn();
- },
- watch: {
- 'status': {
- handler: function($status) {
- console.log("监听到父级扫码状态",$status);
- if ($status*1 === 1) {
- this.device_status = 1;
- this.getBluetoothState();
- }
- }, immediate: true
- }
- },
- methods:{
-
- setDeviceStatus($status = 0) {
- $this.device_status = $status;
- $this.$emit("deviceStatus", $status);
- },
-
- getBluetoothState() {
-
- wx.getBluetoothAdapterState({
- success: function (res) {
- if (res.available) {
-
- wx.startBluetoothDevicesDiscovery({
- allowDuplicatesKey: true,
- success: () => {
- $this.onBluetoothFound();
- },
- fail() {
-
- $this.setDeviceStatus(3);
- },
- });
- } else {
-
- console.log(res["errMsg"])
- setTimeout(() => {
- Toast.fail({
- message: res["errMsg"],
- });
- }, 3000);
- }
- },
- fail: function (err) {
-
- console.log(err["errMsg"])
- setTimeout(() => {
- Toast.fail({
- message: err["errMsg"],
- });
- }, 3000);
- }
- })
- },
-
- onBluetoothFound() {
- $this.bleFoundTimeOut = setTimeout(() => {
- if ($this.device_status === 0 || $this.device_status === 3) {
-
- wx.offBluetoothDeviceFound();
-
- wx.stopBluetoothDevicesDiscovery();
- Dialog.confirm({
- message: '脑机连接失败',
- showCancelButton: true,
- cancelButtonText: "查看指引",
- }).catch(() => {
- mpvue.navigateTo({
- url: "/pages/banner/guide/main"
- })
- });
- }
- }, 7000)
- try {
-
- wx.onBluetoothDeviceFound((res) => {
- $this.code = ble_store.getters.getDeviceSn();
- res.devices.forEach((device) => {
- if (!device.name && !device.localName) { return; }
- if (device.localName && device.localName !== "") {
- device.name = device.localName;
- }
- if (device["name"].toUpperCase() === $this.code) {
-
- wx.stopBluetoothDevicesDiscovery();
-
- $this.bLEConnection(device.deviceId);
-
- clearTimeout($this.bleFoundTimeOut);
- console.log("连接低功耗蓝牙设备:", JSON.stringify(device));
- }
- });
- });
- } catch (e) {
- console.log("打开蓝牙error", e);
- $this.setDeviceStatus(3);
- }
- },
-
- bLEConnection(deviceId) {
-
- wx.offBluetoothDeviceFound();
-
- wx.stopBluetoothDevicesDiscovery();
-
- wx.createBLEConnection({
- deviceId: deviceId,
- success: () => {
-
- wx.setBLEMTU({
- deviceId,
- mtu: 250,
- success(res) {
- console.log("设置mtu成功", JSON.stringify(res));
- }
- })
-
- ble_store.setters.setDeviceId(deviceId);
-
- $this.$connection.getBLEDeviceServices(deviceId);
-
- $this.setDeviceStatus(2);
-
- WechatLog.info("脑机蓝牙连接成功:", $this.code);
- },
- fail(err) {
-
- $this.setDeviceStatus(3);
- console.log(err);
- },
- });
- },
-
- cancelConnect() {
- clearTimeout($this.bleFoundTimeOut);
- $this.setDeviceStatus(5);
- },
- },
- }
- </script>
- <template>
- <div>
- <van-row class="padding">
- <van-col span="5" offset="4">
- <div class="device_bg flex flex-direction align-center justify-center">
- <img src="https://img.shuimuai.com/web/phone.png" alt="" class="device_phone">
- <text class="text-gray device_text"> 我的手机</text>
- </div>
- </van-col>
- <van-col span="3" offset="1">
- <div class="dot_container flex align-center">
- <div class="dot_wait">
- <img v-if="status*1 === 1" src="https://img.shuimuai.com/web/dot.png" alt="" class="moving_dot"
- :class="{moving: status*1 === 1}"
- />
- <img v-if="status*1 === 2" src="https://img.shuimuai.com/m_sign_gou%402x.png" alt="" class="moving_dot">
- <img v-if="status*1 === 3" src="https://img.shuimuai.com/fail.png" alt="" class="moving_dot">
- </div>
- </div>
- </van-col>
- <van-col span="5" offset="0">
- <div class="device_bg flex flex-direction align-center justify-center">
- <img src="https://img.shuimuai.com/web/brain.png" alt="" class="device_brain">
- <text class="text-gray device_text"> 水母智能脑机</text>
- </div>
- </van-col>
- </van-row>
- <van-row>
- <van-col span="9" offset="1">
- <button class="cu-btn bg-red lg text-white" @click="cancelConnect">
- <img src="https://img.shuimuai.com/m_duankainaohuan.png" class="cut_brain_icon" alt="">
- <text class="padding-lr cut_text">取消连接</text>
- </button>
- </van-col>
- <van-col span="12" offset="1">
- <text class="text-gray text-lg">
- <template v-if="status*1 === 1">
- 连接中...
- </template>
- <template v-if="status*1 === 2">
- 连接成功
- </template>
- <template v-if="status*1 === 3">
- 连接失败
- </template>
- </text>
- </van-col>
- </van-row>
- <van-toast id="van-toast"/>
- <van-dialog id="van-dialog"/>
- </div>
- </template>
- <style scoped></style>
|