更新记录

1.0.8(2024-01-06)

  1. 修复iOS uts语法兼容性问题

1.0.7(2024-01-06)

1.修复Android新版本uts语法兼容性问题

1.0.6(2023-07-28)

修改插件前缀

查看更多

平台兼容性

Vue2 Vue3
App 快应用 微信小程序 支付宝小程序 百度小程序 字节小程序 QQ小程序
HBuilderX 3.6.8,Android:4.4,iOS:不支持 × × × × × ×
钉钉小程序 快手小程序 飞书小程序 京东小程序
× × × ×
H5-Safari Android Browser 微信浏览器(Android) QQ浏览器(Android) Chrome IE Edge Firefox PC-Safari
× × × × × × × × ×

厦门汉印电子技术有限公司-打印机

型号:N41

集成步骤

  • 下载示例项目demo导入HBuilderX
  • 插件网页上点击右上角"试用"按钮,绑定项目AppID,然后继续点击"继续导入HBuilderX"按钮下载插件到项目的uni_modules目录

定制或采购源码联系QQ252797991

API接口


    import {
        UTSBluetoothAdapter,
        UTSPublicFunction,
        UTSHPRTPrinterHelper,
        UTSUsbManager

    } from "@/uni_modules/wrs-uts-hanyin";

    let bluetoothAdapter = new UTSBluetoothAdapter();
    let publicFunction = new UTSPublicFunction();
  • 扫描蓝牙

// 扫描结果在bluetoothAdapter.registerReceiver回调
bluetoothAdapter.startDiscovery();
  • 设置蓝牙回调

                    bluetoothAdapter.registerReceiver((resp) => {
                        let action = resp.action;
                        switch (action) {
                            case "android.bluetooth.device.action.FOUND": {
                                // 扫描到蓝牙
                                let device = resp.device;
                                if(device.bluetoothClass.majorDeviceClass == 1536) {
                                    this.bluetoothArray.push(device);
                                }

                            }
                            break;
                            case "android.bluetooth.device.action.BOND_STATE_CHANGED": {
                                // 蓝牙状态变更
                                let device = resp.device;
                                let bondState = device.bondState;
                                switch (bondState) {
                                    case 11: {
                                        // 正在配对......
                                    }
                                    break;
                                    case 12: {
                                        // 完成配对
                                    }
                                    break;
                                    case 10: {
                                        // 取消配对
                                    }
                                    break;
                                    default:
                                        break;
                                }
                            }
                            break;
                            case "android.bluetooth.adapter.action.DISCOVERY_FINISHED": {
                                // 蓝牙扫描完成
                            }
                            break;
                            default:
                                break;
                        }
                    });
  • 蓝牙停止扫描

