收藏
回答

小程序连接打印机,发送成功数据却打印不出来。可以走纸,确定打印机没问题。

框架类型 问题类型 API/组件名称 终端类型 微信版本 基础库版本
小程序 Bug wx.writeBLECharacteristicValue 微信安卓客户端 8.0.24 2.25.3
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({
                        statetrue// 启用 notify 功能
                        deviceId: deviceId,
                        serviceId: serviceId,
                        characteristicId: uuid,
                        success(res) { 
                            console.log('第七步,启用notify成功', res.errMsg) 


                            let arrPrint = [];
                            //初始化打印机
                            arrPrint.push(util.sendDirective([0x1B0x40])); //16进制
                            //居中对齐
                            arrPrint.push(util.sendDirective([0x1B0x610x01])); //居中
                            //正文
                            arrPrint.push(util.sendDirective([0x1B0x0E]));
                            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, //写入数据
                                            successfunction (res{
                                                console.log('writeBLECharacteristicValue success ->'+i, res) 
                                            },
                                            failfunction (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, //写入数据
                                            successfunction (res{
                                                console.log('writeBLECharacteristicValue success ->'+i, res) 
                                            },
                                            failfunction (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)
            }
        })
回答关注问题邀请回答
收藏

2 个回答

  • 胖达-177
    胖达-177
    2023-06-05

    万能云打印小程序,让你的网络打印机秒变微信手机打印机。

    只要你的打印机与手机在同一 wifi,一键自动搜索、匹配您的打印机型号,不用安装驱程。

    几乎支持所有品牌的打印机,支持常用办公文档、图片打印。简单易用,完全免费,请微信搜索“万能云打印"小程序试用。

    2023-06-05
    有用
    回复
  • 风飞
    风飞
    2023-02-22

    2023-02-22
    有用
    回复
登录 后发表内容