赞
踩
方式一:
通过微信与机器建立连接之后然后通过指定指令去进行打印
方式二:
不使用指令,使用微信api进行打印,先要进行转码,打印中文会有一个乱码的情况可以看一下下面的链接,
乱码情况: https://blog.csdn.net/qq_38188047/article/details/105240273
指令打印步骤具体步骤
上面的步骤不能乱,否则可能搜索不到蓝牙打印设备
简单效果展示
获取到数据渲染到列表,然后建立链接
connect(e) { const _that = this; wx.showToast({ title: '打印机连接中', }); wx.createBLEConnection({ deviceId: e.target.dataset.deviceid, //上面选择蓝牙设备的deviceId,例:连接第一个设备devices[0].deviceId success(res) { wx.showToast({ title: '连接成功', }); //已连接停止搜索 wx.stopBluetoothDevicesDiscovery({ success(res) { console.log(res) } }) if (res.errCode == 0) { _that.getServeice(e.target.dataset.deviceid) } }, fail(err) { console.log(err, 'err'); } }) },
连接之后获取服务信息,判断是否可以支持打印
getServeice(deviceId) { //获取服务列表 const _that = this; var nowType = false; wx.getBLEDeviceServices({ deviceId, //已连接的蓝牙设备ID success: async function (res) { for (let index = 0; index < res.services.length; index++) { const item = res.services[index]; if (nowType === false) { const serviceType = await _that.decideCharacteristics(item, deviceId); if (serviceType) { //可以打印,进行打印相关指令 nowType = true; //指令转码 _that.valueToBuffer(serviceType.deviceId, serviceType.uuid, serviceType.characteristicsId); } else { nowType = false; } } } }, fail(err) { wx.showToast({ title: err.errMsg, }); } }) },
//判断当前服务是否可以进行打印值,可以打印返回相应的值, decideCharacteristics(services, deviceId) { let useSVtype = false; //判断当前服务有没有可进行打印 return new Promise((resolve, reject) => { wx.getBLEDeviceCharacteristics({ deviceId, serviceId: services.uuid, success(data) { for (var i = 0; i < data.characteristics.length; i++) { var charData = data.characteristics[i]; if (charData.properties.write) { //判断打印属性 useSVtype = true; resolve({ deviceId, uuid: services.uuid, characteristicsId: charData.uuid }); } }; if (useSVtype === false) { //当前服务不可打印 resolve(false) } } }) }) },
完成之后把指令换成字buffer
valueToBuffer(deviceId, serviceId, characteristicId) { let bufferData = new Promise((resolve, reject) => { //这个 _t.printData就是你的指令!例如 :!! 0 200 200 670 1\r\nCENTER\r\nSETMAG 2 2\r\nTEXT 55 7 0 0 运单号:620058136\r\n... resolve(this.cutCommand(this.data.printData)); }); bufferData.then(buffer => { this.writeBLECharacteristicValue(deviceId, serviceId, characteristicId, buffer, 1); }); }, cutCommand(data) { //指令转换 var sendData64 = []; var packageLength = 10; var byData = wx.base64ToArrayBuffer(ToBase64.encode64gb2312(data)); //指令转码转码 for (let i = 0; i < Math.ceil(byData.byteLength / packageLength); i++) { var ble_end = packageLength * (i + 1); var ble_begin = i * packageLength if (ble_end > byData.byteLength) { //从begin(包括),到end(不包括)。 const newBuffer = byData.slice(ble_begin); // 从哪个开始到那个结束 sendData64[i] = newBuffer } else { const newBuffer = byData.slice(ble_begin, ble_end); // 从哪个开始到那个结束 sendData64[i] = newBuffer } } return sendData64; },
进行打印
writeBLECharacteristicValue(deviceId, serviceId, writeId, buffer, times) { var _that = this; var sendData64 = buffer; if (sendData64.length >= times) { wx.writeBLECharacteristicValue({ deviceId: deviceId, serviceId: serviceId, characteristicId: writeId, value: sendData64[times - 1], // value: wx.base64ToArrayBuffer(sendData64[times - 1]), success: function (res) { wx.showLoading({ title: '打印中...', mask: true }); _that.writeBLECharacteristicValue(deviceId, serviceId, writeId, buffer, ++times); }, fail: function (res) { _that.showMyModel('提示', '传输失败'); } }) } else { wx.hideLoading(); _that.showMyModel('提示', '传输完成'); } },
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。