0

我正在使用Flutter Dart创建一个Android 应用程序,我对如何使用打印预览屏幕创建我们自己的定制设计(即需要垂直打印设计)并最终使用蓝牙热敏打印机打印该设计有点困惑。

换句话说,我只是想要如下图所示的结果。

在此处输入图像描述

所以我正在寻求一点帮助。提前致谢。

4

1 回答 1

0

在查看了其他插件 githubs 后,我使用了自己的自定义代码,因为我无法从这些插件中获取大部分命令。我使用 LAN 热敏打印机,但我确信这也适用于蓝牙。

https://www.starmicronics.com/support/Mannualfolder/dot_star_cm_en.pdf 我使用这个网站查看可以发送到打印机的命令,这只是从那里试错。希望您可以从那里获得工作所需的命令。

使用此插件连接打印机并尝试用他们的方法打印一些东西,看看它是否已连接。 https://pub.dev/packages/esc_pos_bluetooth

我的代码中的底部方法“printTicketViaLan()”是通过 LAN 发送的。所以为了使用蓝牙,我做了另一种方法“printUsingBluetooth()”,但我没有蓝牙打印机,所以我无法测试它。使用 ticket.rawBytes(bytes); 发送数据

下面是我尝试过并适用于我的打印机的命令。

PS。使用 LAN 方法,您不需要任何插件。

import 'dart:convert' show utf8;
import 'dart:io';
import 'package:flutter/material.dart';

class _MyHomePageState extends State<MyHomePage> {

  void _printOrder() async {
    const esc = '\x1B';
    const gs = '\x1D';
    const fs = '\x1C';
    
    const reset = '$esc@'; // Initialize printer
    const underline = '$esc-1';
    const noUnderline = '$esc-0';
    const whiteOnBlack = '${esc}4';
    const noWhiteOnBlack = '${esc}5';
    const doubleWide = '${esc}W1';
    const doubleHeight = '${esc}h1';
    const tripleWide = '${esc}W2';
    const tripleHeight = '${esc}h2';
    const cutPaper = '${esc}d3';
    const cutPaper = '${esc}d3';

    List<int> bytes = List<int>();
    bytes += reset.codeUnits;

    bytes += tripleWide.codeUnits;
    bytes += tripleHeight.codeUnits;
    bytes += utf8.encode('Some Headline \n');
    bytes += doubleWide.codeUnits;
    bytes += doubleHeight.codeUnits;
    bytes += utf8.encode('Subtitle');
    bytes += reset.codeUnits;

    
    bytes += utf8.encode('Normal Text \n');
    bytes += whiteOnBlack.codeUnits;
    bytes += utf8.encode('White Text with black background\n');
    bytes += reset.codeUnits;

    bytes += underline.codeUnits;
    bytes += noUnderline.codeUnits;
    bytes += utf8.encode('Underlined Text \n');
    
    bytes += cutPaper.codeUnits;
    

    printUsingBluetooth(bytes);

    printTicketViaLan(bytes);
  }

  void printUsingBluetooth(List<int> bytes) {
    PrinterBluetoothManager printerManager = PrinterBluetoothManager();
    printerManager.scanResults.listen((printers) async {
      // store found printers and choose the right one from a list
    });
    printerManager.startScan(Duration(seconds: 4));

    //Creating new ticket and using bytes to send.
    Ticket ticket = Ticket();
    ticket.rawBytes(bytes);
    //Not sure if isKanji must be set to true... Try both.
    ticket.rawBytes(bytes, isKanji: true);


    printerManager.selectPrinter(printer);
    final PosPrintResult res = await printerManager.printTicket(ticket);

    print('Print result: ${res.msg}');
  }

  void printTicketViaLan(List<int> bytes) async {
    Duration timeout = const Duration(seconds: 5);
    var result = await Socket.connect('192.168.0.34', 9100, timeout: timeout)
        .then((Socket socket) {
      socket.add(bytes);
      socket.destroy();
      return 'Data sent Success';
    }).catchError((dynamic e) {
      return 'Error sending Data';
    });
    print(result);
  }
于 2020-08-24T09:29:12.313 回答