Flutter蓝牙POS打印机插件smart_bluetooth_pos_printer的使用

Flutter蓝牙POS打印机插件smart_bluetooth_pos_printer的使用

Pub Version

一个库用于发现打印机并发送打印机命令。

该库允许向不同平台(如Android、iOS、Windows)和不同接口(如蓝牙和BLE)的打印机发送ESC命令。

flutter_pos_printer_platform 启发。

主要功能

  • 支持Android、iOS和Windows
  • 扫描蓝牙设备
  • 发送原始 List<int> bytes 数据到设备,查看此库以生成ESC/POS命令 flutter_esc_pos_utils

特性

Android iOS Windows 描述
蓝牙经典接口 允许连接经典蓝牙设备。
蓝牙低功耗(BLE)接口 允许连接支持BLE的蓝牙设备。
扫描 开始扫描仅蓝牙设备或网络设备(Android/iOS)。
连接 建立与设备的连接。
断开连接 取消与设备的活动或待处理连接。
状态流 设备蓝牙状态变化流。
打印 打印字节。

开始使用

完整的示例请查看/example文件夹。以下是使用该库的一些重要部分的代码。

通过 flutter_esc_pos_utils 生成打印数据。

import 'package:esc_pos_utils/esc_pos_utils.dart';

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

bytes += generator.text('Test Print', styles: const PosStyles(align: PosAlign.center));
bytes += generator.text('Product 1');
bytes += generator.text('Product 2');

Android

允许连接蓝牙(经典和BLE)、USB和网络设备。

更改Android的minSdkVersion

smart_bluetooth_pos_printer 仅兼容从Android SDK版本21开始的版本,因此在android/app/build.gradle中应进行更改:

在build.gradle中设置:

defaultConfig {
    ...
    minSdkVersion 21
    targetSdkVersion 31
    ...
}

选择设备类型 <PrinterType> (蓝牙、USB、网络)

如果选择蓝牙,可以发送可选参数:

  • isBle -> 允许连接支持此技术的蓝牙设备
  • autoconnect -> 当设备状态为None时允许重新连接

iOS

允许连接蓝牙(BLE)和网络设备。

如何使用

初始化PrinterManager实例

import 'package:smart_bluetooth_pos_printer/smart_bluetooth_pos_printer.dart';

var printerManager = PrinterManager.instance;

扫描

var devices = [];
_scan({bool isBle = false}) {
    // 查找打印机
    PrinterManager.instance.discovery(isBle: isBle).listen((device) {
        devices.add(device);
    });
}

连接

_connectDevice(PrinterDevice selectedPrinter, {bool reconnect = false, bool isBle = false}) async {
    await printerManager.connect(
        model: BluetoothPrinterInput(
            name: selectedPrinter!.deviceName,
            address: selectedPrinter!.address!,
            isBle: selectedPrinter!.isBle ?? false,
            autoConnect: _reconnect));
  }

断开连接

_disconnectDevice() async {
    await PrinterManager.instance.disconnect();
}

监听蓝牙状态

PrinterManager.instance.stateBluetooth.listen((status) {
  log(' ----------------- status bt $status ------------------ ');
});

发送字节打印

_sendBytesToPrint(List<int> bytes) async { 
  PrinterManager.instance.send(bytes: bytes);
}

排错

错误:‘State restoration of CBCentralManager is only allowed for applications that have specified the “bluetooth-central” background mode’

在info.plist中添加:

<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>

支持我

如果您认为本项目对您的开发有所帮助,您可以支持该项目,任何支持都是十分珍贵的。

Paypal


示例代码

