printer android fix
parent
0de81ff7d2
commit
f45835d215
|
|
@ -13,9 +13,10 @@
|
|||
<uses-permission android:name="android.permission.BLUETOOTH_PRIVILEGED" />
|
||||
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
|
||||
<uses-permission android:name="android.permission.ACCESS_BLUETOOTH_ADMIN" />
|
||||
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
|
||||
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
|
||||
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
|
||||
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
|
||||
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
|
||||
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
|
||||
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION"/>
|
||||
|
||||
|
||||
<application
|
||||
|
|
|
|||
|
|
@ -24,6 +24,8 @@ const TextStyle productTextStyle = const TextStyle(
|
|||
fontWeight: FontWeight.w400, color: Colors.black, fontSize: 15);
|
||||
const TextStyle buttonTitleTextStyle = const TextStyle(
|
||||
fontWeight: FontWeight.w700, color: whiteColor, fontSize: 14);
|
||||
const TextStyle buttonTitleDisableTextStyle = const TextStyle(
|
||||
fontWeight: FontWeight.w700, color: fillColor, fontSize: 14);
|
||||
const TextStyle buttonBigTitleTextStyle = const TextStyle(
|
||||
fontWeight: FontWeight.w700,
|
||||
color: whiteColor,
|
||||
|
|
|
|||
|
|
@ -35,9 +35,31 @@ class _ImageShowContainerState extends State<ImageShowContainer> {
|
|||
final PrinterBluetoothManager printerManager = PrinterBluetoothManager();
|
||||
final DialogService _dialogService = locator<DialogService>();
|
||||
final BluetoothDevice printerBtDevice = Redux.store.state.settingState.printerBT;
|
||||
final BluetoothManager bluetoothManager = BluetoothManager.instance;
|
||||
|
||||
bool _printing = false;
|
||||
|
||||
void _preparePrint() async {
|
||||
if (Platform.isIOS) {
|
||||
await _print();
|
||||
} else {
|
||||
bluetoothManager.state.listen((val) {
|
||||
print("state = $val");
|
||||
if (!mounted) return;
|
||||
if (val == 12) {
|
||||
print('on');
|
||||
_print();
|
||||
|
||||
} else if (val == 10) {
|
||||
print('off');
|
||||
_dialogService.showDialog(
|
||||
description: 'Отсутвует соеденение Bluetooth или он отключен' , title: 'Bluetooth');
|
||||
}
|
||||
print('state is $val');
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
void _print() async {
|
||||
final SettingState state = Redux.store.state.settingState;
|
||||
if(state.printerBT == null) {
|
||||
|
|
@ -47,7 +69,7 @@ class _ImageShowContainerState extends State<ImageShowContainer> {
|
|||
|
||||
bool isIos = Platform.isIOS;
|
||||
int chunkSizeBytes = 3096;
|
||||
int queueSleepTimeMs = 50;
|
||||
int queueSleepTimeMs = 100;
|
||||
|
||||
if(isIos){
|
||||
chunkSizeBytes = 75;
|
||||
|
|
@ -81,11 +103,13 @@ class _ImageShowContainerState extends State<ImageShowContainer> {
|
|||
_dialogService.showDialog(description: res.msg);
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
setState(() {
|
||||
_printing = false;
|
||||
});
|
||||
} catch (e) {
|
||||
print(e);
|
||||
}
|
||||
await Future.delayed(Duration(seconds: 7));
|
||||
setState(() {
|
||||
_printing = false;
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
|
|
@ -110,7 +134,7 @@ class _ImageShowContainerState extends State<ImageShowContainer> {
|
|||
),
|
||||
)
|
||||
else
|
||||
IconButton(icon: Icon(Icons.print), onPressed: _print)
|
||||
IconButton(icon: Icon(Icons.print), onPressed: _preparePrint)
|
||||
],
|
||||
),
|
||||
body: ListView(
|
||||
|
|
|
|||
|
|
@ -117,13 +117,13 @@ Future<Ticket> testTicketImage(PaperSize paper) async {
|
|||
// Print image
|
||||
final ByteData byteData = await rootBundle.load('assets/images/check.png');
|
||||
final Uint8List bytes = byteData.buffer.asUint8List();
|
||||
final Im.Image imagea = Im.decodeImage(bytes);
|
||||
final Im.Image image = Im.decodeImage(bytes);
|
||||
// Using `ESC *`
|
||||
//ticket.image(imagea);
|
||||
// Using `GS v 0` (obsolete)
|
||||
//ticket.imageRaster(imagea);
|
||||
// Using `GS ( L`
|
||||
ticket.imageRaster(imagea, imageFn: PosImageFn.bitImageRaster);
|
||||
ticket.imageRaster(image, imageFn: PosImageFn.bitImageRaster);
|
||||
//ticket.image(imagea);
|
||||
|
||||
|
||||
|
|
@ -137,8 +137,8 @@ Future<Ticket> printImageCheck(PaperSize paper, String base64Src) async {
|
|||
final Ticket ticket = Ticket(paper);
|
||||
final Uint8List bytes = base64Decode(base64Src);
|
||||
final Im.Image image = Im.decodeImage(bytes);
|
||||
//ticket.imageRaster(image, imageFn: PosImageFn.bitImageRaster);
|
||||
ticket.image(image);
|
||||
ticket.imageRaster(image, imageFn: PosImageFn.bitImageRaster);
|
||||
//ticket.image(image);
|
||||
ticket.feed(2);
|
||||
ticket.cut();
|
||||
return ticket;
|
||||
|
|
@ -209,17 +209,15 @@ Future<Ticket> printTextCheck(PaperSize paper, String encoding, var data ) async
|
|||
const double qrSize = 200;
|
||||
try {
|
||||
final uiImg = await QrPainter(
|
||||
|
||||
data: qr,
|
||||
version: QrVersions.auto,
|
||||
gapless: true,
|
||||
).toImageData(qrSize);
|
||||
final dir = await getTemporaryDirectory();
|
||||
final pathName = '${dir.path}/qr_tmp.png';
|
||||
final qrFile = File(pathName);
|
||||
final imgFile = await qrFile.writeAsBytes(uiImg.buffer.asUint8List());
|
||||
final img = Im.decodePng(imgFile.readAsBytesSync());
|
||||
|
||||
//final dir = await getTemporaryDirectory();
|
||||
//final pathName = '${dir.path}/qr_tmp.png';
|
||||
//final qrFile = File(pathName);
|
||||
//final imgFile = await qrFile.writeAsBytes(uiImg.buffer.asUint8List());
|
||||
final img = Im.decodePng(uiImg.buffer.asUint8List());
|
||||
ticket.image(img);
|
||||
|
||||
//ticket.qrcode(qr, size: QRSize.Size1 );
|
||||
|
|
|
|||
|
|
@ -1,6 +1,8 @@
|
|||
import 'dart:io';
|
||||
import 'dart:typed_data';
|
||||
|
||||
import 'package:aman_kassa_flutter/widgets/fields/busy_button_icon.dart';
|
||||
import 'package:flutter_bluetooth_basic/flutter_bluetooth_basic.dart';
|
||||
import 'package:aman_kassa_flutter/core/locator.dart';
|
||||
import 'package:aman_kassa_flutter/core/logger.dart';
|
||||
import 'package:aman_kassa_flutter/core/route_names.dart';
|
||||
|
|
@ -14,6 +16,7 @@ import 'package:aman_kassa_flutter/widgets/fields/aman_icon_button_horizontal.da
|
|||
import 'package:esc_pos_bluetooth/esc_pos_bluetooth.dart';
|
||||
import 'package:esc_pos_utils/esc_pos_utils.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:permission_handler/permission_handler.dart';
|
||||
import 'package:aman_kassa_flutter/views/settings/printer/PrinterTest.dart';
|
||||
import 'package:flutter_redux/flutter_redux.dart';
|
||||
import 'package:logger/logger.dart';
|
||||
|
|
@ -30,48 +33,67 @@ class SettingPrinterView extends StatefulWidget {
|
|||
class _SettingPrinterViewState extends State<SettingPrinterView> {
|
||||
NavigatorService _navigatorService = locator<NavigatorService>();
|
||||
final DialogService _dialogService = locator<DialogService>();
|
||||
PrinterBluetoothManager printerManager = PrinterBluetoothManager();
|
||||
final PrinterBluetoothManager printerManager = PrinterBluetoothManager();
|
||||
final BluetoothManager bluetoothManager = BluetoothManager.instance;
|
||||
final Logger log = getLogger('SettingPrinterView');
|
||||
|
||||
bool _printing = false;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_permission();
|
||||
|
||||
}
|
||||
|
||||
|
||||
void _testPrint() async {
|
||||
final SettingState state = Redux.store.state.settingState;
|
||||
printerManager.selectPrinter(PrinterBluetooth(state.printerBT));
|
||||
bool isIos = Platform.isIOS;
|
||||
int chunkSizeBytes = 3096;
|
||||
int queueSleepTimeMs = 50;
|
||||
setState(() {
|
||||
_printing = true;
|
||||
});
|
||||
try {
|
||||
final SettingState state = Redux.store.state.settingState;
|
||||
printerManager.selectPrinter(PrinterBluetooth(state.printerBT));
|
||||
bool isIos = Platform.isIOS;
|
||||
int chunkSizeBytes = 3096;
|
||||
int queueSleepTimeMs = 100;
|
||||
|
||||
if(isIos) {
|
||||
chunkSizeBytes = 75;
|
||||
queueSleepTimeMs = 10;
|
||||
if (isIos) {
|
||||
chunkSizeBytes = 75;
|
||||
queueSleepTimeMs = 10;
|
||||
}
|
||||
|
||||
log.i(chunkSizeBytes);
|
||||
log.i(queueSleepTimeMs);
|
||||
|
||||
// TODO Don't forget to choose printer's paper
|
||||
PaperSize paper = state.printerPaperSize == SettingPrinterPaperM80 ? PaperSize.mm80 : PaperSize.mm58;
|
||||
if (SettingPrinterEncodingImage == state.printerEncoding) {
|
||||
final PosPrintResult res = await printerManager.printTicket(
|
||||
await testTicketImage(paper),
|
||||
chunkSizeBytes: chunkSizeBytes,
|
||||
queueSleepTimeMs: queueSleepTimeMs
|
||||
);
|
||||
_dialogService.showDialog(description: res.msg);
|
||||
} else {
|
||||
final PosPrintResult res = await printerManager.printTicket(
|
||||
await printTextCheck(paper, state.printerEncoding, exampleJson['check_text']),
|
||||
chunkSizeBytes: chunkSizeBytes,
|
||||
queueSleepTimeMs: queueSleepTimeMs
|
||||
);
|
||||
_dialogService.showDialog(description: res.msg);
|
||||
}
|
||||
} catch (e) {
|
||||
print('ERROR');
|
||||
print(e);
|
||||
}
|
||||
|
||||
log.i(chunkSizeBytes);
|
||||
log.i(queueSleepTimeMs);
|
||||
//7 sec safe disconnect
|
||||
await Future.delayed(Duration(seconds: 7));
|
||||
|
||||
// TODO Don't forget to choose printer's paper
|
||||
PaperSize paper = state.printerPaperSize == SettingPrinterPaperM80 ? PaperSize.mm80 : PaperSize.mm58;
|
||||
if(SettingPrinterEncodingImage == state.printerEncoding) {
|
||||
final PosPrintResult res = await printerManager.printTicket(
|
||||
await testTicketImage(paper),
|
||||
chunkSizeBytes: chunkSizeBytes,
|
||||
queueSleepTimeMs: queueSleepTimeMs
|
||||
);
|
||||
_dialogService.showDialog(description: res.msg);
|
||||
} else {
|
||||
final PosPrintResult res = await printerManager.printTicket(
|
||||
await printTextCheck(paper, state.printerEncoding, exampleJson['check_text']),
|
||||
chunkSizeBytes: chunkSizeBytes,
|
||||
queueSleepTimeMs: queueSleepTimeMs
|
||||
);
|
||||
_dialogService.showDialog(description: res.msg);
|
||||
}
|
||||
setState(() {
|
||||
_printing = false;
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
|
|
@ -115,13 +137,15 @@ class _SettingPrinterViewState extends State<SettingPrinterView> {
|
|||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.end,
|
||||
children: [
|
||||
AmanIconButtonHorizontal(
|
||||
BusyButtonIcon(
|
||||
icon: Icons.local_printshop_outlined,
|
||||
title: 'Напечатать тестовую страницу',
|
||||
activeColor: primaryColor,
|
||||
selected: vm.printerBT != null,
|
||||
busy: _printing,
|
||||
enabled: vm.printerBT != null,
|
||||
onPressed: () {
|
||||
_testPrint();
|
||||
|
||||
_startInitialPrint();
|
||||
|
||||
},
|
||||
),
|
||||
],
|
||||
|
|
@ -134,4 +158,45 @@ class _SettingPrinterViewState extends State<SettingPrinterView> {
|
|||
),
|
||||
);
|
||||
}
|
||||
|
||||
//Метод для получения постоянного доступа к местополения
|
||||
//только для Android
|
||||
void _permission() async {
|
||||
if( Platform.isAndroid) {
|
||||
var status = await Permission.locationAlways.status;
|
||||
if (status.isUndetermined || status.isDenied || status.isPermanentlyDenied) {
|
||||
if (await Permission.locationAlways
|
||||
.request()
|
||||
.isGranted) {
|
||||
print('Granted');
|
||||
} else {
|
||||
_dialogService.showDialog(
|
||||
description: 'Необходимо указать постоянный доступ к местоположении для поиска принтера');
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void _startInitialPrint() async {
|
||||
|
||||
|
||||
if (Platform.isIOS) {
|
||||
await _testPrint();
|
||||
} else {
|
||||
bluetoothManager.state.listen((val) {
|
||||
print("state = $val");
|
||||
if (!mounted) return;
|
||||
if (val == 12) {
|
||||
print('on');
|
||||
_testPrint();
|
||||
|
||||
} else if (val == 10) {
|
||||
print('off');
|
||||
_dialogService.showDialog(
|
||||
description: 'Отсутвует соеденение Bluetooth или он отключен' , title: 'Bluetooth');
|
||||
}
|
||||
print('state is $val');
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -52,7 +52,7 @@ class _BusyButtonState extends State<BusyButton> {
|
|||
? AutoSizeText(
|
||||
widget.title,
|
||||
textAlign: TextAlign.center,
|
||||
style: buttonTitleTextStyle,
|
||||
style: widget.enabled ? buttonTitleTextStyle : buttonTitleDisableTextStyle,
|
||||
minFontSize: 2,
|
||||
maxLines: 1,
|
||||
)
|
||||
|
|
|
|||
|
|
@ -68,7 +68,7 @@ class _BusyButtonIconState extends State<BusyButtonIcon> {
|
|||
Text(
|
||||
widget.title,
|
||||
textAlign: TextAlign.center,
|
||||
style: buttonTitleTextStyle,
|
||||
style: widget.enabled ? buttonTitleTextStyle : buttonTitleDisableTextStyle,
|
||||
maxLines: 1,
|
||||
),
|
||||
],
|
||||
|
|
|
|||
14
pubspec.lock
14
pubspec.lock
|
|
@ -401,6 +401,20 @@ packages:
|
|||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "1.9.2"
|
||||
permission_handler:
|
||||
dependency: "direct main"
|
||||
description:
|
||||
name: permission_handler
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "5.1.0+2"
|
||||
permission_handler_platform_interface:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: permission_handler_platform_interface
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "2.0.2"
|
||||
petitparser:
|
||||
dependency: transitive
|
||||
description:
|
||||
|
|
|
|||
|
|
@ -37,6 +37,7 @@ dependencies:
|
|||
esc_pos_bluetooth: ^0.2.8
|
||||
esc_pos_utils: ^0.3.6 # no edit for esc_pos_bluetooth: ^0.2.8
|
||||
charset_converter: ^1.0.3
|
||||
permission_handler: ^5.0.1+2
|
||||
dev_dependencies:
|
||||
flutter_test:
|
||||
sdk: flutter
|
||||
|
|
|
|||
Loading…
Reference in New Issue