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

如何在Flutter中使用Imchine E-POS AP02打印发票收据

用Flutter实现Imchine E-POS AP02发票打印方案

Imchine E-POS AP02支持标准ESC/POS指令集,市面上主流的ESC/POS打印库均可适配,下面是具体的实现指导和代码示例:

一、设备兼容性确认

你调研的esc_pos_printer、blue_thermal_printer都可以适配AP02,优先推荐esc_pos_printer,它对ESC/POS指令的封装更完整,适配成本更低。

二、设备连接步骤

AP02常用蓝牙连接,步骤如下:

  • 让打印机进入配对模式(参考设备说明书操作)
  • 在系统设置中完成打印机与手机/平板的蓝牙配对
  • 获取打印机的MAC地址(可在设备底部标签或系统蓝牙配对列表中查看)

三、使用esc_pos_printer实现打印

1. 依赖配置

在pubspec.yaml中添加依赖:

dependencies:
  esc_pos_printer: ^4.1.0
  flutter_bluetooth_serial: ^0.4.0

2. 完整打印示例代码

import 'package:flutter_bluetooth_serial/flutter_bluetooth_serial.dart';
import 'package:esc_pos_printer/esc_pos_printer.dart';
import 'package:esc_pos_utils/esc_pos_utils.dart';

Future<void> printStoreReceipt() async {
  // 1. 获取已配对的蓝牙设备列表
  List<BluetoothDevice> bondedDevices = [];
  try {
    bondedDevices = await FlutterBluetoothSerial.instance.getBondedDevices();
  } catch (e) {
    print('获取蓝牙设备失败: $e');
    return;
  }

  // 2. 匹配目标打印机(用MAC地址匹配更精准)
  const targetPrinterMac = 'XX:XX:XX:XX:XX:XX'; // 替换为你的AP02 MAC地址
  BluetoothDevice? printerDevice;
  for (var device in bondedDevices) {
    if (device.address == targetPrinterMac) {
      printerDevice = device;
      break;
    }
  }

  if (printerDevice == null) {
    print('未找到目标打印机,请检查蓝牙配对状态');
    return;
  }

  // 3. 建立蓝牙连接
  BluetoothConnection? connection;
  try {
    connection = await BluetoothConnection.toAddress(printerDevice.address);
    print('打印机连接成功');
  } catch (e) {
    print('连接打印机失败: $e');
    return;
  }

  // 4. 构建并发送打印指令
  final profile = await CapabilityProfile.load();
  final printer = Printer(connection, PaperSize.mm80, profile);

  try {
    // 打印标题
    await printer.text('XX便利店', styles: const PosStyles(
      align: PosAlign.center,
      height: PosTextSize.size2,
      width: PosTextSize.size2,
      bold: true,
    ));
    await printer.text('------------------------------');
    
    // 打印商品明细
    await printer.text('矿泉水 x1    ¥3.00', styles: const PosStyles(align: PosAlign.left));
    await printer.text('面包 x2      ¥10.00', styles: const PosStyles(align: PosAlign.left));
    await printer.text('------------------------------');
    
    // 打印结算信息
    await printer.text('总计: ¥13.00', styles: const PosStyles(
      align: PosAlign.right,
      bold: true,
      height: PosTextSize.size1,
    ));
    await printer.text('支付方式: 微信支付', styles: const PosStyles(align: PosAlign.right));
    await printer.text('------------------------------');
    
    // 打印二维码(可选)
    await printer.qrcode('https://example.com/receipt/12345');
    
    // 走纸+切纸(部分设备需调整切纸指令)
    await printer.feed(3);
    await printer.cut();
    
    print('打印完成');
  } catch (e) {
    print('打印出错: $e');
  } finally {
    // 关闭连接
    connection?.dispose();
    print('已断开打印机连接');
  }
}

四、错误处理与调试要点

  • 权限问题:在AndroidManifest.xml中添加蓝牙权限,iOS在Info.plist中添加蓝牙访问权限描述,避免因权限不足无法扫描/连接设备
  • 连接超时:可以用Future.timeout给连接操作添加超时限制,比如BluetoothConnection.toAddress(...).timeout(const Duration(seconds: 5))
  • 指令无效:如果切纸、加粗等效果不生效,可查阅AP02官方ESC/POS手册,替换为设备支持的指令码
  • 配对异常:若无法找到设备,先在系统蓝牙列表确认已完成配对,重启打印机后重试

五、替代方案(使用blue_thermal_printer)

如果偏好blue_thermal_printer,核心逻辑类似,只需替换打印部分的API,示例片段:

import 'package:blue_thermal_printer/blue_thermal_printer.dart';

BlueThermalPrinter printer = BlueThermalPrinter.instance;

// 连接后打印
await printer.printCustom('XX便利店', 2, 1);
await printer.printNewLine();
await printer.printCustom('矿泉水 x1 ¥3.00', 1, 0);
await printer.cut();

内容的提问来源于stack exchange,提问作者Badriya Haddad

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.06 04:00:24