bluetoothAdapter.cancelDiscovery();
  • 蓝牙/Wi-Fi连接打印机
                    // 蓝牙连接参数
                    let portSetting = "Bluetooth," + address; 
                    // Wi-Fi连接参数
                    // let portSetting = "WiFi," + this.ip + "," + this.port;
                    UTSHPRTPrinterHelper.portOpen(portSetting, (resp) => {
                        if(resp.flag && resp.result == 0) {
                            // 连接成功
                        } else {
                            // 连接失败
                        }
                    });
  • 获取所有USB设备

                UTSUsbManager.getDeviceList((resp)=>{
                    console.log("getDeviceList resp:" + JSON.stringify(resp));
                    let devices = resp.devices;
                    if(devices.length > 0) {
                        let titles = [];
                        for(let i = 0; i < devices.length; i ++) {
                            let deivce = devices[i];
                            let title = device.deviceName;
                            titles.push(title);
                        }
                        this.showActionSheet(titles, (index)=>{
                            let deivce = devices[i];
                            let deviceId = device.deviceId;
                            let requestCode = this.requestCode;
                            let  action = this.action;
                            let flags  = 0;
                            UTSUsbManager.requestPermission(deviceId, requestCode, action, flags);
                        });
                    } else {
                        this.showToast("没有找到USB设备");
                    }
                });
  • 请求USB权限

                            let deivce = devices[i];
                            let deviceId = device.deviceId;
                            let requestCode = this.requestCode;
                            let  action = this.action;
                            let flags  = 0;
                            // 权限结果通过UTSUsbManager.registerReceiver回调
                            UTSUsbManager.requestPermission(deviceId, requestCode, action, flags);
  • 设置USB回调

                    UTSUsbManager.registerReceiver(this.action, (resp)=>{
                        let action = resp.action;
                        switch (action){
                            case this.action:
                            {
                                let granted = resp.granted;
                                this.showMsg("有USB设备授权结果:" + JSON.stringify(resp));
                                if(granted) {
                                    this.usbDevConect(resp.device.deviceId);
                                }
                            }
                                break;
                            case "android.hardware.usb.action.USB_DEVICE_DETACHED":
                            {
                                this.showMsg("有USB设备插入:" + JSON.stringify(resp));
                            }
                            break;
                            case "android.hardware.usb.action.USB_DEVICE_ATTACHED":
                            {
                                this.showMsg("有USB设备拔出:" + JSON.stringify(resp));
                            }
                            break;
                            default:
                                break;
                        }
                    })
  • USB连接打印机

                    UTSHPRTPrinterHelper.portOpenUSBDevice(deviceId, (resp) => {
                        if(resp.flag && resp.result == 0) {
                            // 连接成功
                        } else {
                            // 连接失败
                        }
                    });
  • 断开连接

                    UTSHPRTPrinterHelper.portClose((resp) => {
                        if(resp.flag && resp.result == 0) {
                            // 连接成功
                        } else {
                            // 连接失败
                        }
                    });
  • 页标签开始指令

            // width:打印范围的宽(单位 mm)。 height:打印范围的高(单位 mm)。
            let width = "100";
            let height = "100";
            UTSHPRTPrinterHelper.printAreaSize(width, height, (resp) => {
                if(resp.flag && resp.result >0) {
                    // 成功
                } else {
                    // 失败
                }
            });
  • 清除打印缓冲区内容

                    // 清除打印缓冲区内容
                    UTSHPRTPrinterHelper.cls((resp) => {
                        if(resp.flag && resp.result >0) {
                            // 成功
                        } else {
                            // 失败
                        }
                    });
  • 页标签结束指令
                    // 页标签结束指令
                    // strnum:打印次数。 strcopies:计数器(默认 1)
                    let strnum = "1";
                    let strcopies = "1";
                    UTSHPRTPrinterHelper.print(strnum, strcopies, (resp) => {
                        if(resp.flag && resp.result >0) {
                            // 成功
                        } else {
                            // 失败
                        }
                    });
  • 打印测试页
                    // 打印测试页
                    UTSHPRTPrinterHelper.selfTest((resp) => {
                        if(resp.flag && resp.result >0) {
                            // 成功
                        } else {
                            // 失败
                        }
                    });
  • 打印图片

                    // 打印图片
                    // x_pos:图片起始的 x 坐标。 
                    //y_pos:图片起始的 y 坐标。 
                    // fileObj { type: "url", value: ""}:图片
                    // fileObj.type: 图片文件类型,取值范围:url、base64
                    // fileObj.value: 图片文件值
                    //isNegate:图片反白。 True:正常显示。 False:取反显示。 
                    // isLZO:是否压缩(必须打印机支持压缩功能)。 
                    // Type: 图片算法。 0:二值算法。 1:半色调算法。
                    let x_pos = "0";
                    let y_pos = "0";
                    let fileObj = {
                        type: "url",
                        value: "/ssss/sss.png" // 图片路径绝对地址或图片的base64
                    };
                    let isNegate = true;
                    let isLZO = false;
                    let type = 1;
                    UTSHPRTPrinterHelper.printImage(x_pos, y_pos, fileObj, isNegate, isLZO, type, (resp) => {
                        if(resp.flag && resp.result >0) {
                            // 成功
                        } else {
                            // 失败
                        }
                    });
  • 文本打印,包含一些变体

                    // 文本打印,包含一些变体
                    // x_pos:文字起始 x 坐标:
                    // y_pos:文字起始 y 坐标: 
                    //font:
                    // 0: Monotye CG Triumvirate Bold Condensed, font width and height is stretchable 
                    // 1: 8 x 12 fixed pitch dot font 
                    // 2: 12 x 20 fixed pitch dot font 
                    // 3: 16 x 24 fixed pitch dot font 
                    // 4: 24 x 32 fixed pitch dot font 
                    // 5: 32 x 48 dot fixed pitch font 
                    // 6: 14 x 19 dot fixed pitch font OCR-B 
                    // 7: 21 x 27 dot fixed pitch font OCR-B 
                    // 8: 14 x25 dot fixed pitch font OCR-A 
                    // 9:只有这个字体能够打印中文。 
                    // Rotation:打印方向。 
                    // 0 : No rotation 
                    // 90 : degrees, in clockwise direction 
                    // 180 : degrees, in clockwise direction 
                    // 270 : degrees, in clockwise direction 
                    // x_multiplication:x 轴方向文字拉伸的倍数(0 号字体和 9 号字体代表字体的真实 大小,单位 px)。
                    // y_multiplication:y 轴方向文字拉伸的倍数(0 号字体和 9 号字体代表字体的真实 大小,单位 px)。 
                    // Alignment:对齐方式(部分旧机型不支持该功能,可以去掉该参数) 1:左对齐 2:居中 3:右对齐
                    // code_data:文本数据
                    let x_pos = "50";
                    let y_pos = "50";
                    let font = "9";
                    let rotation = "0";
                    let x_multiplication = "1";
                    let y_multiplication = "1";
                    let alignment = 2;
                    let code_data = "厦门汉印电子技术有限公司";
                    UTSHPRTPrinterHelper.printText(x_pos, y_pos, font, rotation, x_multiplication,
                        y_multiplication, alignment, code_data, (resp) => {
                            if(resp.flag && resp.result >0) {
                                // 成功
                            } else {
                                // 失败
                            }
                    });
  • 打印二维码
                    // 打印二维码
                    // rotation:条码方向:
                    // 0 : No rotation 
                    // 90 : degrees, in clockwise direction 
                    // 180 : degrees, in clockwise direction 
                    // 270 : degrees, in clockwise direction 
                    // x_pos:二维码的起始横坐标。 
                    // y_pos:二维码的起始纵坐标。 
                    // ecc_level:纠错等级 
                    // L : 7% 
                    // M : 15% 
                    // Q : 25% 
                    // H : 30% 
                    // width:宽度:1~10 
                    // Mode:模式
                    // A: Auto 
                    // M: Manual 
                    // code_data:二维码的数据。
                    let x_pos = "10";
                    let y_pos = "10";
                    let ecc_level = "25%";
                    let width = "3";
                    let mode = "A";
                    let rotation = "0";
                    let code_data = "http://www.baidu.com";
                    UTSHPRTPrinterHelper.printQRcode(x_pos, y_pos, ecc_level, width, mode, rotation, code_data,
                        (resp) => {
                            if(resp.flag && resp.result >0) {
                                // 成功
                            } else {
                                // 失败
                            }
                        });

隐私、权限声明

1. 本插件需要申请的系统权限列表:

1. 读写、 蓝牙、 定位

2. 本插件采集的数据、发送的服务器地址、以及数据用途说明:

插件不采集任何数据

3. 本插件是否包含广告,如包含需详细说明广告表达方式、展示频率:

暂无用户评论。

使用中有什么不明白的地方,就向插件作者提问吧~ 我要提问