0

I am creating one Android app using Flutter Dart and I am little bit confused about how to create our own customized design (i.e. need vertical print design) with Print Preview screen and finally print that design using Bluetooth Thermal Printer.

In other words simply I am wanting the results like bellow attached image.

enter image description here

So I am looking for little bit help. Thanks in Advance.

1 Answers1

0

I used my own custom code after looking through other plugins githubs, because i couldn't get most of the commands to work from those plugins. I use a LAN thermal printer but im sure this will work for Bluetooth too.

https://www.starmicronics.com/support/Mannualfolder/dot_star_cm_en.pdf I used this website to see the commands you can send to the printer and it was just trial and error from there. Hopefully you can get the command you need to work from there.

Use this plugin to connect the printer and try printing something with their method, to see if it is connected. https://pub.dev/packages/esc_pos_bluetooth

The bottom method "printTicketViaLan()" in my code is sending via LAN. So to use Bluetooth i have made another method "printUsingBluetooth()" but i don't have a Bluetooth printer, so I can't test it. Send the data using ticket.rawBytes(bytes);

Bellow are the commands i have tried and works for my printer.

PS. With the LAN method you don't need any plugins.

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);
  }
Jacob Welin
  • 161
  • 1
  • 7