uni连接便携式蓝牙打印

流程:

   连接

  1. 检查蓝牙是否打开(uni.openBluetoothAdapter)
  2. 开始搜素周围蓝牙设备(uni.startBluetoothDevicesDiscovery)
  3. 搜索到新的设备(自己将新设备添加到列表)(uni.onBluetoothDeviceFound)
  4. 连接设备(uni.createBLEConnection)
  5. 停止搜索(因为搜索很耗内存)(uni.stopBluetoothDevicesDiscovery)
  6. 获取设备所有服务(uni.getBLEDeviceServices)
  7. 获取服务的特征值(uni.getBLEDeviceCharacteristi

123是按顺序来的,所以方法都在上一的回掉中执行,467同理。

以上的所有接口在官网可查详情:uni-app官网

   打印 

        运用CPCL指令集(不同的设备用不同的指令),使用encode编码打印

        注意:蓝牙传输有字节限制,如有需要,请分包发送

   打印坑

              标签元素的纵坐标可以生效,横坐标设置了不生效 !!!

   解决方法

                在最前面插入一个空(‘’)文字

省流:

        booth.js是封装的方法,还有需要cpcl.js(打印机指令),encoding.js(编码js),encoding-indexes.js(编码js)。booth.js和cpcl.js需要按照需求更改代码,encoding.js和encoding-indexes.js可以在网上搜到(CSDN不知道怎么弄上去,可私信)

        booth.js(最好把方法都封装一下,挂到vue,方便不同地方调用):
var cpcl = require("./cpcl.js");
let Booth={
    deviceId:'',
    serviceId:'',
    characteristicId:'',
    oneTimeData:0,
    looptime:0,
    lastData:0,
    currentTime: 1,
    list:[],
    boothlist:[],
    
    getlanya(){
        uni.openBluetoothAdapter({  //蓝牙是否打开
          fail(){
            uni.showToast({
              title: '请打开蓝牙',
              icon:'error',
              duration: 2000
            });
          }
        })
        if(uni.getStorageSync('deviceId')){
            this.deviceId = uni.getStorageSync('deviceId')
            this.link()
        }else{
            const that = this
            uni.startBluetoothDevicesDiscovery({    //搜素周围蓝牙设备
              success(res) {
                uni.onBluetoothDeviceFound(res=> {    //搜索到新的设备
                  that.boothlist.push(res)
                })
              }
            })
        }
    },
    link(value){
        uni.stopBluetoothDevicesDiscovery({   //停止搜索
          success(res) {
            console.log('停止了搜索');
          }
        })
        this.deviceId = value
        const deviceId = value;
        const that = this
         uni.createBLEConnection({    //连接设备
          deviceId,
          success(res) {
              console.log('link中++++++++++++++++++');
              setTimeout(()=>{ 
                  that.getServe()
              },100)
          },
          fail(err){
            console.log(err,'link错误');
            uni.hideLoading();
            uni.showToast({
              title: '连接超时',
              icon:'error',
              duration: 2000
            });
            this.say.say('连接超时')
          }
        })
    },
    getServe(){   //获取设备所有服务
        const deviceId = uni.getStorageSync('deviceId')
        const that = this
        setTimeout(()=>{
          uni.getBLEDeviceServices({    
            deviceId,
            success(res) {
              console.log('getServe成功');
              that.serviceId = res.services[0].uuid
              uni.setStorageSync('serviceId', this.serviceId);
              that.getCharacteristic()
            }
          })
        },1000)
    },
    getCharacteristic(){		//获取特征值
        const deviceId = this.deviceId;
        const serviceId = this.serviceId;
        const that = this
        uni.getBLEDeviceCharacteristics({
          deviceId,
          serviceId,
          success(res) {
            console.log('获取特征值中')
            that.characteristicId = res.characteristics[0].uuid
            uni.hideLoading();
            uni.showToast({
                  title: '连接成功',
                  duration: 1000 
            });
            setTimeout(()=>{
              uni.navigateBack();
          },1000)
          }
        })
    },
    

    writ(type,value1,value2,value3,value4,value5){
        const command = cpcl.jpPrinter.createNew();
        command.init();
        if(type=='HS'){   
          command.printQRCode(140,50,5,value1);
          command.changebig3()
          command.printlnText90just(24,320,140,value2);
        }else if(type=='QY'){   
            command.center()
            command.changebig5()
            command.printlnText(24,0,50,value1);
        }
         
        this.prepareSend(command.getData()); //准备发送数据
       
    },
    prepareSend(buff) {
        //准备发送,根据每次发送字节数来处理分包数量
        var j = 0;
        var m; 
        for (m = 500; m < 1000; m += 10) {
            this.list[j] = m;
            j++;
        }
        this.oneTimeData = this.list[0]
    
        const that = this;
        const time = that.oneTimeData;
        const looptime = parseInt(buff.length / time);
        const lastData = parseInt(buff.length % time); //console.log(looptime + "---" + lastData)

        this.looptime=looptime + 1,
        this.lastData=lastData,
        this.currentTime=1
        
        
        that.Send(buff);
    },
    Send(buff) {
          //分包发送
          const that = this;
          const currentTime = that.currentTime;
          const loopTime = that.looptime;
          const lastData = that.lastData;
          const onTimeData = that.oneTimeData;
          var buf;
          var dataView;
              const serviceId = this.serviceId
              const deviceId = this.deviceId
              const characteristicId = this.characteristicId
          if (currentTime < loopTime) {
         
            buf = new ArrayBuffer(onTimeData);
            dataView = new DataView(buf);
            var o;
            for (o = 0; o < onTimeData; ++o) {
              dataView.setUint8(o, buff[(currentTime - 1) * onTimeData + o]);
            }
          } else {
          
            buf = new ArrayBuffer(lastData);
            dataView = new DataView(buf);
            var p;
            for (p = 0; p < lastData; ++p) {
              dataView.setUint8(p, buff[(currentTime - 1) * onTimeData + p]);
            }
          } 
          console.log("第" + currentTime + "次发送数据大小为:" + buf.byteLength)
    
          uni.writeBLECharacteristicValue({    //给打印机发送二进制数据
            deviceId,
            serviceId,
            characteristicId,
            value: buf,
            success: function (res) {
                // console.log('打印成功');
    
            },
            fail: function (e) {
                // console.log('打印失败',e);
            },
       
          });
    },
}

export default Booth

  • 4
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值