android - 从 dart/flutter 在 POS USB 打印机上打印
问题描述
在 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
解决方案
事实证明,一个小小的改变就能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
块,则字节将发送到打印机。
我对文档的理解claimInterface
是,在写入设备之前需要此方法。
迁移到健全的 null 安全性的包的一个分支,现在在这里。
推荐阅读
- java - 如何在android中自定义单选按钮
- reactjs - 如何解决 Jest 遇到的意外令牌
- ansible - Ansible:如何过滤 dict2items 并仅为匹配的值运行 playbook
- java - 测试扩展抽象类和接口的 Spring Service 类
- python - 比较两个 Python 字典:CS50 PSET6 DNA
- docker-swarm - Docker Swarm Secrets + Portainer
- r - 将带有自己数据框的 geom_bar 添加到现有的 ggplot2
- java - Wildfly 数据源通过 VPN 到 MS SQL Server
- csv - 如何在groovy中比较2个CSV文件只有前6个标志
- python - 微分熵是用 scipy.stats 中的integrate.quad 计算的?