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

Flutter中blue_thermal_printer无法触发钱箱及替代包版本问题求助

Flutter触发钱箱问题解决方案

问题背景

使用blue_thermal_printer包调用drawerPin5()触发钱箱无反应;切换esc_pos_printer包时遇到版本兼容错误:Build failed due to use of deprecated Android v1 embedding,需要实现支付完成后打印收据并自动打开钱箱的功能。

用户代码示例

import 'package:blue_thermal_printer/blue_thermal_printer.dart';
BlueThermalPrinter bluetooth = BlueThermalPrinter.instance;
extension PrintSize on Size {
  int get val {
    switch (this) {
      case Size.medium:
        return 0;
      case Size.bold:
        return 1;
      case Size.boldMedium:
        return 2;
      case Size.boldLarge:
        return 3;
      case Size.extraLarge:
        return 4;
      default:
        return 0;
    }
  }
}
sample() async {
    await bluetooth.isConnected.then((isConnected) {
      if (isConnected == true) {
        bluetooth.printNewLine();
        bluetooth.printCustom("Receipt", Size.boldMedium.val, Align.center.val);
        bluetooth.printNewLine();
        bluetooth.printCustom("TEST PRINT", Size.bold.val, Align.center.val);
        bluetooth.printNewLine();
        bluetooth.paperCut();
        bluetooth.drawerPin5(); // 调用此函数触发钱箱但无任何反应
      }
    });
}

解决方案

方案一:修复blue_thermal_printer钱箱触发问题

  1. 切换钱箱触发引脚
    部分钱箱使用Pin2而非Pin5,尝试调用bluetooth.drawerPin2()替代drawerPin5()。

  2. 手动发送ESC指令(推荐)
    多数支持钱箱的打印机遵循ESC/POS指令集,直接发送指令比依赖包封装函数更可靠:

    sample() async {
        await bluetooth.isConnected.then((isConnected) {
          if (isConnected == true) {
            // ... 原有打印代码 ...
            bluetooth.paperCut();
            // 发送ESC指令触发Pin5钱箱:ESC p 0 25 25
            bluetooth.writeBytes([0x1B, 0x70, 0x00, 0x19, 0x19]);
            // 若钱箱接Pin2,使用此指令:ESC p 1 25 25
            // bluetooth.writeBytes([0x1B, 0x70, 0x01, 0x19, 0x19]);
          }
        });
    }
    

    指令说明:0x1B是ESC字符,0x70是触发钱箱的指令,第三个参数指定引脚(0=Pin5,1=Pin2),最后两个参数是脉冲时长(25=约100ms,可根据钱箱调整)。

  3. 检查打印机兼容性
    确认打印机支持钱箱触发功能,部分低端打印机可能不支持该指令。

方案二:解决esc_pos_printer版本兼容问题

  1. 升级包版本
    在pubspec.yaml中使用最新版本的esc_pos_printer(如^4.0.0及以上),新版本已适配Android v2嵌入。

  2. 修改Android项目配置

    • 打开android/app/src/main/AndroidManifest.xml,修改Activity配置:
      <activity
          android:name="io.flutter.embedding.android.FlutterActivity"
          android:exported="true"
          android:launchMode="singleTop"
          android:theme="@style/LaunchTheme">
          <meta-data
              android:name="flutterEmbedding"
              android:value="2" />
          <!-- 其他原有配置 -->
      </activity>
      
    • 打开android/build.gradle,确保compileSdkVersion≥33,targetSdkVersion与compileSdkVersion匹配。
  3. 使用esc_pos_printer触发钱箱
    若包配置正常,可通过以下代码触发钱箱:

    import 'package:esc_pos_printer/esc_pos_printer.dart';
    import 'package:esc_pos_utils/esc_pos_utils.dart';
    
    // 连接打印机后执行
    final profile = await CapabilityProfile.load();
    final printer = NetworkPrinter(PaperSize.mm80, profile);
    final PosPrintResult res = await printer.connect('192.168.1.100', port: 9100);
    if (res == PosPrintResult.success) {
      // ... 打印收据代码 ...
      // 触发Pin5钱箱
      printer.drawerPin5();
      printer.disconnect();
    }
    

内容的提问来源于stack exchange,提问作者gan jinqu

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.10 16:01:47