import 'dart:async';
import 'dart:log';
import 'dart:io';
import 'package:esc_pos_utils/esc_pos_utils.dart';
import 'package:flutter/material.dart';
import 'package:smart_bluetooth_pos_printer/smart_bluetooth_pos_printer.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({Key? key}) : super(key: key);

  [@override](/user/override)
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  var _isBle = false;
  var _reconnect = false;
  var _isConnected = false;
  var printerManager = PrinterManager.instance;
  var devices = <BluetoothPrinter>[];
  StreamSubscription<List<PrinterDevice>>? _subscription;
  StreamSubscription<BTStatus>? _subscriptionBtStatus;
  BTStatus _currentStatus = BTStatus.none;
  // _currentUsbStatus 仅在Android上支持
  // ignore: unused_field
  List<int>? pendingTask;
  BluetoothPrinter? selectedPrinter;

  [@override](/user/override)
  void initState() {
    super.initState();

    _scan();

    // 订阅监听蓝牙连接状态变化
    _subscriptionBtStatus =
        PrinterManager.instance.stateBluetooth.listen((status) {
      log(' ----------------- status bt $status ------------------ ');
      _currentStatus = status;
      if (status == BTStatus.connected) {
        setState(() {
          _isConnected = true;
        });
      }
      if (status == BTStatus.none) {
        setState(() {
          _isConnected = false;
        });
      }
      if (status == BTStatus.connected && pendingTask != null) {
        if (Platform.isAndroid) {
          Future.delayed(const Duration(milliseconds: 1000), () {
            PrinterManager.instance.send(bytes: pendingTask!);
            pendingTask = null;
          });
        } else if (Platform.isIOS) {
          PrinterManager.instance.send(bytes: pendingTask!);
          pendingTask = null;
        }
      }
    });
  }

  [@override](/user/override)
  void dispose() {
    _subscription?.cancel();
    _subscriptionBtStatus?.cancel();

    super.dispose();
  }

  // 根据PrinterType扫描设备
  void _scan() {
    printerManager.startScan(timeout: const Duration(seconds: 4), isBle: true);
    _subscription = printerManager.scanResults().listen((results) {
      devices.clear();
      for (PrinterDevice r in results) {
        devices.add(BluetoothPrinter(
          deviceName: r.name,
          address: r.address,
          isBle: _isBle,
        ));
      }
      print(devices.length);
      setState(() {});
    });
  }

  void selectDevice(BluetoothPrinter device) async {
    if (selectedPrinter != null) {
      if (device.address != selectedPrinter!.address) {
        await PrinterManager.instance.disconnect();
      }
    }

    selectedPrinter = device;
    setState(() {});
  }

  Future _printReceiveTest() async {
    List<int> bytes = [];

    // Xprinter XP-N160I
    final profile = await CapabilityProfile.load(name: 'XP-N160I');
    // PaperSize.mm80 或 PaperSize.mm58
    final generator = Generator(PaperSize.mm80, profile);
    bytes += generator.setGlobalCodeTable('CP1252');
    bytes += generator.text('Test Print',
        styles: const PosStyles(align: PosAlign.center));
    bytes += generator.text('Product 1');
    bytes += generator.text('Product 2');

    _printEscPos(bytes, generator);
  }

  /// 打印票据
  void _printEscPos(List<int> bytes, Generator generator) async {
    if (selectedPrinter == null) return;
    var bluetoothPrinter = selectedPrinter!;

    bytes += generator.cut();
    await printerManager.connect(
        model: BluetoothPrinterInput(
            name: bluetoothPrinter.deviceName,
            address: bluetoothPrinter.address!,
            isBle: bluetoothPrinter.isBle ?? false,
            autoConnect: _reconnect));
    pendingTask = null;
    if (Platform.isAndroid) pendingTask = bytes;

    if (Platform.isAndroid) {
      if (_currentStatus == BTStatus.connected) {
        printerManager.send(bytes: bytes);
        pendingTask = null;
      }
    } else {
      printerManager.send(bytes: bytes);
    }
  }

  // 连接设备
  _connectDevice() async {
    _isConnected = false;
    if (selectedPrinter == null) return;
    await printerManager.connect(
        model: BluetoothPrinterInput(
            name: selectedPrinter!.deviceName,
            address: selectedPrinter!.address!,
            isBle: selectedPrinter!.isBle ?? false,
            autoConnect: _reconnect));

    setState(() {});
  }

  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Flutter Pos Plugin Platform example app'),
        ),
        body: Center(
          child: Container(
            height: double.infinity,
            constraints: const BoxConstraints(maxWidth: 400),
            child: SingleChildScrollView(
              padding: EdgeInsets.zero,
              child: Column(
                children: [
                  Padding(
                    padding: const EdgeInsets.all(8.0),
                    child: Row(
                      children: [
                        Expanded(
                          child: ElevatedButton(
                            onPressed: selectedPrinter == null || _isConnected
                                ? null
                                : () {
                                    _connectDevice();
                                  },
                            child: const Text("Connect",
                                textAlign: TextAlign.center),
                          ),
                        ),
                        const SizedBox(width: 8),
                        Expanded(
                          child: ElevatedButton(
                            onPressed: selectedPrinter == null || !_isConnected
                                ? null
                                : () {
                                    if (selectedPrinter != null) {
                                      printerManager.disconnect();
                                      setState(() {
                                        _isConnected = false;
                                      });
                                    }
                                  },
                            child: const Text("Disconnect",
                                textAlign: TextAlign.center),
                          ),
                        ),
                      ],
                    ),
                  ),
                  Visibility(
                    visible: Platform.isAndroid,
                    child: SwitchListTile.adaptive(
                      contentPadding:
                          const EdgeInsets.only(bottom: 20.0, left: 20),
                      title: const Text(
                        "This device supports ble (low energy)",
                        textAlign: TextAlign.start,
                        style: TextStyle(fontSize: 19.0),
                      ),
                      value: _isBle,
                      onChanged: (bool? value) {
                        setState(() {
                          _isBle = value ?? false;
                          _isConnected = false;
                          selectedPrinter = null;
                          _scan();
                        });
                      },
                    ),
                  ),
                  Visibility(
                    visible: Platform.isAndroid,
                    child: SwitchListTile.adaptive(
                      contentPadding:
                          const EdgeInsets.only(bottom: 20.0, left: 20),
                      title: const Text(
                        "reconnect",
                        textAlign: TextAlign.start,
                        style: TextStyle(fontSize: 19.0),
                      ),
                      value: _reconnect,
                      onChanged: (bool? value) {
                        setState(() {
                          _reconnect = value ?? false;
                        });
                      },
                    ),
                  ),
                  Column(
                      children: devices
                          .map(
                            (device) => ListTile(
                              title: Text('${device.deviceName}'),
                              onTap: () {
                                // do something
                                selectDevice(device);
                              },
                              leading: selectedPrinter != null &&
                                      ((device.address != null &&
                                          selectedPrinter!.address ==
                                              device.address))
                                  ? const Icon(
                                      Icons.check,
                                      color: Colors.green,
                                    )
                                  : null,
                              trailing: OutlinedButton(
                                onPressed: selectedPrinter == null ||
                                        device.deviceName !=
                                            selectedPrinter?.deviceName
                                    ? null
                                    : () async {
                                        _printReceiveTest();
                                      },
                                child: const Padding(
                                  padding: EdgeInsets.symmetric(
                                      vertical: 2, horizontal: 20),
                                  child: Text("Print test ticket",
                                      textAlign: TextAlign.center),
                                ),
                              ),
                            ),
                          )
                          .toList()),
                ],
              ),
            ),
          ),
        ),
      ),
    );
  }
}

