blue_thermal_printer 1.0.8

blue_thermal_printer #

https://pub.dev/packages/blue_thermal_printer

A new Flutter plugin for connecting to thermal printer via bluetooth (Android Only), this plugin is still under development Note: Migrate your project to AndroidX

Getting Started #

  1. Depend on it

Add this to your package's pubspec.yaml file:

dependencies:
  blue_thermal_printer: ^any
  1. Install it

You can install packages from the command line:

with Flutter:

$ flutter packages get

Alternatively, your editor might support flutter packages get. Check the docs for your editor to learn more.

  1. Import it

Now in your Dart code, you can use:

 import 'package:blue_thermal_printer/blue_thermal_printer.dart';

EXAMPLE:

import 'dart:io';
import 'dart:typed_data';
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:blue_thermal_printer/blue_thermal_printer.dart';
import 'package:flutter/services.dart';
import 'package:path_provider/path_provider.dart';

void main() => runApp(new MyApp());

class MyApp extends StatefulWidget {
 @override
 _MyAppState createState() => new _MyAppState();
}

class _MyAppState extends State<MyApp> {

  BlueThermalPrinter bluetooth = BlueThermalPrinter.instance;

  List<BluetoothDevice> _devices = [];
  BluetoothDevice _device;
  bool _connected = false;
  bool _pressed = false;
  String pathImage;

  @override
  void initState() {
   super.initState();
   initPlatformState();
   initSavetoPath();
  }

 initSavetoPath()async{
    //read and write
    //image max 300px X 300px
    final filename = 'yourlogo.png';
    var bytes = await rootBundle.load("assets/images/yourlogo.png");
    String dir = (await getApplicationDocumentsDirectory()).path;
    writeToFile(bytes,'$dir/$filename');
    setState(() {
     pathImage='$dir/$filename';
   });
 }
  Future<void> initPlatformState() async {
    List<BluetoothDevice> devices = [];

   try {
     devices = await bluetooth.getBondedDevices();
   } on PlatformException {
     // TODO - Error
   }

   bluetooth.onStateChanged().listen((state) {
     switch (state) {
        case BlueThermalPrinter.CONNECTED:
          setState(() {
            _connected = true;
            _pressed = false;
         });
         break;
        case BlueThermalPrinter.DISCONNECTED:
         setState(() {
           _connected = false;
           _pressed = false;
          });
         break;
        default:
          print(state);
          break;
     }
   });

    if (!mounted) return;
   setState(() {
      _devices = devices;
   });
  }

 @override
 Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Blue Thermal Printer'),
       ),
        body: Container(
         child: ListView(
           children: <Widget>[
             Padding(
               padding: const EdgeInsets.fromLTRB(10.0, 10.0, 10.0, 0.0),
               child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                 children: <Widget>[
                   Text(
                      'Device:',
                     style: TextStyle(
                        fontWeight: FontWeight.bold,
                     ),
                    ),
                   DropdownButton(
                      items: _getDeviceItems(),
                      onChanged: (value) => setState(() => _device = value),
                      value: _device,
                   ),
                   RaisedButton(
                      onPressed:
                     _pressed ? null : _connected ? _disconnect : _connect,
                      child: Text(_connected ? 'Disconnect' : 'Connect'),
                    ),
                 ],
               ),
             ),
              Padding(
                padding: const EdgeInsets.only(left: 10.0, right: 10.0, top: 50),
                child:  RaisedButton(
                  onPressed: _connected ? _tesPrint : null,
                 child: Text('TesPrint'),
               ),
             ),
            ],
         ),
        ),
     ),
   );
 }
  List<DropdownMenuItem<BluetoothDevice>> _getDeviceItems() {
    List<DropdownMenuItem<BluetoothDevice>> items = [];
   if (_devices.isEmpty) {
     items.add(DropdownMenuItem(
       child: Text('NONE'),
     ));
    } else {
     _devices.forEach((device) {
       items.add(DropdownMenuItem(
        child: Text(device.name),
          value: device,
       ));
     });
   }
   return items;
  }
 void _connect() {
    if (_device == null) {
     show('No device selected.');
   } else {
      bluetooth.isConnected.then((isConnected) {
       if (!isConnected) {
          bluetooth.connect(_device).catchError((error) {
           setState(() => _pressed = false);
         });
         setState(() => _pressed = true);
       }
      });
    }
 }
  void _disconnect() {
    bluetooth.disconnect();
    setState(() => _pressed = true);
 }

