You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用@brooons/react-native-bluetooth-escpos-printer打印开头出现感叹号求助

解决@brooons/react-native-bluetooth-escpos-printer v1.2.0打印开头出现感叹号的问题

可行修复方案

1. 排查库默认指令前缀

v1.2.0版本的库可能在打印逻辑中自动注入了带感叹号的控制指令,可尝试手动构造打印内容,跳过库的默认前缀逻辑:

import { BluetoothEscposPrinter, Printer } from '@brooons/react-native-bluetooth-escpos-printer';

// 手动拼接打印元素,避免库自动添加额外字符
const printData = [
  Printer.TEXT('需要打印的内容', {
    encoding: 'GBK',
    align: Printer.ALIGN.CENTER
  }),
  Printer.NEW_LINE(),
  Printer.NEW_LINE()
];

BluetoothEscposPrinter.printText(printData.join(''), {})
  .then(() => console.log('打印完成'))
  .catch(err => console.error(err));

2. 手动过滤前置感叹号

如果库自动添加的感叹号无法通过构造内容避免,可在打印前直接清除开头的感叹号:

const targetContent = '!实际要打印的内容';
const cleanedContent = targetContent.replace(/^!/, '');

BluetoothEscposPrinter.printText(cleanedContent, { encoding: 'GBK' })
  .then(() => console.log('打印完成'))
  .catch(err => console.error(err));

3. 更换库版本

该问题可能是v1.2.0的专属bug,尝试降级到更早的稳定版本或升级到后续版本:

# 降级到v1.1.5
npm install @brooons/react-native-bluetooth-escpos-printer@1.1.5 --save
# 或升级到最新稳定版
npm install @brooons/react-native-bluetooth-escpos-printer@latest --save

4. 发送打印机初始化指令

部分蓝牙打印机缓存可能导致异常字符输出,打印前发送ESC初始化指令清除缓存:

// 发送ESC @指令重置打印机
const initCmd = '\x1B\x40';
const printContent = initCmd + '需要打印的内容';

BluetoothEscposPrinter.printText(printContent, { encoding: 'GBK' })
  .then(() => console.log('打印完成'))
  .catch(err => console.error(err));

内容的提问来源于stack exchange,提问作者riane riri

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.04 13:40:33