更新记录

1.0.0(2025-05-28)

佳博打印机,热敏条码打印机,支持小票、面单、标签,支持蓝牙、Wi-Fi、USB、串口等连接


平台兼容性

uni-app

Vue2 Vue3 Chrome Safari app-vue app-nvue Android iOS 鸿蒙
- - - - 4.4 × ×
微信小程序 支付宝小程序 抖音小程序 百度小程序 快手小程序 京东小程序 鸿蒙元服务 QQ小程序 飞书小程序 快应用-华为 快应用-联盟
× × × × × × - × × × ×

uni-app x

Chrome Safari Android iOS 鸿蒙 微信小程序
- - 5.0 × × ×

佳博打印机,热敏条码打印机,支持小票、面单、标签,支持蓝牙、Wi-Fi、USB、串口等连接

集成步骤:

接口


import {
    UTSGPSDK,
    UTSBluetoothBT,
    UTSUsbManager199
} from "@/uni_modules/wrs-uts-gpsdk"
let gpsdk = new UTSGPSDK()
  • 设置回调

gpsdk.onCallback((resp) => {
    this.showMsg(JSON.stringify(resp))
    let opt = resp.opt
    switch (opt){
        case "onConnecting":
        {
            // 连接中。。。
            this.showMsg("连接中。。。")
        }
            break;
            case "onSuccess":
            {

                this.showMsg("onSuccess")
            }
            break;
            case "onFailure":
            {
                this.showMsg("onFailure")

            }
            break
            case "onDisconnect":
            {
                this.showMsg("打印机断开连接")
            }
            break;
            case "onCheckCommand":
            {
                this.showMsg("onCheckCommand。。。")
            }
            break;
        default:
            break;
    }
})
  • 连接打印机,支持蓝牙、Wi-Fi、串口、usb等方式连接

let address = device.address
// 连接方式,支持BLUETOOTH、BLE_BLUETOOTH、USB、WIFI、SERIALPORT
let connMethod = "BLUETOOTH"
let params = {}

params.connMethod = connMethod
// 命令类型,支持ESC、TSC、CPCL
params.command = this.command
switch (connMethod) {
    case "BLUETOOTH":
        params.macAddress = address
        break;
    case "BLE_BLUETOOTH":
        params.macAddress = address
        break;
    case "USB":
        // 通过UTSUsbManager.getDeviceList((resp)=>{})接口获取deviceId
        params.deviceId = 12121
        break;
    case "WIFI":
        // 打印机ip
        params.ip = "192.168.0.1"
        // 打印机端口,一般都是9100
        params.port = 9100
        break;
    case "SERIALPORT":
        // 串口路径,通过let pathArray = gpsdk.getAllDevicesPath()接口选择一个串口路径
        params.serialPort = "xxxx"
        params.baudrate = 9600
        break;
    default:
        break;
}
gpsdk.connect(params, (resp) => {
    this.showMsg(JSON.stringify(resp))
    let flag = resp.flag
    if (flag) {
        // 
    } else {
        // 
    }
})
  • 蓝牙开始扫描

UTSBluetoothBT.startDiscovery()
  • 蓝牙停止扫描

UTSBluetoothBT.cancelDiscovery()
  • 设置蓝牙扫描回调

UTSBluetoothBT.registerReceiver((resp) => {

    let action = resp.action
    switch (action) {
        // 蓝牙状态变化
        case "android.bluetooth.adapter.action.STATE_CHANGED":
            let intent = resp.intent
            let extras = intent.extras
            let state = extras["android.bluetooth.adapter.extra.STATE"]
            this.showMsg("蓝牙状态变化 state:" + state)
            break;
            // 开始扫描蓝牙
        case "android.bluetooth.adapter.action.DISCOVERY_STARTED":
            break;
            // 蓝牙搜索结束
        case "android.bluetooth.adapter.action.DISCOVERY_FINISHED":
            break;
            // 搜索到蓝牙设备
        case "android.bluetooth.device.action.FOUND": {
            let device = resp.device
            let name = device.name
            if (name) {

                let length = this.bluetoothArray.length
                let isExist = false
                for (let i = 0; i < length; i++) {
                    let tempName = this.bluetoothArray[i].name
                    if (name == tempName) {
                        isExist = true
                    }
                }
                if (!isExist) {
                    console.log(JSON.stringify(resp))
                    this.bluetoothArray.push(device)
                }
            }
            //this.showMsg(JSON.stringify(device))

        }

        break;
        //在系统弹出配对框之前(确认/输入配对码)
        case "android.bluetooth.device.action.PAIRING_REQUEST":
            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.device.action.ACL_CONNECTED":
            break;
            //最底层连接断开
        case "android.bluetooth.device.action.ACL_DISCONNECTED":
            break;
            //BluetoothAdapter连接状态
        case "android.bluetooth.adapter.action.CONNECTION_STATE_CHANGED":
            break;
            // BluetoothHeadset连接状态
        case "android.bluetooth.headset.profile.action.CONNECTION_STATE_CHANGED":
            break;
            //BluetoothA2dp连接状态
        case "android.bluetooth.a2dp.profile.action.CONNECTION_STATE_CHANGED":
            break;
        default:
            break;
    }
})
  • 打开蓝牙