//write to app path
 Future<void> writeToFile(ByteData data, String path) {
    final buffer = data.buffer;
    return new File(path).writeAsBytes(
        buffer.asUint8List(data.offsetInBytes, data.lengthInBytes));
 }

  void _tesPrint() async {
    //SIZE
    // 0- normal size text
    // 1- only bold text
   // 2- bold with medium text
   // 3- bold with large text
   //ALIGN
   // 0- ESC_ALIGN_LEFT
   // 1- ESC_ALIGN_CENTER
    // 2- ESC_ALIGN_RIGHT
    bluetooth.isConnected.then((isConnected) {
      if (isConnected) {
        bluetooth.printCustom("HEADER",3,1);
        bluetooth.printNewLine();
        bluetooth.printImage(pathImage);
        bluetooth.printNewLine();
        bluetooth.printLeftRight("LEFT", "RIGHT",0);
        bluetooth.printLeftRight("LEFT", "RIGHT",1);
        bluetooth.printNewLine();
        bluetooth.printLeftRight("LEFT", "RIGHT",2);
        bluetooth.printCustom("Body left",1,0);
        bluetooth.printCustom("Body right",0,2);
        bluetooth.printNewLine();
        bluetooth.printCustom("Terimakasih",2,1);
        bluetooth.printNewLine();
        bluetooth.printQRcode("Insert Your Own Text to Generate");
        bluetooth.printNewLine();
        bluetooth.printNewLine();
        bluetooth.paperCut();
        }
   });
  }
 Future show(
      String message, {
       Duration duration: const Duration(seconds: 3),
      }) async {
    await new Future.delayed(new Duration(milliseconds: 100));
   Scaffold.of(context).showSnackBar(
      new SnackBar(
        content: new Text(
          message,
         style: new TextStyle(
            color: Colors.white,
          ),
       ),
        duration: duration,
      ),
    );
 }
}

1.0.8 #

*fix crash with "Methods marked with @UiThread must be executed on the main thread, thanks to ricardochen

1.0.7 #

  • change documentation

1.0.6 #

  • add size to printleftright

1.0.5 #

  • fix bug

1.0.4 #

  • fix bug

1.0.3 #

  • Add print left + right

1.0.2 #

  • Add Endline in QRCODE

1.0.1 #

  • Add Doc.

1.0.0 #

  • initial release.

example/README.md

blue_thermal_printer_example #

Demonstrates how to use the blue_thermal_printer plugin.

Getting Started #

import 'dart:io';
import 'dart:typed_data';
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:blue_thermal_printer/blue_thermal_printer.dart';
import 'package:flutter/services.dart';
import 'package:path_provider/path_provider.dart';

void main() => runApp(new MyApp());

class MyApp extends StatefulWidget {
 @override
 _MyAppState createState() => new _MyAppState();
}

class _MyAppState extends State<MyApp> {

  BlueThermalPrinter bluetooth = BlueThermalPrinter.instance;

  List<BluetoothDevice> _devices = [];
  BluetoothDevice _device;
  bool _connected = false;
  bool _pressed = false;
  String pathImage;

  @override
  void initState() {
   super.initState();
   initPlatformState();
   initSavetoPath();
  }

 initSavetoPath()async{
    //read and write
    //image max 300px X 300px
    final filename = 'yourlogo.png';
    var bytes = await rootBundle.load("assets/images/yourlogo.png");
    String dir = (await getApplicationDocumentsDirectory()).path;
    writeToFile(bytes,'$dir/$filename');
    setState(() {
     pathImage='$dir/$filename';
   });
 }
  Future<void> initPlatformState() async {
    List<BluetoothDevice> devices = [];

   try {
     devices = await bluetooth.getBondedDevices();
   } on PlatformException {
     // TODO - Error
   }

   bluetooth.onStateChanged().listen((state) {
     switch (state) {
        case BlueThermalPrinter.CONNECTED:
          setState(() {
            _connected = true;
            _pressed = false;
         });
         break;
        case BlueThermalPrinter.DISCONNECTED:
         setState(() {
           _connected = false;
           _pressed = false;
          });
         break;
        default:
          print(state);
          break;
     }
   });

    if (!mounted) return;
   setState(() {
      _devices = devices;
   });
  }

