【问题标题】:Printing on POS USB printer from dart/flutter从 dart/flutter 在 POS USB 打印机上打印
【发布时间】:2021-09-27 06:12:42
【问题描述】:

在 Android Flutter 项目中,我需要在 USB POS 打印机上进行打印,并且为了进行测试,我有一台 Epson TM-T20。发送原始字节将是理想的。 flutter_usb_write 包不起作用(write 返回 false)并且它的状态有点令人担忧(还没有健全的 null 安全性,主页是克罗地亚语并且没有问题跟踪器)。

还有其他选择吗?

Future<void> printOnPOS(String text) async {
  // text = String.fromCharCodes([27, 64]) + text; // Initialize printer
  text += "\r\n";
  try {
    var flutterUsbWrite = FlutterUsbWrite();
    List<UsbDevice> devices = await flutterUsbWrite.listDevices();
    print("devices: $devices");
    var device = devices[1]; // this picks the printer, checked correct
    var port = await flutterUsbWrite.open(
      vendorId: device.vid,
      productId: device.pid,
    );
    print("port: $port");
    var rw = await flutterUsbWrite.write(Uint8List.fromList(text.codeUnits));
    print("rw: $rw");
    await flutterUsbWrite.close();
  } on PlatformException catch (e) {
    print(e.message);
  }
}

输出:

I/flutter ( 7763): devices: [UsbDevice: e0f-3 VMware Virtual USB Mouse, VMware null, UsbDevice: 4b8-e03 TM-T20, EPSON 405551460005550000]
I/flutter ( 7763): port: UsbDevice: 4b8-e03 TM-T20, EPSON 405551460005550000
I/flutter ( 7763): rw: false
D/UsbDeviceConnectionJNI( 7763): close

【问题讨论】:

    标签: android flutter dart


    【解决方案1】:

    事实证明,一个小改动就能让flutter_usb_write 工作。

    FlutterUsbWritePlugin.java 中,我们在方法write 中有这个:

          if (this.ep != null && this.mInterface != null && this.m_Connection != null) {
            transferResult = this.m_Connection.bulkTransfer(this.ep, bytes, bytes.length, 0);
          } else {
            if (this.m_Connection.claimInterface(this.mInterface, true)) {
              transferResult = this.m_Connection.bulkTransfer(this.ep, bytes, bytes.length, 0);
            }
          }
    

    if 中的表达式计算结果为 true,然后对 bulkTransfer 的调用失败。

    如果我们改为强制执行 else 块,则字节将发送到打印机。

    我对@9​​87654330@的文档的理解是,在写入设备之前需要这个方法。

    包的一个分支,迁移到可靠的 null 安全性,现在是 here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-11-16
      • 1970-01-01
      • 1970-01-01
      • 2022-08-03
      • 2019-11-23
      • 2012-10-11
      相关资源
      最近更新 更多