UTSBluetoothBT.enable()
  • 判断蓝牙是否打开

let isEnabled = UTSBluetoothBT.isEnabled()
  • 获取所有的串口设备

let deviceArray = gpSDK.getAllDevicesPath()
  • 获取所有的USB设备

UTSUsbManager.getDeviceList((resp)=>{

})
  • 断开打印机连接

gpSDK.close()
  • 获取打印机状态

let status = gpSDK.getPrinterStatus() if(status == 0) { // 正常 } else { // 不正常 }


- 打印小票ESC

let params = {} params.command = "ESC" params.commands = [{ //初始化打印机 opt: "addInitializePrinter" }, { // 设置打印居中 opt: "addSelectJustification", value: "CENTER" }, { // 打印文字 opt: "addText", value: "ESC票据测试\n" }, { //打印并换行 opt: "addPrintAndLineFeed" }, { // 设置打印居中 opt: "addSelectJustification", value: "LEFT" }, { // 打印文字 opt: "addText", value: "打印文字测试" }, { //打印并换行 opt: "addPrintAndLineFeed" }, { //切纸(带切刀打印机才可用) opt: "addCutPaper" } ] gpsdk.sendCommands(params, (resp) => { this.showMsg(JSON.stringify(resp)) })


- esc打印pdf

```javascript

// 本地pdf文件的绝对路径
let path = plus.io.convertLocalFileSystemURL('_www/static/test.pdf')
let is58MM = true
let width = is58MM ? 384 : 576
gpsdk.writePDFToEsc(path, width, (resp) => {
    this.showMsg(JSON.stringify(resp))
})
  • 打印标签TSC

let params = {}
params.command = "TSC"
params.commands = [
    {
        opt: "addUserCommand",
        value: "\r\n"
    },
    {
        // 设置标签尺寸宽高,按照实际尺寸设置 单位mm
        opt: "addSize",
        width: 58,
        height: 70
    },
    {
        // 设置标签间隙,按照实际尺寸设置,如果为无间隙纸则设置为0 单位mm
        opt: "addGap",
        value: 0
    },
    {
         // 设置原点坐标
        opt: "addReference",
        x: 0,
        y: 0
    },
    {
         // 撕纸模式开启
        opt: "addTear",
        value: "ON"
    },
    {
          // 清除打印缓冲区
        opt: "addCls"
    },
    {
           // 绘制简体中文
        opt: "addText",
        x: 30,
        y: 30,
        font: "TSS24.BF2",
        rotation:0,
        Xscal:1,
        Yscal:1,
        text: "欢迎使用Printer"
    },
    {
          // 打印标签
        opt: "addPrint",
        m: 1,
        n: 1
    },
    {
         // 打印标签后 蜂鸣器响
        opt: "addSound",
        level: 1,
        interval: 100
    },
]
gpsdk.sendCommands(params, (resp) => {
    this.showMsg(JSON.stringify(resp))
})
  • 打印面单CPCL

let params = {}
params.command = "CPCL"
params.commands = [
    {

        opt: "addUserCommand",
        value: "\r\n"
    },
    {
        opt: "addInitializePrinter",
        height: 900,
        qty: 1
    },
    {

        opt: "addPagewidth",
        value: 576
    },
    {

        opt: "addJustification",
        value: "CENTER"
    },
    {

        opt: "addText",
        font: "4",
        x: 0,
        y: 30,
        text: "CPCL测试"
    },
    {

        opt: "addPrint"
    },
]
gpsdk.sendCommands(params, (resp) => {
    this.showMsg(JSON.stringify(resp))
})

隐私、权限声明

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

蓝牙

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

插件不采集任何数据

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

暂无用户评论。

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