 @override
 Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Blue Thermal Printer'),
       ),
        body: Container(
         child: ListView(
           children: <Widget>[
             Padding(
               padding: const EdgeInsets.fromLTRB(10.0, 10.0, 10.0, 0.0),
               child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                 children: <Widget>[
                   Text(
                      'Device:',
                     style: TextStyle(
                        fontWeight: FontWeight.bold,
                     ),
                    ),
                   DropdownButton(
                      items: _getDeviceItems(),
                      onChanged: (value) => setState(() => _device = value),
                      value: _device,
                   ),
                   RaisedButton(
                      onPressed:
                     _pressed ? null : _connected ? _disconnect : _connect,
                      child: Text(_connected ? 'Disconnect' : 'Connect'),
                    ),
                 ],
               ),
             ),
              Padding(
                padding: const EdgeInsets.only(left: 10.0, right: 10.0, top: 50),
                child:  RaisedButton(
                  onPressed: _connected ? _tesPrint : null,
                 child: Text('TesPrint'),
               ),
             ),
            ],
         ),
        ),
     ),
   );
 }
  List<DropdownMenuItem<BluetoothDevice>> _getDeviceItems() {
    List<DropdownMenuItem<BluetoothDevice>> items = [];
   if (_devices.isEmpty) {
     items.add(DropdownMenuItem(
       child: Text('NONE'),
     ));
    } else {
     _devices.forEach((device) {
       items.add(DropdownMenuItem(
        child: Text(device.name),
          value: device,
       ));
     });
   }
   return items;
  }
 void _connect() {
    if (_device == null) {
     show('No device selected.');
   } else {
      bluetooth.isConnected.then((isConnected) {
       if (!isConnected) {
          bluetooth.connect(_device).catchError((error) {
           setState(() => _pressed = false);
         });
         setState(() => _pressed = true);
       }
      });
    }
 }
  void _disconnect() {
    bluetooth.disconnect();
    setState(() => _pressed = true);
 }

//write to app path
 Future<void> writeToFile(ByteData data, String path) {
    final buffer = data.buffer;
    return new File(path).writeAsBytes(
        buffer.asUint8List(data.offsetInBytes, data.lengthInBytes));
 }

  void _tesPrint() async {
    //SIZE
    // 0- normal size text
    // 1- only bold text
   // 2- bold with medium text
   // 3- bold with large text
   //ALIGN
   // 0- ESC_ALIGN_LEFT
   // 1- ESC_ALIGN_CENTER
    // 2- ESC_ALIGN_RIGHT
    bluetooth.isConnected.then((isConnected) {
      if (isConnected) {
        bluetooth.printCustom("HEADER",3,1);
        bluetooth.printNewLine();
        bluetooth.printImage(pathImage);
        bluetooth.printNewLine();
        bluetooth.printLeftRight("LEFT", "RIGHT",0);
        bluetooth.printLeftRight("LEFT", "RIGHT",1);
        bluetooth.printNewLine();
        bluetooth.printLeftRight("LEFT", "RIGHT",2);
        bluetooth.printCustom("Body left",1,0);
        bluetooth.printCustom("Body right",0,2);
        bluetooth.printNewLine();
        bluetooth.printCustom("Terimakasih",2,1);
        bluetooth.printNewLine();
        bluetooth.printQRcode("Insert Your Own Text to Generate");
        bluetooth.printNewLine();
        bluetooth.printNewLine();
        bluetooth.paperCut();
   });
  }
 Future show(
      String message, {
       Duration duration: const Duration(seconds: 3),
      }) async {
    await new Future.delayed(new Duration(milliseconds: 100));
   Scaffold.of(context).showSnackBar(
      new SnackBar(
        content: new Text(
          message,
         style: new TextStyle(
            color: Colors.white,
          ),
       ),
        duration: duration,
      ),
    );
 }
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  blue_thermal_printer: ^1.0.8

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter pub get

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:blue_thermal_printer/blue_thermal_printer.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
79
Health:
Code health derived from static analysis. [more]
99
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
89
Learn more about scoring.

We analyzed this package on Aug 21, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.4.0
  • pana: 0.12.19
  • Flutter: 1.7.8+hotfix.4

Platforms

Detected platforms: Flutter

References Flutter, and has no conflicting libraries.

Health issues and suggestions

Document public APIs. (-0.29 points)

40 out of 41 API elements have no dartdoc comment.Providing good documentation for libraries, classes, functions, and other API elements improves code readability and helps developers find and use your API.

Fix lib/blue_thermal_printer.dart. (-0.50 points)

Analysis of lib/blue_thermal_printer.dart reported 1 hint:

line 34 col 35: This function has a return type of 'Future', but doesn't end with a return statement.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
flutter 0.0.0
Transitive dependencies
collection 1.14.11 1.14.12
meta 1.1.6 1.1.7
sky_engine 0.0.99
typed_data 1.1.6
vector_math 2.0.8
Dev dependencies
flutter_test