class BluetoothPrinter {
  int? id;
  String? deviceName;
  String? address;
  bool? isBle;

  bool? state;

  BluetoothPrinter(
      {this.deviceName, this.address, this.state, this.isBle = false});
}

更多关于Flutter蓝牙POS打印机插件smart_bluetooth_pos_printer的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter蓝牙POS打印机插件smart_bluetooth_pos_printer的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


当然,下面是一个关于如何使用Flutter蓝牙POS打印机插件smart_bluetooth_pos_printer的代码示例。这个示例将展示如何初始化蓝牙打印机、搜索设备、连接打印机以及发送打印命令。

首先,确保你已经在pubspec.yaml文件中添加了smart_bluetooth_pos_printer依赖:

dependencies:
  flutter:
    sdk: flutter
  smart_bluetooth_pos_printer: ^latest_version # 替换为最新版本号

然后,运行flutter pub get来获取依赖。

接下来,编写Flutter代码:

import 'package:flutter/material.dart';
import 'package:smart_bluetooth_pos_printer/smart_bluetooth_pos_printer.dart';

void main() {
  runApp(MyApp());
}

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

class _MyAppState extends State<MyApp> {
  SmartBluetoothPosPrinter? _printer;
  List<BluetoothDevice> _devices = [];
  BluetoothDevice? _connectedDevice;

  @override
  void initState() {
    super.initState();
    _printer = SmartBluetoothPosPrinter();
    _printer!.scanDevices().listen((devices) {
      setState(() {
        _devices = devices;
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Bluetooth POS Printer Demo'),
        ),
        body: Column(
          children: [
            Expanded(
              child: ListView.builder(
                itemCount: _devices.length,
                itemBuilder: (context, index) {
                  return ListTile(
                    title: Text(_devices[index].name ?? 'Unknown Device'),
                    subtitle: Text(_devices[index].address),
                    onTap: () async {
                      await _connectToDevice(_devices[index]);
                    },
                  );
                },
              ),
            ),
            SizedBox(height: 20),
            ElevatedButton(
              onPressed: _isConnected ? _printText : null,
              child: Text(_isConnected ? 'Print Text' : 'Connect First'),
            ),
          ],
        ),
      ),
    );
  }

  bool get _isConnected => _connectedDevice != null;

  Future<void> _connectToDevice(BluetoothDevice device) async {
    bool success = await _printer!.connect(device.address);
    if (success) {
      setState(() {
        _connectedDevice = device;
      });
    } else {
      // Handle connection failure
      ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text('Connection failed')));
    }
  }

  Future<void> _printText() async {
    if (_connectedDevice == null) return;

    String textToPrint = "Hello, this is a test print!\n";
    bool success = await _printer!.printText(textToPrint);

    if (success) {
      ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text('Print successful')));
    } else {
      ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text('Print failed')));
    }
  }
}

代码解释:

  1. 依赖导入:在pubspec.yaml中添加smart_bluetooth_pos_printer依赖。
  2. 初始化插件:在_MyAppStateinitState方法中初始化SmartBluetoothPosPrinter实例,并监听扫描到的设备。
  3. UI构建:使用ListView.builder显示扫描到的蓝牙设备列表,每个设备项点击时尝试连接。
  4. 连接设备_connectToDevice方法尝试连接到指定的蓝牙设备,并在成功后更新状态。
  5. 打印文本_printText方法向已连接的打印机发送打印命令。

注意:

  • 确保你的设备支持蓝牙,并且蓝牙已开启。
  • 根据实际使用情况,可能需要对错误处理和用户体验进行进一步优化。
  • smart_bluetooth_pos_printer插件的具体API可能会根据版本有所变化,请参考其官方文档获取最新信息。
回到顶部