如何从 Flutter 打印到热敏打印机?

问题描述 投票:0回答:1

我正在尝试通过蓝牙从 Flutter 应用程序将 QR 码(或者此时实际上只是任何内容)打印到热敏打印机。我使用 esc_pos_utils 生成打印命令字节,使用 flutter_blue_plus 管理蓝牙连接。我已经成功连接到打印机,扫描蓝牙服务,并找到可写特征。注意 - 下面的 UUID 是该特定打印机 (Phomemo M120) 上唯一实际可写特征的 UUID。

connectDevice(BluetoothDevice device) async {
    // listen for disconnection
    device.connectionState.listen((BluetoothConnectionState state) async {
      if (state == BluetoothConnectionState.disconnected) {
        // typically, start a periodic timer that tries to periodically reconnect.
        // Note: you must always re-discover services after disconnection!
        // TODO - do something
      }
    });

    // Connect to the device
    // Note: You should always call `connectionState.listen` before you call connect!
    await device.connect();
    _connectedDevice = device;

    // Yank the MTU
    _mtu = await device.mtu.first;

    // Note: You must call discoverServices after every connection!
    List<BluetoothService> services = await device.discoverServices();
    _services = services;

    services.forEach((service) async {
      // Reads all characteristics
      var characteristics = service.characteristics;
      for (BluetoothCharacteristic c in characteristics) {
        print(c.toString());
        if (c.characteristicUuid.toString() ==
                "0000ff02-0000-1000-8000-00805f9b34fb" &&
            service.serviceUuid.toString() ==
                "0000ff00-0000-1000-8000-00805f9b34fb") {
          _characteristic = c;
        }
        // List<int> value = await c.read();
        // print(value);
      }
    });
  }

当我尝试生成字节并打印时,打印机确实做出了反应,它只是输出了几个标签,而没有实际打印任何内容。

final profile = await CapabilityProfile.load();
    final generator = Generator(PaperSize.mm58, profile);
    List<int> bytes = [];

    bytes += generator.text("abc TEST 123");
    bytes += generator.qrcode('example.com');
    await _characteristic!.write(bytes, allowLongWrite: true);

我还尝试过使用 TSPL 命令,根据制造商的说法,该打印机也支持该命令。 Phomemo m120手册(可能需要向下滚动一点,但它声称支持ESC/POS/TSPL指令集。

List<int> printText(String text) {
    List<String> commands = [];

    // Set fixed label size
    commands.add('SIZE 40 mm,30 mm\n');

    // Clear the buffer
    commands.add('CLS\n');

    // Add text, we assume standard text properties
    commands.add('TEXT 10,10,"3",0,1,1,"$text"\n');

    // Print the label
    commands.add('PRINT 1\n');

    // END
    commands.add('END\n');

    // Convert commands to bytes
    List<int> bytes = commands
        .map((command) => command.codeUnits)
        .expand((codeUnit) => codeUnit)
        .toList();

    return bytes;
  }

但同样的事情,打印机只是送出几个标签,而不打印任何东西。我知道打印机没有使用制造商应用程序时出现的故障,它确实打印成功。知道我做错了什么吗?

flutter printing bluetooth thermal-printer
1个回答
0
投票

尝试:

bluetooth.writeBytes(utf8.encode("abc TEST 123"));

而是:

await _characteristic!.write(bytes, allowLongWrite: true);

或与:

String string = "abc TEST 123";
bytes = string.codeUnits;

而是:

bytes += generator.text("abc TEST 123");

确保您已连接到打印机

© www.soinside.com 2019 - 2024. All rights reserved.