huaji_bluetooth_print 1.6.4 copy "huaji_bluetooth_print: ^1.6.4" to clipboard
huaji_bluetooth_print: ^1.6.4 copied to clipboard

Flutter Bluetooth printing plugin for Gprinter-compatible ESC, TSC, and CPCL thermal printers.

pub package

Introduction #

Huaji Bluetooth Print is a Flutter plugin for Gprinter-compatible Bluetooth thermal printers on Android and iOS. It supports ESC receipt printing and TSC label printing on both platforms. CPCL printing is currently Android-only.

Underway(please suggest) #

[ ] print x,y positions
[ ] set paper size
[ ] more print examples

verison #

3.0.0(flutter 2.x)
2.0.0(flutter 1.12)
1.2.0(flutter 1.9)

Features #

Android iOS Description
scan Starts a scan for Bluetooth Low Energy devices.
connect Establishes a connection to the device.
disconnect Cancels an active or pending connection to the device.
state Stream of state changes for the Bluetooth Device.
print test message print device test message.
print text print custom text, support layout.
print image print image.
print qrcode print qrcode,support change size.
print barcode print barcode

Usage #

Example

To use this plugin :

  dependencies:
    flutter:
      sdk: flutter
    huaji_bluetooth_print: ^1.6.4
  • init a HuajiBluetoothPrint instance
import 'package:huaji_bluetooth_print/huaji_bluetooth_print.dart';
import 'package:huaji_bluetooth_print/bluetooth_print_model.dart';


HuajiBluetoothPrint bluetoothPrint = HuajiBluetoothPrint.instance;

scan #

// begin scan
bluetoothPrint.startScan(timeout: Duration(seconds: 4));

// get devices
StreamBuilder<List<BluetoothDevice>>(
    stream: bluetoothPrint.scanResults,
    initialData: [],
    builder: (c, snapshot) => Column(
      children: snapshot.data.map((d) => ListTile(
        title: Text(d.name??''),
        subtitle: Text(d.address),
        onTap: () async {
          setState(() {
            _device = d;
          });
        },
        trailing: _device!=null && _device.address == d.address?Icon(
          Icons.check,
          color: Colors.green,
        ):null,
      )).toList(),
    ),
  ),

connect #

await bluetoothPrint.connect(_device);

// Some label printers accept TSC commands but do not answer status queries.
await bluetoothPrint.connect(
  _device,
  fallbackCommand: BluetoothPrinterCommand.esc,
);

connect completes with true only after the native connection is ready and the printer command mode has been detected or resolved by fallbackCommand. It completes with false if the connection fails or times out. isConnected is true only when the printer is ready to accept print data.

On iOS, a device must come from the current scan results before it can be connected. The bundled GSDK static library contains a device slice only, so iOS Simulator builds are not supported.

disconnect #

await bluetoothPrint.disconnect();

listen state #

      bluetoothPrint.state.listen((state) {
      print('cur device status: $state');
# bluetooth_print

      switch (state) {
        case HuajiBluetoothPrint.CONNECTED:
          setState(() {
            _connected = true;
          });
          break;
        case HuajiBluetoothPrint.DISCONNECTED:
          setState(() {
            _connected = false;
          });
          break;
        default:
          break;
      }
    });

A new flutter plugin project.

    Map<String, dynamic> config = Map();
    List<LineText> list = List();
    list.add(LineText(type: LineText.TYPE_TEXT, content: 'A Title', weight: 1, align: LineText.ALIGN_CENTER,linefeed: 1));
    list.add(LineText(type: LineText.TYPE_TEXT, content: 'this is conent left', weight: 0, align: LineText.ALIGN_LEFT,linefeed: 1));
    list.add(LineText(type: LineText.TYPE_TEXT, content: 'this is conent right', align: LineText.ALIGN_RIGHT,linefeed: 1));
    list.add(LineText(linefeed: 1));
    list.add(LineText(type: LineText.TYPE_BARCODE, content: 'A12312112', size:10, align: LineText.ALIGN_CENTER, linefeed: 1));
    list.add(LineText(linefeed: 1));
    list.add(LineText(type: LineText.TYPE_QRCODE, content: 'qrcode i', size:10, align: LineText.ALIGN_CENTER, linefeed: 1));
    list.add(LineText(linefeed: 1));
