Dart SDK
Dart SDK 适用于 Flutter 跨平台应用开发,支持 iOS、Android、Windows、macOS 多平台。
所有包均发布至 pub.dev,搜索 psdk 前缀即可找到。
dependencies: psdk_frame_father: ^latest # 核心框架 psdk_device_adapter: ^latest # 设备适配器指令包(按需选择)
Section titled “指令包(按需选择)”dependencies: psdk_fruit_cpcl: ^latest # CPCL 指令 psdk_fruit_esc: ^latest # ESC 指令 psdk_fruit_tspl: ^latest # TSPL 指令蓝牙包(按平台选择)
Section titled “蓝牙包(按平台选择)”dependencies: psdk_bluetooth_ble: ^latest # BLE (iOS/Android/macOS) psdk_bluetooth_classic: ^latest # 经典蓝牙 (Android) psdk_bluetooth_windows: ^latest # Windows 蓝牙| 包名 | 说明 | 平台 |
|---|---|---|
| psdk_frame_father | 核心框架 | 全平台 |
| psdk_device_adapter | 设备适配器 | 全平台 |
| psdk_bluetooth_ble | BLE 蓝牙 | iOS/Android/macOS |
| psdk_bluetooth_classic | 经典蓝牙 | Android |
| psdk_bluetooth_windows | Windows 蓝牙 | Windows |
| psdk_fruit_cpcl | CPCL 指令 | 全平台 |
| psdk_fruit_esc | ESC 指令 | 全平台 |
| psdk_fruit_tspl | TSPL 指令 | 全平台 |
| psdk_network | Wi-Fi 连接 | 全平台 |
| psdk_usb_standard | USB 连接 | Android |
| psdk_usb_windows | USB 连接 | Windows |
1. 搜索设备
Section titled “1. 搜索设备”import 'package:psdk_bluetooth_classic/psdk_bluetooth_classic.dart';
// 开启蓝牙搜索await ClassicBluetooth().startDiscovery( disconnectConnectedDevice: false);
// 监听搜索结果ClassicBluetooth().discovered().listen((devices) { for (final device in devices) { print('发现设备: ${device.name}'); }});2. 连接设备
Section titled “2. 连接设备”// 连接到设备final connectedDevice = await ClassicBluetooth().connect(device);print('连接成功: ${connectedDevice.name}');3. 断开连接
Section titled “3. 断开连接”await ClassicBluetooth().disconnect();ESC 指令使用
Section titled “ESC 指令使用”ESC 指令广泛用于热敏小票打印机。详细指令说明请参考 ESC 指令文档。
import 'package:psdk_fruit_esc/psdk_fruit_esc.dart';
final esc = ESC.generic(connectedDevice);await esc .initialize() .align(Align.center) .text('收银小票') .align(Align.left) .text('商品A x1 ¥29.90') .cut() .write();打印小票示例
Section titled “打印小票示例”final esc = ESC.generic(connectedDevice);await esc .initialize() // 标题 .align(Align.center) .textSize(width: 2, height: 2) .bold(true) .text('XX 便利店') .bold(false) .textSize(width: 1, height: 1) .text('订单号: 20240115001') .text('================================') // 商品列表 .align(Align.left) .text('可乐 500ml x2 ¥6.00') .text('薯片 大包 x1 ¥8.50') .text('--------------------------------') // 合计 .textSize(width: 1, height: 2) .text('合计: ¥14.50') .textSize(width: 1, height: 1) // 二维码 .align(Align.center) .qrcode('https://shop.example.com', size: 5) .text('扫码关注店铺') .feed(3) .cut() .write();import 'dart:ui' as ui;
// 加载图片final image = await loadImage('assets/logo.png');
final esc = ESC.generic(connectedDevice);await esc .initialize() .image(EscImage(image: image)) .feed(3) .cut() .write();CPCL 指令使用
Section titled “CPCL 指令使用”CPCL 指令用于便携式标签打印机。详细指令说明请参考 CPCL 指令文档。
import 'package:psdk_fruit_cpcl/psdk_fruit_cpcl.dart';
final cpcl = CPCL.generic(connectedDevice);await cpcl .page(CPCLPage(width: 576, height: 400)) .text(CPCLText(x: 50, y: 50, content: '商品标签')) .barcode(CPCLBarcode(x: 50, y: 150, content: '1234567890')) .print() .write();完整标签示例
Section titled “完整标签示例”final cpcl = CPCL.generic(connectedDevice);await cpcl .page(CPCLPage(width: 576, height: 400)) // 标题 .center() .text(CPCLText( x: 0, y: 30, font: 4, size: 2, content: '商品标签', )) // 商品信息 .left() .text(CPCLText(x: 30, y: 100, content: '名称: 有机苹果')) .text(CPCLText(x: 30, y: 140, content: '规格: 500g/袋')) .text(CPCLText(x: 30, y: 180, content: '价格: ¥25.90')) // 条码 .barcode(CPCLBarcode( x: 100, y: 230, type: CPCLBarcodeType.code128, height: 60, content: '6901234567890', )) // 二维码 .qrcode(CPCLQRCode( x: 400, y: 100, content: 'https://example.com', )) .print() .write();TSPL 指令使用
Section titled “TSPL 指令使用”TSPL 指令用于标签条码打印机。详细指令说明请参考 TSPL 指令文档。
import 'package:psdk_fruit_tspl/psdk_fruit_tspl.dart';
final tspl = TSPL.generic(connectedDevice);await tspl .page(TPage().width(60).height(40)) .cls() .text(TText().x(50).y(50).content('标签内容')) .print(1) .write();完整标签示例
Section titled “完整标签示例”final tspl = TSPL.generic(connectedDevice);await tspl .page(TPage().width(60).height(40)) .gap(3) .direction(Direction.up) .cls() // 标题 .text(TText() .x(180).y(30) .font('TSS24.BF2') .xMulti(2).yMulti(2) .content('商品标签')) // 分隔线 .bar(TBar().x(30).y(80).width(420).height(2)) // 商品信息 .text(TText().x(30).y(100).content('品名: 有机苹果')) .text(TText().x(30).y(140).content('规格: 500g/袋')) .text(TText().x(30).y(180).content('价格: ¥25.90')) // 条码 .barcode(TBarcode() .x(100).y(220) .type('128') .height(60) .content('6901234567890')) // 二维码 .qrcode(TQRCode() .x(350).y(100) .cellWidth(4) .content('https://example.com')) .print(1) .write();监听打印机回传
Section titled “监听打印机回传”connectedDevice.received.listen((data) { print('打印机回传: $data'); // 解析打印机状态、查询结果等});try { await cpcl.write(); print('打印成功');} on BluetoothException catch (e) { print('蓝牙错误: ${e.message}');} on PrinterException catch (e) { print('打印机错误: ${e.message}');} catch (e) { print('未知错误: $e');}BLE 蓝牙使用(iOS/macOS)
Section titled “BLE 蓝牙使用(iOS/macOS)”import 'package:psdk_bluetooth_ble/psdk_bluetooth_ble.dart';
// 初始化final ble = BleBluetooth();
// 搜索设备await ble.startDiscovery();
// 监听发现的设备ble.discovered().listen((devices) { print('发现 ${devices.length} 个设备');});
// 连接设备final connectedDevice = await ble.connect(device);
// 断开连接await ble.disconnect();完整示例请参考: