chenhaozhe
2025-08-06 00a363f1ef1e2ded9f7a8e933cd89d863458a3b4
components/blueToothConnector/blueToothConnector.vue
@@ -23,7 +23,6 @@
            <uni-card :title="device.name" :extra="connectedDeviceId === device.address?'已连接':'未连接'"
               style="margin: 10px;">
               <view class="operation-zone">
                  <view></view>
                  <button class="op1" size="mini" plain @click="connectBT(device)">连接设备</button>
               </view>
            </uni-card>
@@ -51,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: {
@@ -95,12 +96,8 @@
               },
               fail: async (e) => {
                  console.error(e)
                  switch (e.code) {
                     case "10009":
                        this.showToast("此设备不支持设备搜索功能!");
                        break;
                     default:
                        console.error(e);
                  if(e.errCode == 10009) {
                     this.showToast('此设备不支持设备搜索功能!');
                  }
               }
            })
@@ -174,22 +171,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) {
@@ -197,7 +200,7 @@
            uni.showLoading({
               title: "发送中..."
            })
            console.log("msg: " + cmd);
            // console.log("msg: " + cmd);
            let toast = (msg) => {
               uni.showToast({
                  icon: 'none',
@@ -208,23 +211,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);
@@ -257,10 +266,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);
            },
@@ -315,17 +324,9 @@
   .operation-zone {
      display: flex;
      justify-content: flex-end;
      padding: 10rpx;
      >view {
         flex: 1;
      }
      >button{
         display: inline-block;
         width: 6rem;
      }
      justify-content: space-around;
      margin-top: 10rpx;
      .op1 {
         border: 1px solid #41a863;
         color: #41a863;