## Getting Started

    ByteData data = await rootBundle.load("assets/images/guide3.png");
    List<int> imageBytes = data.buffer.asUint8List(data.offsetInBytes, data.lengthInBytes);
    String base64Image = base64Encode(imageBytes);
    list.add(LineText(type: LineText.TYPE_IMAGE, content: base64Image, align: LineText.ALIGN_CENTER, linefeed: 1));
This project is a starting point for a Flutter
[plug-in package](https://flutter.dev/developing-packages/),
a specialized package that includes platform-specific implementation code for
Android and/or iOS.

    try {
      await bluetoothPrint.printReceipt(config, list);
    } on PlatformException catch (error) {
      debugPrint('Print failed: $error');
    }

For help getting started with Flutter, view our online documentation, which offers tutorials, samples, guidance on mobile development, and a full API reference.

    Map<String, dynamic> config = Map();
    config['width'] = 40; // 标签宽度,单位mm
    config['height'] = 70; // 标签高度,单位mm
    config['gap'] = 2; // 标签间隔,单位mm
    config['copies'] = 2; // 连续打印份数,合并到一次TSC PRINT指令
    config['sound'] = true; // 默认发送蜂鸣器指令;设为false可关闭
    config['cashDrawer'] = false; // 默认不发送钱箱指令

    // x、y坐标位置,单位dpi,1mm=8dpi
    List<LineText> list = List();
    list.add(LineText(type: LineText.TYPE_TEXT, x:10, y:10, content: 'A Title'));
    list.add(LineText(type: LineText.TYPE_TEXT, x:10, y:40, content: 'this is content'));
    list.add(LineText(type: LineText.TYPE_QRCODE, x:10, y:70, content: 'qrcode i\n'));
    list.add(LineText(type: LineText.TYPE_BARCODE, x:10, y:190, content: 'qrcode i\n'));

    List<LineText> list1 = List();
    ByteData data = await rootBundle.load("assets/images/guide3.png");
    List<int> imageBytes = data.buffer.asUint8List(data.offsetInBytes, data.lengthInBytes);
    String base64Image = base64Encode(imageBytes);
    list1.add(LineText(type: LineText.TYPE_IMAGE, x:10, y:10, content: base64Image,));

    await bluetoothPrint.printLabel(config, list);
    await bluetoothPrint.printLabel(config, list1);

Print futures complete after the native write has been attempted. Always await them so connection and write failures can be handled by the app.

Troubleshooting #

ios import third party library

[Please Read link: https://stackoverflow.com/questions/19189463/cocoapods-podspec-issue)
*.podspec add:

# .a filename must begin with lib, eg. 'libXXX.a'
s.vendored_libraries = '**/*.a'

error:'State restoration of CBCentralManager is only allowed for applications that have specified the "bluetooth-central" background mode'

info.plist add:

<key>NSBluetoothAlwaysUsageDescription</key>
<string>Allow App use bluetooth?</string>
<key>NSBluetoothPeripheralUsageDescription</key>
<string>Allow App use bluetooth?</string>
<key>UIBackgroundModes</key>
<array>
    <string>bluetooth-central</string>
    <string>bluetooth-peripheral</string>
</array>

FAQ Support #

you can join this QQ group, feedback your problem

Thanks For #

0
likes
140
points
241
downloads

Documentation

API reference

Publisher

unverified uploader

Weekly Downloads

Flutter Bluetooth printing plugin for Gprinter-compatible ESC, TSC, and CPCL thermal printers.

Repository (GitHub)
View/report issues

License

MIT (license)

Dependencies

flutter, json_annotation, rxdart

More

Packages that depend on huaji_bluetooth_print

Packages that implement huaji_bluetooth_print