diff --git a/lib/core/services/blue_print_service.dart b/lib/core/services/blue_print_service.dart index 8b3c2fa..b409b3a 100644 --- a/lib/core/services/blue_print_service.dart +++ b/lib/core/services/blue_print_service.dart @@ -33,7 +33,7 @@ class BluePrintService extends BaseService { } else { try { await _bluetooth.connect(_device!); - await Future.delayed(Duration(seconds: 2)); + await Future.delayed(Duration(seconds: 5)); response = true; } catch (e) { print('Error connect $e'); @@ -55,8 +55,21 @@ class BluePrintService extends BaseService { return response; } - Future printBytes(Uint8List bytes) async { + Future printBytes(Uint8List bytes, { int chunkSizeBytes = 20, int queueSleepTimeMs = 20 }) async { Map config = Map(); - return await _bluetooth.rawBytes(config, bytes); + final len = bytes.length; + List> chunks = []; + for (var i = 0; i < len; i += chunkSizeBytes) { + var end = (i + chunkSizeBytes < len) ? i + chunkSizeBytes : len; + chunks.add(bytes.sublist(i, end)); + } + + for (var i = 0; i < chunks.length; i += 1) { + await _bluetooth.rawBytes(config, chunks[i]); + await Future.delayed(Duration(milliseconds: queueSleepTimeMs)); + } + + + return true; } } diff --git a/lib/views/settings/setting_printer_view.dart b/lib/views/settings/setting_printer_view.dart index 1076c9e..0b33dc3 100644 --- a/lib/views/settings/setting_printer_view.dart +++ b/lib/views/settings/setting_printer_view.dart @@ -40,10 +40,8 @@ class _SettingPrinterViewState extends State { void initState() { super.initState(); _permission(); - } - void _testPrint() async { setState(() { _printing = true; @@ -64,33 +62,35 @@ class _SettingPrinterViewState extends State { log.i(chunkSizeBytes); log.i(queueSleepTimeMs); - // TODO Don't forget to choose printer's paper - PaperSize paper = state.printerPaperSize == SettingPrinterPaperM80 ? PaperSize.mm80 : PaperSize.mm58; + PaperSize paper = state.printerPaperSize == SettingPrinterPaperM80 + ? PaperSize.mm80 + : PaperSize.mm58; if (SettingPrinterEncodingImage == state.printerEncoding) { final bool res = await printerManager.printBytes( - Uint8List.fromList(await testTicketImage(paper) - )); + Uint8List.fromList(await testTicketImage(paper)), + chunkSizeBytes: chunkSizeBytes, + queueSleepTimeMs: queueSleepTimeMs); _dialogService.showDialog(description: 'result is $res'); } else { final bool res = await printerManager.printBytes( - Uint8List.fromList(await printTextCheck(paper, state.printerEncoding!, exampleJson['check_text'])) - ); + Uint8List.fromList(await printTextCheck( + paper, state.printerEncoding!, exampleJson['check_text'])), + chunkSizeBytes: chunkSizeBytes, + queueSleepTimeMs: queueSleepTimeMs); _dialogService.showDialog(description: 'result is $res'); } - } catch (e) { print('ERROR'); print(e); } - - //7 sec safe disconnect - await Future.delayed(Duration(seconds: 15)); + //10 sec safe disconnect + await Future.delayed(Duration(seconds: 14)); await printerManager.disconnect(); + await Future.delayed(Duration(seconds: 3)); setState(() { _printing = false; }); - } @override @@ -117,13 +117,17 @@ class _SettingPrinterViewState extends State { }), SettingItem( title: 'Кодировка', - name: vm.printerEncoding != null ? encoding[vm.printerEncoding] : '' , + name: vm.printerEncoding != null + ? encoding[vm.printerEncoding] + : '', onTap: () { _navigatorService.push(SettingsPrinterEncodingRoute); }), SettingItem( title: 'Ширина ленты', - name: vm.printerPaperSize != null ? paperSize[vm.printerPaperSize] : null , + name: vm.printerPaperSize != null + ? paperSize[vm.printerPaperSize] + : null, onTap: () { _navigatorService.push(SettingsPrinterPaperRoute); }), @@ -156,24 +160,24 @@ class _SettingPrinterViewState extends State { //Метод для получения постоянного доступа к местополения //только для Android void _permission() async { - if( Platform.isAndroid) { + if (Platform.isAndroid) { var status = await Permission.location.status; log.i(status); - if ( status.isDenied || status.isPermanentlyDenied) { + if (status.isDenied || status.isPermanentlyDenied) { DialogResponse response = await _dialogService.showConfirmationDialog( title: 'Доступ', - description: 'Для поиска устройств Bluetooth необходимо предоставить доступ к отслеживанию геолокации.', + description: + 'Для поиска устройств Bluetooth необходимо предоставить доступ к отслеживанию геолокации.', cancelTitle: 'Нет', confirmationTitle: 'Хорошо', ); if (response.confirmed) { - if (await Permission.location - .request() - .isGranted) { + if (await Permission.location.request().isGranted) { print('Granted'); } else { _dialogService.showDialog( - description: 'Необходимо указать постоянный доступ к местоположении для поиска принтера'); + description: + 'Необходимо указать постоянный доступ к местоположении для поиска принтера'); _navigatorService.pop(); } } else { @@ -184,8 +188,6 @@ class _SettingPrinterViewState extends State { } void _startInitialPrint() async { - - if (Platform.isIOS) { _testPrint(); } else { @@ -195,11 +197,11 @@ class _SettingPrinterViewState extends State { if (val == 12) { print('on'); _testPrint(); - } else if (val == 10) { print('off'); _dialogService.showDialog( - description: 'Отсутвует соеденение Bluetooth или он отключен' , title: 'Bluetooth'); + description: 'Отсутвует соеденение Bluetooth или он отключен', + title: 'Bluetooth'); } print('state is $val'); });