[图片]
小程序连接打印机,发送成功数据却打印不出来。可以走纸,确定打印机没问题。wx.getBLEDeviceCharacteristics({ deviceId, serviceId, success: (res) => { console.log('getBLEDeviceCharacteristics success', res.characteristics) for (let i = 0; i < res.characteristics.length; i++) { let item = res.characteristics[i]; let _uuid = item.uuid; let bindDevice = tthis.data.bindDevice; // 读取低功耗蓝牙设备的特征值的二进制数据值 注意:必须设备的特征值支持 read 才可以成功调用。 if (item.properties.read) { bindDevice.read = true; } //向低功耗蓝牙设备特征值中写入二进制数据。注意:必须设备的特征值支持 write 才可以成功调用。 if (item.properties.write) { bindDevice.serviceId = serviceId; bindDevice.characteristicId = _uuid; bindDevice.write = true; // callback ? callback() : '' } //启用低功耗蓝牙设备特征值变化时的 notify 功能,使用characteristicValueChange事件 var uuid = "" if (item.properties.notify || item.properties.indicate) { uuid=_uuid; } wx.notifyBLECharacteristicValueChange({ state: true, // 启用 notify 功能 deviceId: deviceId, serviceId: serviceId, characteristicId: uuid, success(res) { console.log('第七步,启用notify成功', res.errMsg) let arrPrint = []; //初始化打印机 arrPrint.push(util.sendDirective([0x1B, 0x40])); //16进制 //居中对齐 arrPrint.push(util.sendDirective([0x1B, 0x61, 0x01])); //居中 //正文 arrPrint.push(util.sendDirective([0x1B, 0x0E])); arrPrint.push(util.hexStringToBuff("\n葵花生活\n\n")); for(var jj=0;jj<arrPrint.length;jj++) for(var k=0;k<1;k++){ //连续打2次 var sendData ="你好,这是测试数据"; let buffer = arrPrint[jj]; var length = buffer.byteLength; console.log("length:",length); var count = Math.ceil(length/20); //最多执行 count 次 for(var i=0;i<count;i++){ //对buffer进行分包,最大不超过20字节 let TempBuffer=""; if(((i+1)*20)<length){ TempBuffer = buffer.slice(i*20,(i+1)*20); console.log("正在进行第"+i+"次数据写入:"+TempBuffer); //写入设备 wx.writeBLECharacteristicValue({ deviceId: deviceId, //设备deviceId serviceId: serviceId, //设备service_id characteristicId:_characteristicId, //设备write特征值 value: TempBuffer, //写入数据 success: function (res) { console.log('writeBLECharacteristicValue success ->'+i, res) }, fail: function (res) { console.log('writeBLECharacteristicValue fail', res) } }) }else{ TempBuffer = buffer.slice(i*20,length); console.log("正在进行第"+i+"次数据写入:"+TempBuffer); //写入设备 wx.writeBLECharacteristicValue({ deviceId: deviceId, //设备deviceId serviceId: serviceId, //设备service_id characteristicId:_characteristicId, //设备write特征值 value: TempBuffer, //写入数据 success: function (res) { console.log('writeBLECharacteristicValue success ->'+i, res) }, fail: function (res) { console.log('writeBLECharacteristicValue fail', res) } }) } // tthis.sleep(i*0.02); //延迟 i*200ms } } }, fail(e){ console.log("fail:", JSON.stringify(e)) } }) //设置当前选中的蓝牙设备,包括是否可读写属性采集 tthis.setData({ bindDevice: bindDevice }); console.log(tthis.data); } }, fail(res) { console.error('获取特征值失败:', res) } })
2023-02-22