gpt4 book ai didi

android - Flutter Blue无法检测到蓝牙打印机(Zebra ez320)

转载 作者:行者123 更新时间:2023-12-03 03:57:43 26 4
gpt4 key购买 nike

您好,我尝试打印到蓝牙打印机中,但是示例代码中的第一个问题是它没有检测到任何蓝牙设备,并且显示了我不知道该设备是什么的设备列表,我尝试了所有方法使其正常工作,但是没发生什么事。顺便说一句,我正在使用名为https://pub.dev/packages/esc_pos_printer的软件包

这是搜索后的结果
enter image description here

import 'package:flutter/services.dart';
import 'package:image/image.dart';
import 'package:esc_pos_printer/esc_pos_printer.dart';
import 'package:flutter/material.dart' hide Image;
import 'package:oktoast/oktoast.dart';

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

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return OKToast(
child: MaterialApp(
title: 'Bluetooth demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Bluetooth demo'),
),
);
}
}

class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;

@override
_MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
PrinterBluetoothManager printerManager = PrinterBluetoothManager();
List<PrinterBluetooth> _devices = [];

@override
void initState() {
super.initState();

printerManager.scanResults.listen((devices) async {
print('UI: Devices found ${devices.length}');
setState(() {
_devices = devices;
});
});
}

void _startScanDevices() async{

setState(() {
_devices = [];
});
printerManager.startScan(Duration(seconds: 100));
}

void _stopScanDevices() {
printerManager.stopScan();
}

Future<Ticket> testTicket() async {
final Ticket ticket = Ticket(PaperSize.mm58);
ticket.text(
'Regular: aA bB cC dD eE fF gG hH iI jJ kK lL mM nN oO pP qQ rR sS tT uU vV wW xX yY zZ');
ticket.text('Special 1: àÀ èÈ éÉ ûÛ üÜ çÇ ôÔ',
styles: PosStyles(codeTable: PosCodeTable.westEur));
ticket.text('Special 2: blåbærgrød',
styles: PosStyles(codeTable: PosCodeTable.westEur));

ticket.text('Bold text', styles: PosStyles(bold: true));
ticket.text('Reverse text', styles: PosStyles(reverse: true));
ticket.text('Underlined text',
styles: PosStyles(underline: true), linesAfter: 1);
ticket.text('Align left', styles: PosStyles(align: PosTextAlign.left));
ticket.text('Align center', styles: PosStyles(align: PosTextAlign.center));
ticket.text('Align right',
styles: PosStyles(align: PosTextAlign.right), linesAfter: 1);

ticket.row([
PosColumn(
text: 'col3',
width: 3,
styles: PosStyles(align: PosTextAlign.center, underline: true),
),
PosColumn(
text: 'col6',
width: 6,
styles: PosStyles(align: PosTextAlign.center, underline: true),
),
PosColumn(
text: 'col3',
width: 3,
styles: PosStyles(align: PosTextAlign.center, underline: true),
),
]);

ticket.text('Text size 200%',
styles: PosStyles(
height: PosTextSize.size2,
width: PosTextSize.size2,
));

// Print image
final ByteData data = await rootBundle.load('assets/logo.png');
final Uint8List bytes = data.buffer.asUint8List();
final Image image = decodeImage(bytes);
ticket.image(image);
// Print image using an alternative (obsolette) command
// ticket.imageRaster(image);

// Print barcode
final List<int> barData = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 4];
ticket.barcode(Barcode.upcA(barData));

// Print mixed (chinese + latin) text. Only for printers supporting Kanji mode
// ticket.text(
// 'hello ! 中文字 # world @ éphémère &',
// styles: PosStyles(codeTable: PosCodeTable.westEur),
// containsChinese: true,
// );

ticket.feed(2);
return ticket;
}

void _testPrint(PrinterBluetooth printer) async {
printerManager.selectPrinter(printer);

final PosPrintResult res =
await printerManager.printTicket(await testTicket());
showToast(res.msg);
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: ListView.builder(
itemCount: _devices.length,
itemBuilder: (BuildContext context, int index) {
return InkWell(
onTap: () => _testPrint(_devices[index]),
child: Column(
children: <Widget>[
Container(
height: 60,
padding: EdgeInsets.only(left: 10),
alignment: Alignment.centerLeft,
child: Row(
children: <Widget>[
Icon(Icons.print),
SizedBox(width: 10),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(_devices[index].name ?? ''),
Text(_devices[index].address),
Text(
'Click to print a test receipt',
style: TextStyle(color: Colors.grey[700]),
),
],
),
)
],
),
),
Divider(),
],
),
);
}),
floatingActionButton: StreamBuilder<bool>(
stream: printerManager.isScanningStream,
initialData: false,
builder: (c, snapshot) {
if (snapshot.data) {
return FloatingActionButton(
child: Icon(Icons.stop),
onPressed: _stopScanDevices,
backgroundColor: Colors.red,
);
} else {
return FloatingActionButton(
child: Icon(Icons.search),
onPressed: _startScanDevices,
);
}
},
),
);
}
}

最佳答案

似乎Zebra ez320不是ESC / POS打印机,而该库旨在与ESC / POS打印机通信。

关于android - Flutter Blue无法检测到蓝牙打印机(Zebra ez320),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59819526/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com