Giter VIP home page Giter VIP logo

wx-printer's Introduction

wx-printer-cpcl

微信小程序连接热敏打印机打印标签

使用

该项目是在Taro3项目中编写,如需在其他小程序框架项目中使用,请自行修改
使用的打印机型号为汉印HM-A300,打印指令为CPCL指令
具体使用方法请至example页面查看

import BleConnector, { DeviceInfo } from '@/utils/bleConnector'
import PrinterUtil from '@/utils/printerUtil'

const bleConnector = new BleConnector()

// 初始化蓝牙模块
bleConnector.initBle()

// 初始化的适合不想自动连接上次连接过的设备请传false
bleConnector.initBle(false)

// 监听蓝牙适配器状态变化
bleConnector.listen('onAdapterStateChange', res => {
  console.log('蓝牙适配器是否可用:%s', res)
})

// 监听扫描
bleConnector.listen('onScanChange', res => {
  console.log('最新的扫描设备列表:%o', res)
})

// 监听设备连接状态
bleConnector.listen('onConnectChange', res => {
  console.log('设备连接状态:%s', res)
})

// 移除监听事件
bleConnector.removeListen('onAdapterStateChange')
bleConnector.removeListen('onScanChange')
bleConnector.removeListen('onConnectChange')

// 扫描设备
bleConnector.scanDevices()

// 连接设备,需要传入设备信息
bleConnector.connectDevice(device)

// 停止扫描
bleConnector.stopScanDevice()

// 断开设备连接
bleConnector.closeDevice()

// 关闭蓝牙模块
bleConnector.closeAdapter()

// 获取打印数据的arrayBuffer
const options = {
  offset: 0,
  xResolution: 200,
  yResolution: 200,
  height: 200,
  printCount: 1
}

// 第一种方式,适合简单的模版
let { buffer, cpclString } = new PrinterUtil(options)
  .printlnText('条形码', 0, 10, 10)
  .printTextBarCode('13213231241242141', 80, 10, 50)
  .printlnText('----------------------------------------', 0, 10, 150)
  .printlnText('二维码', 0, 10, 180)
  .printQRCode('1232131232131231', 10, 10, 230)

// 第二种方式,直接写CPCL指令
// let { buffer, cpclString } = new PrinterUtil(options)
//   .setCpclString(`T 8 0 10 10 条形码 
//   BT 7 0 5
//   B 128 1 1 80 10 50 13213231241242141
//   BT OFF
//   T 8 0 10 150 ---------------------------------------- 
//   T 8 0 10 180 二维码 
//   B QR 10 230 M 2 U 10
//   M0A,QR code 1232131232131231
//   ENDQR
//   `)

console.log('🖨️设备需要写入的buffer数据:%o', buffer)
console.log('当前需要打印的内容cpcl格式编码字符串:%s', cpclString)

// 写数据
bleConnector.write(buffer)

wx-printer's People

Contributors

gengar-666 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.