qq_41295110
2025-07-15 ba04983aaf5cbb0d57c4ec3530ee1663f0f83115
components/blueToothConnector/blueToothConnector.vue
@@ -50,13 +50,15 @@
      getAddress
   } from '../../utils/auth';
   import blueToothTool from '@/plugins/BluetoothTool.js';
import { nextTick } from "vue";
   export default {
      name: "blueToothPrinterConnector",
      data() {
         return {
            discoveredDevices: [], // 查询到的设备
            connectedDeviceId: "",
            readSuccess: false
            readSuccess: false,
            clock: null,
         };
      },
      methods: {
@@ -173,22 +175,28 @@
            });
         },
         async checkReadSuccess(count) {
            // 批量插入时,如果前一次发送成功,则清除前一次的时钟
            if (this.clock != null) {
               this.clock.stop();
               this.clock = null;
            }
            let that = this
            return new Promise((resolve, reject) => {
               let currentCount = 0;
               let clock = new CommonUtils.timeClock(() => {
               that.clock = new CommonUtils.timeClock(() => {
                  console.log(that.readSuccess)
                  currentCount++;
                  if (this.readSuccess == true) {
                     clock.stop();
                  if (that.readSuccess == true) {
                     that.clock.stop();
                     resolve(true);
                  } else if (currentCount >= count) {
                     clock.stop();
                     that.clock.stop();
                     reject(new Error('读取超时'));
                  }
               }, 2000);
               // 启动时钟
               clock.start();
               that.clock.start();
            });
         },
         async sendMessage(cmd) {
@@ -196,7 +204,7 @@
            uni.showLoading({
               title: "发送中..."
            })
            console.log("msg: " + cmd);
            // console.log("msg: " + cmd);
            let toast = (msg) => {
               uni.showToast({
                  icon: 'none',
@@ -207,23 +215,29 @@
                  uni.hideLoading()
               }, 3000)
            }
            const result = blueToothTool.sendByteData(cmd);
            if (!result) {
               return toast("发送失败,请重试...")
            }
            this.checkReadSuccess(5)
            .then((res) => {
               if(res === true) {
                  toast('发送成功!!!')
            let that = this
            return new Promise((resolve, reject) => {
               const result = blueToothTool.sendByteData(cmd);
               if (!result) {
                  toast("发送失败,请重试...")
                  reject(false)
               }
               that.checkReadSuccess(5)
                  .then((res) => {
                     if (res === true) {
                        toast('发送成功!!!')
                        resolve(true)
                     }
                  })
                  .catch((err) => {
                     toast('设备异常,请检查设备状态...')
                     reject(false)
                  })
                  .finally(() => {
                     that.readSuccess = false
                  })
            })
            .catch((err) => {
               toast('设备异常,请检查设备状态...')
            })
            .finally(() => {
               this.readSuccess = false
            })
         },
         uint8ArrayToSignedArray(uint8Array) {
            let signedArray = new Array(uint8Array.length);
@@ -256,10 +270,10 @@
               console.log("搜索完成");
            },
            readDataCallback: (dataByteArr) => {
               /* if(that.receiveDataArr.length >= 200) {
                  that.receiveDataArr = [];
               }
               that.receiveDataArr.push.apply(that.receiveDataArr, dataByteArr); */
               // if(that.receiveDataArr.length >= 200) {
               //    that.receiveDataArr = [];
               // }
               // that.receiveDataArr.push.apply(that.receiveDataArr, dataByteArr);
               this.readSuccess = true
               console.log("读取完成" + dataByteArr);
            },