uniapp内置蓝牙打印

发布于:2025-07-05 ⋅ 阅读:(18) ⋅ 点赞:(0)

在这里插入图片描述

概述

内置打印机,无需原生SDK支持,直接用uniapp蓝牙调用打印,适用于所有内置打印机的设备

下载源码

功能说明

  1. 打印服务介绍
    打印服务是系统预置的功能,用于管理打印机设置和外接打印机的添加。开发者通过连接内置打印机进行打印业务,包括58和80mm两种打印规格的热敏打印。

  2. 注意事项

    务必打开设备蓝牙开关,再导入示例程序运行

    无需APP原生任何SDK导入,只要设备支持蓝牙低功耗协议即可,

    尽量使用 HBuilderX 4.0+ 以上版本

    打印结束后也及时关闭打印服务

  3. 内置蓝牙打印机原理

    在蓝牙设备列表中可以看到1个已经配对,且永远存在的蓝牙设备“InnerPrinter”,这是由操作系统虚拟出来的打印机设备,实际并不存在。虚拟蓝牙支持Sunmi《esc/pos》指令。

    大致实现过程如下:

    1). uni.createBLEConnection与该蓝牙设备建立连接。

    2). 将指令和文本内容拼接转码为Bytes。

    3). 发送给InnerPrinter。

    4). 底层打印服务驱动打印设备完成打印。

  4. 常见问题
    代码报错: 23:25:52.693 …/…/…/…/C:/Users/86198/Documents/HBuilderProjects/内置蓝牙打印示例/uni_modules/sunmi-blueprint/js_sdk/bluetoothMixin.js (1:7):
    “default” is not exported by “…/…/…/…/C:/Users/86198/Documents/HBuilderProjects/内置蓝牙打印示例/uni_modules/sunmi-blueprint/js_sdk/printerjobs.js”, imported

这是明显的Node版本不兼容导致的,尽可能使用14版本,编译版本一定要选VUE2,不要选VUE3
Hbuilder使用4.0以上,最好是最新的

再强调下,一定要选择VUE2 VUE2 VUE2,文档也写了,示例也发了,运行截图也有,也有GPT可以查,还不会就告别这个行业吧

上来啥也不会就给1星的白痴勿扰,免费的插件,还求着你用吗?

完整代码示例

<template>
	<view style="padding: 10px;">
		  <button @click="innerPrinter('1')" style="margin-top: 10px;">打印[堂食-结账单]</button>
		  <button @click="innerPrinter('2')" style="margin-top: 10px;">打印[堂食-点菜单]</button>
		  <button @click="innerPrinter('3')" style="margin-top: 10px;">打印[零点-结账单]</button>
		  <button @click="innerPrinter('4')" style="margin-top: 10px;">打印[外卖订单]</button>
		  <button @click="innerPrinter('5')" style="margin-top: 10px;">打印[排号小票]</button>
		  <button @click="innerPrinter('6')" style="margin-top: 10px;">打印[堂食-预结单]</button>
		  <button @click="closePrint()" style="margin-top: 10px;">关闭蓝牙打印</button>
	</view>
</template>

<script>
	import { bluetoothMixin } from '@/uni_modules/sunmi-blueprint/js_sdk/bluetoothMixin'
	import printerUtil from '@/uni_modules/sunmi-blueprint/js_sdk/printerutil.js'
	import jsonData1 from './jsonData1.json';
	import jsonData2 from './jsonData2.json';
	import jsonData3 from './jsonData3.json';
	import jsonData6 from './jsonData6.json';
	export default {
		mixins: [bluetoothMixin], // 使用mixin
		data() {
			return {
				//内置打印机-蓝牙信息配置
				printConfig:{
				  "deviceId": "00:11:22:33:44:55",
				  "serviceId": "00001101-0000-1000-8000-00805F9B34FB",
				  "notifyId": "00001101-0000-1000-8000-00805F9B34FB",
				  "writeId": "00001101-0000-1000-8000-00805F9B34FB",
				},
			}
		},
		methods: {
			innerPrinter(templateId) {
			  switch(templateId){
				case '1'://堂食-结账单
				  this.templatePrint(this.printConfig,templateId, 1,jsonData1)
				  break;  
			    case '2'://堂食-点菜单
			      this.templatePrint(this.printConfig,templateId,1,jsonData2)
			      break;
				case '3'://零点-结账单
				  this.templatePrint(this.printConfig,templateId, 1, jsonData3)
				  break;  
				case '4'://外卖订单
				  this.templatePrint(this.printConfig,templateId, 1, {})
				  break; 
				case '5'://排号小票
				  this.templatePrint(this.printConfig,templateId, 1, {})
				   break; 	
			    case '6'://堂食-预结单
			      this.templatePrint(this.printConfig,templateId, 1, jsonData6)
			      break;
			  }
			}
		}
	}
</script>

打印方法

import PrinterJobs from './printerjobs.js'
import printerUtil from './printerutil.js'
printerJobs
	.setAlign('rt') //文字居中方式设置 对齐方式 LT/CT/RT
	.setSize(2, 2)  //设置字体大小 高度 宽度
	.print(printerUtil.fillLine())  //打印分割线
	.setBold(true)//字体加粗
	.print(printerUtil.inline('应付', '¥99.99'))  //两列
	.print(printerUtil.inline2('鲜汁肉包', 'x2', '¥199.99')) //打印三列
	.text('内容') //增加打印内容
	.println()//打印换行
	.setLineSpacing(10) //设置行间距为 n 点行,默认值行间距是 30 点
	.lineFeed()//打印空行
	.setColor(0)//设置字体颜色,需要打印机支持 0 默认颜色黑色 1 红色
	.beep() //蜂鸣警报,需要打印机支持
	.clear() //清空任务 防止打印任务太多,打印机停不下来
	.printQrcodeByESC('123456') //打印二维码
	.cutPaper() //切纸

长期维护,持续更新,提供终身免费技术支持

在这里插入图片描述