没错就是我哎呀
没错就是我哎呀
  • 发布:2023-01-16 13:36
  • 更新:2023-07-14 13:26
  • 阅读:1846

蓝牙连接便携式打印机(芝柯)

分类:Native.js

vue

<template>  
  <view>  
    <button @click="searchBle">搜索蓝牙</button>  
    <view style="margin-top: 30upx" :key="index" v-for="(item, index) in devices">  
      <button style="width: 400upx; color: #0081ff" @click="onConn(item)">  
        {{ item.name }}  
      </button>  
    </view>  
    <button style="margin-top: 100upx" @click="senBleLabel()">标签打印</button>  
  </view>  
</template>  

js


<script>  
import tsc from "@/static/libs/tsc.js";  
import bluetoothTool from "@/static/libs/BluetoothTool.js";  

export default {  
  data() {  
    return {  
      devices: [],  
      currDev: null,  
      connId: "",  
      piaojuText: "",  

      tableDomId: "",  
      tableImgPath: "",  

      canvasWidth: 80,  
      canvasHeight: 60,  

      msg: "",  
      count: 0,  
    };  
  },  
  watch: {  
    msg() {  
      uni.showToast({  
        title: this.msg,  
      });  
    },  
  },  
  onReady() {  
    this.renderCanvas();  
  },  
  mounted() {  
    //#ifdef APP-PLUS  
    // 蓝牙  
    bluetoothTool.init({  
      listenBTStatusCallback: (state) => {  
        if (state == "STATE_ON") {  
          let lastBleAddress = uni.getStorageSync("lastBleAddress");  
          if (lastBleAddress) {  
            uni.showLoading({  
              title: "正在连接...",  
            });  
            console.log(lastBleAddress);  
            bluetoothTool.connDevice(lastBleAddress, (result) => {  
              uni.hideLoading();  
              uni.showToast({  
                title: result ? "连接成功!" : "连接失败...",  
              });  
              let _this = this;  

              let countSet = setInterval(() => {  
                if (_this.count == 5) {  
                  clearInterval(countSet);  
                } else {  
                  _this.count  ;  
                  _this.senBleLabel();  
                }  
              }, 2000);  
            });  
          }  
        }  
      },  
      discoveryDeviceCallback: this.onDevice,  
      discoveryFinishedCallback: function () {  
        that.msg = "搜索完成";  
      },  
      readDataCallback: function (dataByteArr) {  
        // 读取蓝牙返回的数据  
        /* if(that.receiveDataArr.length >= 200) {  
                            that.receiveDataArr = [];  
                        }  
                        that.receiveDataArr.push.apply(that.receiveDataArr, dataByteArr); */  
      },  
      connExceptionCallback: function (e) {  
        console.log(e);  
        that.msg = "设备连接失败";  
      },  
    });  
    //#endif  
  },  
  methods: {  
    destroyed: function () {  
      console.log("destroyed----------");  
      if (this.connId != "") {  
        uni.closeBLEConnection({  
          deviceId: this.connId,  
          success(res) {  
            console.log(res);  
          },  
        });  
      }  
    },  
    searchBle() {  
      var that = this;  
      console.log("initBule");  
      // 使用openBluetoothAdapter 接口,免去主动申请权限的麻烦  
      uni.openBluetoothAdapter({  
        success(res) {  
          this.devices = [];  
          console.log("打开 蓝牙模块,开始搜索模式...");  
          console.log(res);  
          bluetoothTool.discoveryNewDevice();  
          //that.onDevice()  
        },  
      });  
    },  
    onDevice(newDevice) {  
      console.log("监听寻找到新设备的事件---------------");  
      console.log(newDevice);  
      if (newDevice.name && newDevice.name != "null") {  
        this.devices.push({  
          name: newDevice.name,  
          address: newDevice.address,  
        });  
      }  
    },  
 stopFindBule() {  
      console.log("停止搜寻附近的蓝牙外围设备---------------");  
      uni.stopBluetoothDevicesDiscovery({  
        success(res) {  
          console.log(res);  
        },  
      });  
    },  
    onConn(item) {  
      console.log("连接蓝牙---------------" + item.address);  

      if (this.selectDevices.includes(item.address)) {  
        that.msg = "该蓝牙已连接";  
      }  
      bluetoothTool.connDevice(item.address, (result) => {  
        if (result) {  
          if (this.selectDevices.length == 3) {  
            this.selectDevices.shift();  
          }  
          this.selectDevices.push(item);  
          uni.setStorageSync("selectDevices", this.selectDevices);  
        }  
        console.log("连接结果:", result);  
      });  
    },  
    senBleLabel() {  
      var command = tsc.jpPrinter.createNew();  
      command.init(0, 200, 200, 210, 1);  
      command.setCls();  
      command.setQR(30, 40, 2, 6, "123");  
      command.setMag(2, 2);  
      command.setText1(55, 30, 180, 60, "地址: 内蒙");  
      command.setMag(2, 2);  
      command.setText1(55, 6, 180, 120, "重量: 15kg");  
      command.setPagePrint();  
      let data = command.getData();  
      bluetoothTool.sendByteData(data);  
    },  
  },  
};  
</script>
0 关注 分享

要回复文章请先登录注册

1***@qq.com

1***@qq.com

你好有完整的代码 希望能参考一下
2023-07-14 13:26
1***@qq.com

1***@qq.com

请问实现自动连接了吗
2023-07-12 15:41
x***@gmail.com

x***@gmail.com

BluetoothTool.js可以分享一下嘛?
2023-05-10 13:30