From 988d6a6c6b05eeb3e4793fb9518ddddb7e2b6e60 Mon Sep 17 00:00:00 2001 From: error500 Date: Wed, 26 Jan 2022 09:05:15 +0600 Subject: [PATCH 1/6] migrate ns-printer --- lib/core/locator.dart | 3 + lib/core/services/blue_print_service.dart | 62 +++++++++++++++++++ lib/redux/actions/setting_actions.dart | 2 +- lib/redux/state/setting_state.dart | 2 +- lib/views/check/image_show_container.dart | 40 ++++++------ .../printer/views/PrinterEncoding.dart | 9 --- .../printer/views/PrinterPaperSize.dart | 9 --- .../settings/printer/views/PrinterSelect.dart | 52 +++------------- lib/views/settings/setting_printer_view.dart | 37 +++++------ pubspec.lock | 23 +++---- pubspec.yaml | 6 +- 11 files changed, 125 insertions(+), 120 deletions(-) create mode 100644 lib/core/services/blue_print_service.dart diff --git a/lib/core/locator.dart b/lib/core/locator.dart index f09757e..08e8541 100644 --- a/lib/core/locator.dart +++ b/lib/core/locator.dart @@ -1,6 +1,7 @@ import 'package:aman_kassa_flutter/core/services/DataService.dart'; +import 'package:aman_kassa_flutter/core/services/blue_print_service.dart'; import '../core/services/DbService.dart'; @@ -25,6 +26,8 @@ class LocatorInjector { locator.registerLazySingleton(() => DialogService()); _log.d('Initializing DbService Service'); locator.registerLazySingleton(() => DbService.instance); + _log.d('Initializing BluePrintService Service'); + locator.registerLazySingleton(() => BluePrintService()); // depencies diff --git a/lib/core/services/blue_print_service.dart b/lib/core/services/blue_print_service.dart new file mode 100644 index 0000000..8b3c2fa --- /dev/null +++ b/lib/core/services/blue_print_service.dart @@ -0,0 +1,62 @@ +import 'dart:typed_data'; + +import 'package:aman_kassa_flutter/core/base/base_service.dart'; +import 'package:bluetooth_print/bluetooth_print.dart'; +import 'package:bluetooth_print/bluetooth_print_model.dart'; + + + +class BluePrintService extends BaseService { + BluetoothPrint _bluetooth = BluetoothPrint.instance; + BluetoothDevice? _device; + + Future scan() async { + await _bluetooth.startScan(timeout: Duration(seconds: 4)); + } + + Future stopScan() async { + await _bluetooth.stopScan(); + } + + Stream> get scanResult => _bluetooth.scanResults; + + + Stream get isScanning => _bluetooth.isScanning; + Stream get state => _bluetooth.state; + set device(BluetoothDevice device) => _device = device; + + + Future connect() async { + bool response = false; + if (_device == null) { + response = false; + } else { + try { + await _bluetooth.connect(_device!); + await Future.delayed(Duration(seconds: 2)); + response = true; + } catch (e) { + print('Error connect $e'); + response = false; + } + } + return response; + } + + Future disconnect() async { + bool response = false; + try { + await _bluetooth.disconnect(); + response = true; + } catch (e) { + print('Error $e'); + response = false; + } + return response; + } + + Future printBytes(Uint8List bytes) async { + Map config = Map(); + return await _bluetooth.rawBytes(config, bytes); + } +} diff --git a/lib/redux/actions/setting_actions.dart b/lib/redux/actions/setting_actions.dart index 2a003fe..3cef5b2 100644 --- a/lib/redux/actions/setting_actions.dart +++ b/lib/redux/actions/setting_actions.dart @@ -1,9 +1,9 @@ import 'package:aman_kassa_flutter/redux/constants/setting_const.dart'; import 'package:aman_kassa_flutter/redux/state/setting_state.dart'; +import 'package:bluetooth_print/bluetooth_print_model.dart'; import 'package:meta/meta.dart'; import 'package:redux/redux.dart'; import 'package:redux_thunk/redux_thunk.dart'; -import 'package:flutter_bluetooth_basic/src/bluetooth_device.dart'; import '../store.dart'; @immutable diff --git a/lib/redux/state/setting_state.dart b/lib/redux/state/setting_state.dart index 07ee938..6072258 100644 --- a/lib/redux/state/setting_state.dart +++ b/lib/redux/state/setting_state.dart @@ -1,6 +1,6 @@ import 'package:aman_kassa_flutter/redux/constants/setting_const.dart'; +import 'package:bluetooth_print/bluetooth_print_model.dart'; import 'package:meta/meta.dart'; -import 'package:flutter_bluetooth_basic/src/bluetooth_device.dart'; @immutable class SettingState { diff --git a/lib/views/check/image_show_container.dart b/lib/views/check/image_show_container.dart index fac7e56..3568aab 100644 --- a/lib/views/check/image_show_container.dart +++ b/lib/views/check/image_show_container.dart @@ -1,9 +1,11 @@ import 'dart:convert'; import 'dart:io'; +import 'dart:typed_data'; import 'package:aman_kassa_flutter/core/locator.dart'; import 'package:aman_kassa_flutter/core/models/check_image_modal.dart'; import 'package:aman_kassa_flutter/core/models/dialog_models.dart'; import 'package:aman_kassa_flutter/core/route_names.dart'; +import 'package:aman_kassa_flutter/core/services/blue_print_service.dart'; import 'package:aman_kassa_flutter/core/services/dialog_service.dart'; import 'package:aman_kassa_flutter/core/services/navigator_service.dart'; import 'package:aman_kassa_flutter/redux/constants/setting_const.dart'; @@ -13,11 +15,10 @@ import 'package:aman_kassa_flutter/shared/app_colors.dart'; import 'package:aman_kassa_flutter/shared/ui_helpers.dart'; import 'package:aman_kassa_flutter/views/settings/printer/PrinterTest.dart'; import 'package:aman_kassa_flutter/widgets/fields/busy_button_icon.dart'; -import 'package:esc_pos_bluetooth/esc_pos_bluetooth.dart'; +import 'package:bluetooth_print/bluetooth_print_model.dart'; import 'package:esc_pos_utils/esc_pos_utils.dart'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; -import 'package:flutter_bluetooth_basic/flutter_bluetooth_basic.dart'; import 'package:material_design_icons_flutter/material_design_icons_flutter.dart'; import 'package:vocsy_esys_flutter_share/vocsy_esys_flutter_share.dart'; import 'package:url_launcher/url_launcher.dart'; @@ -32,11 +33,10 @@ class ImageShowContainer extends StatefulWidget { } class _ImageShowContainerState extends State { - final PrinterBluetoothManager printerManager = PrinterBluetoothManager(); + final BluePrintService printerManager = locator(); final DialogService _dialogService = locator(); final BluetoothDevice? printerBtDevice = Redux.store!.state.settingState!.printerBT; - final BluetoothManager bluetoothManager = BluetoothManager.instance; bool _printing = false; @@ -44,7 +44,7 @@ class _ImageShowContainerState extends State { if (Platform.isIOS) { _print(); } else { - bluetoothManager.state.listen((val) { + printerManager.state.listen((val) { print("state = $val"); if (!mounted) return; if (val == 12) { @@ -82,32 +82,32 @@ class _ImageShowContainerState extends State { _printing = true; }); try { - printerManager.selectPrinter(PrinterBluetooth(state.printerBT!)); + printerManager.device = state.printerBT!; + await printerManager.connect(); PaperSize paper = state.printerPaperSize == SettingPrinterPaperM80 ? PaperSize.mm80 : PaperSize.mm58; if (SettingPrinterEncodingImage == state.printerEncoding) { - final PosPrintResult res = await printerManager.printTicket( - await printImageCheck(paper, widget.showModel.data!.base64Data!), - chunkSizeBytes: chunkSizeBytes, - queueSleepTimeMs: queueSleepTimeMs); - if (res.value != 1) { - _dialogService.showDialog(description: res.msg); + final bool res = await printerManager.printBytes( + Uint8List.fromList(await printImageCheck(paper, widget.showModel.data!.base64Data!))); + if (!res) { + _dialogService.showDialog(description: 'Ошибка при печати'); } } else { - final PosPrintResult res = await printerManager.printTicket( - await printTextCheck(paper, state.printerEncoding!, - jsonDecode(widget.showModel.data!.textData!)), - chunkSizeBytes: chunkSizeBytes, - queueSleepTimeMs: queueSleepTimeMs); - if (res.value != 1) { - _dialogService.showDialog(description: res.msg); + final bool res = await printerManager.printBytes( + Uint8List.fromList(await printTextCheck(paper, state.printerEncoding!, + jsonDecode(widget.showModel.data!.textData!))) + ); + if (!res) { + _dialogService.showDialog(description: 'Ошибка при печати'); } } + } catch (e) { print(e); } - await Future.delayed(Duration(seconds: 7)); + await Future.delayed(Duration(seconds: 15)); + await printerManager.disconnect(); setState(() { _printing = false; }); diff --git a/lib/views/settings/printer/views/PrinterEncoding.dart b/lib/views/settings/printer/views/PrinterEncoding.dart index cff273b..e80c4fc 100644 --- a/lib/views/settings/printer/views/PrinterEncoding.dart +++ b/lib/views/settings/printer/views/PrinterEncoding.dart @@ -1,22 +1,13 @@ -import 'dart:async'; -import 'dart:typed_data'; -import 'dart:ui' as ui; - import 'package:aman_kassa_flutter/core/logger.dart'; import 'package:aman_kassa_flutter/redux/actions/setting_actions.dart'; import 'package:aman_kassa_flutter/redux/store.dart'; -import 'package:esc_pos_bluetooth/esc_pos_bluetooth.dart'; -import 'package:esc_pos_utils/esc_pos_utils.dart'; import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart' hide Image; import 'package:flutter/rendering.dart'; -import 'package:flutter_bluetooth_basic/flutter_bluetooth_basic.dart'; -import 'package:intl/intl.dart'; import 'package:logger/logger.dart'; - import '../data/settings_envi.dart'; diff --git a/lib/views/settings/printer/views/PrinterPaperSize.dart b/lib/views/settings/printer/views/PrinterPaperSize.dart index 39453be..0be129d 100644 --- a/lib/views/settings/printer/views/PrinterPaperSize.dart +++ b/lib/views/settings/printer/views/PrinterPaperSize.dart @@ -1,20 +1,11 @@ -import 'dart:async'; -import 'dart:typed_data'; -import 'dart:ui' as ui; - import 'package:aman_kassa_flutter/core/logger.dart'; import 'package:aman_kassa_flutter/redux/actions/setting_actions.dart'; import 'package:aman_kassa_flutter/redux/store.dart'; -import 'package:esc_pos_bluetooth/esc_pos_bluetooth.dart'; -import 'package:esc_pos_utils/esc_pos_utils.dart'; import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart' hide Image; import 'package:flutter/rendering.dart'; - -import 'package:flutter_bluetooth_basic/flutter_bluetooth_basic.dart'; -import 'package:intl/intl.dart'; import 'package:logger/logger.dart'; import '../data/settings_envi.dart'; diff --git a/lib/views/settings/printer/views/PrinterSelect.dart b/lib/views/settings/printer/views/PrinterSelect.dart index cccd885..ff12a7b 100644 --- a/lib/views/settings/printer/views/PrinterSelect.dart +++ b/lib/views/settings/printer/views/PrinterSelect.dart @@ -1,25 +1,16 @@ -import 'dart:async'; -import 'dart:typed_data'; -import 'dart:ui' as ui; - +import 'package:aman_kassa_flutter/core/locator.dart'; import 'package:aman_kassa_flutter/core/logger.dart'; +import 'package:aman_kassa_flutter/core/services/blue_print_service.dart'; import 'package:aman_kassa_flutter/redux/actions/setting_actions.dart'; import 'package:aman_kassa_flutter/redux/store.dart'; +import 'package:bluetooth_print/bluetooth_print_model.dart'; -import 'package:esc_pos_bluetooth/esc_pos_bluetooth.dart'; -import 'package:esc_pos_utils/esc_pos_utils.dart'; import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart' hide Image; import 'package:flutter/rendering.dart'; - -import 'package:flutter_bluetooth_basic/flutter_bluetooth_basic.dart'; -import 'package:intl/intl.dart'; import 'package:logger/logger.dart'; -import '../PrinterTest.dart'; - - class PrinterSelectView extends StatefulWidget { PrinterSelectView({Key? key, this.title}) : super(key: key); final String? title; @@ -30,16 +21,14 @@ class PrinterSelectView extends StatefulWidget { } class _PrinterSelectViewState extends State { - PrinterBluetoothManager printerManager = PrinterBluetoothManager(); - List _devices = []; + BluePrintService printerManager = locator(); + List _devices = []; Logger _logger = getLogger('PrinterSelectView'); @override void initState() { super.initState(); - - printerManager.scanResults.listen((devices) async { - // print('UI: Devices found ${devices.length}'); + printerManager.scanResult.listen((devices) async { setState(() { _devices = devices; }); @@ -51,15 +40,15 @@ class _PrinterSelectViewState extends State { setState(() { _devices = []; }); - printerManager.startScan(Duration(seconds: 4)); + printerManager.scan(); } void _stopScanDevices() { printerManager.stopScan(); } - void _selectPrinter(PrinterBluetooth printer, BuildContext context, ) async { - printerManager.selectPrinter(printer); + void _selectPrinter(BluetoothDevice printer, BuildContext context, ) async { + printerManager.device = printer; _logger.i(printer.name); _logger.i(printer.address); @@ -71,27 +60,6 @@ class _PrinterSelectViewState extends State { await Redux.store!.dispatch(selectPrinterFromSetting(device)); Navigator.of(context).pop(false); } - - void _testPrint(PrinterBluetooth printer) async { - printerManager.selectPrinter(printer); - - // TODO Don't forget to choose printer's paper - const PaperSize paper = PaperSize.mm58; - - // TEST PRINT - // final PosPrintResult res = - // await printerManager.printTicket(await testTicket(paper), queueSleepTimeMs: 50); - - final PosPrintResult res = - await printerManager.printTicket( - await testTicketImage(paper), - chunkSizeBytes: 1024, - queueSleepTimeMs: 50 - ); - - - } - final key = GlobalKey(); @override @@ -140,7 +108,7 @@ class _PrinterSelectViewState extends State { ); }), floatingActionButton: StreamBuilder( - stream: printerManager.isScanningStream, + stream: printerManager.isScanning, initialData: false, builder: (c, snapshot) { if (snapshot.data != null) { diff --git a/lib/views/settings/setting_printer_view.dart b/lib/views/settings/setting_printer_view.dart index 92e7d79..1076c9e 100644 --- a/lib/views/settings/setting_printer_view.dart +++ b/lib/views/settings/setting_printer_view.dart @@ -2,8 +2,8 @@ import 'dart:io'; import 'dart:typed_data'; import 'package:aman_kassa_flutter/core/models/dialog_models.dart'; +import 'package:aman_kassa_flutter/core/services/blue_print_service.dart'; 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'; @@ -12,9 +12,6 @@ import 'package:aman_kassa_flutter/core/services/navigator_service.dart'; import 'package:aman_kassa_flutter/redux/constants/setting_const.dart'; import 'package:aman_kassa_flutter/redux/state/setting_state.dart'; import 'package:aman_kassa_flutter/redux/store.dart'; -import 'package:aman_kassa_flutter/shared/app_colors.dart'; -import 'package:aman_kassa_flutter/widgets/fields/aman_icon_button_horizontal.dart'; -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'; @@ -34,8 +31,7 @@ class SettingPrinterView extends StatefulWidget { class _SettingPrinterViewState extends State { NavigatorService _navigatorService = locator(); final DialogService _dialogService = locator(); - final PrinterBluetoothManager printerManager = PrinterBluetoothManager(); - final BluetoothManager bluetoothManager = BluetoothManager.instance; + final BluePrintService printerManager = locator(); final Logger log = getLogger('SettingPrinterView'); bool _printing = false; @@ -54,7 +50,8 @@ class _SettingPrinterViewState extends State { }); try { final SettingState state = Redux.store!.state.settingState!; - printerManager.selectPrinter(PrinterBluetooth(state.printerBT!)); + printerManager.device = state.printerBT!; + await printerManager.connect(); bool isIos = Platform.isIOS; int chunkSizeBytes = 3096; int queueSleepTimeMs = 100; @@ -67,31 +64,29 @@ 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; if (SettingPrinterEncodingImage == state.printerEncoding) { - final PosPrintResult res = await printerManager.printTicket( - await testTicketImage(paper), - chunkSizeBytes: chunkSizeBytes, - queueSleepTimeMs: queueSleepTimeMs - ); - _dialogService.showDialog(description: res.msg); + final bool res = await printerManager.printBytes( + Uint8List.fromList(await testTicketImage(paper) + )); + _dialogService.showDialog(description: 'result is $res'); } else { - final PosPrintResult res = await printerManager.printTicket( - await printTextCheck(paper, state.printerEncoding!, exampleJson['check_text']), - chunkSizeBytes: chunkSizeBytes, - queueSleepTimeMs: queueSleepTimeMs + final bool res = await printerManager.printBytes( + Uint8List.fromList(await printTextCheck(paper, state.printerEncoding!, exampleJson['check_text'])) ); - _dialogService.showDialog(description: res.msg); + _dialogService.showDialog(description: 'result is $res'); } + } catch (e) { print('ERROR'); print(e); } //7 sec safe disconnect - await Future.delayed(Duration(seconds: 7)); - + await Future.delayed(Duration(seconds: 15)); + await printerManager.disconnect(); setState(() { _printing = false; }); @@ -194,7 +189,7 @@ class _SettingPrinterViewState extends State { if (Platform.isIOS) { _testPrint(); } else { - bluetoothManager.state.listen((val) { + printerManager.state.listen((val) { print("state = $val"); if (!mounted) return; if (val == 12) { diff --git a/pubspec.lock b/pubspec.lock index 36d7daf..7bf0b18 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -29,6 +29,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "4.2.0" + bluetooth_print: + dependency: "direct main" + description: + path: "../bluetooth_print" + relative: true + source: path + version: "3.0.1" boolean_selector: dependency: transitive description: @@ -113,13 +120,6 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "2.0.3" - esc_pos_bluetooth: - dependency: "direct main" - description: - name: esc_pos_bluetooth - url: "https://pub.dartlang.org" - source: hosted - version: "0.4.1" esc_pos_utils: dependency: "direct main" description: @@ -160,13 +160,6 @@ packages: description: flutter source: sdk version: "0.0.0" - flutter_bluetooth_basic: - dependency: transitive - description: - name: flutter_bluetooth_basic - url: "https://pub.dartlang.org" - source: hosted - version: "0.1.7" flutter_lock_screen: dependency: "direct main" description: @@ -498,7 +491,7 @@ packages: name: rxdart url: "https://pub.dartlang.org" source: hosted - version: "0.26.0" + version: "0.27.3" shared_preferences: dependency: "direct main" description: diff --git a/pubspec.yaml b/pubspec.yaml index 49a1894..08515ac 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -34,10 +34,12 @@ dependencies: shared_preferences: ^2.0.12 flutter_lock_screen: ^2.0.1 local_auth: ^1.1.10 - esc_pos_bluetooth: ^0.4.1 - esc_pos_utils: ^1.1.0 # no edit for esc_pos_bluetooth: ^0.2.8 + #esc_pos_bluetooth: ^0.4.1 + esc_pos_utils: ^1.1.0 charset_converter: ^2.0.0 permission_handler: ^8.3.0 + bluetooth_print: + path: ../bluetooth_print dev_dependencies: flutter_test: sdk: flutter From 7b06e4c3903d621adb8b0b9d310b95b9a14c1dde Mon Sep 17 00:00:00 2001 From: error500 Date: Thu, 27 Jan 2022 09:35:05 +0600 Subject: [PATCH 2/6] ns blue_print_service.dart --- lib/core/services/blue_print_service.dart | 19 +++++-- lib/views/settings/setting_printer_view.dart | 54 ++++++++++---------- 2 files changed, 44 insertions(+), 29 deletions(-) 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'); }); From 16ea7ead942efa309ce76e7e546e5608d7b0408d Mon Sep 17 00:00:00 2001 From: error500 Date: Tue, 1 Feb 2022 13:22:39 +0600 Subject: [PATCH 3/6] flutter_blue added for ios --- lib/core/services/blue_print_service.dart | 135 +++++++++++++++---- lib/views/settings/setting_printer_view.dart | 8 +- pubspec.lock | 16 ++- pubspec.yaml | 1 + 4 files changed, 132 insertions(+), 28 deletions(-) diff --git a/lib/core/services/blue_print_service.dart b/lib/core/services/blue_print_service.dart index b409b3a..a9c712e 100644 --- a/lib/core/services/blue_print_service.dart +++ b/lib/core/services/blue_print_service.dart @@ -1,43 +1,106 @@ +import 'dart:io'; import 'dart:typed_data'; import 'package:aman_kassa_flutter/core/base/base_service.dart'; import 'package:bluetooth_print/bluetooth_print.dart'; import 'package:bluetooth_print/bluetooth_print_model.dart'; - - +import 'package:flutter_blue/flutter_blue.dart' as flutter_blue; +import 'package:flutter_blue/gen/flutterblue.pb.dart' as proto; class BluePrintService extends BaseService { - BluetoothPrint _bluetooth = BluetoothPrint.instance; + final BluetoothPrint _bluetoothAndr = BluetoothPrint.instance; + flutter_blue.BluetoothDevice? _bluetoothDeviceIOS; + final flutter_blue.FlutterBlue _bluetoothIOS = + flutter_blue.FlutterBlue.instance; + + final bool isAndroid = Platform.isAndroid; + final bool isIos = Platform.isIOS; + // final bool isAndroid = false; + // final bool isIos = true; + BluetoothDevice? _device; Future scan() async { - await _bluetooth.startScan(timeout: Duration(seconds: 4)); + if (isAndroid) { + await _bluetoothAndr.startScan(timeout: Duration(seconds: 4)); + } else if (isIos) { + await _bluetoothIOS.startScan(timeout: const Duration(seconds: 5)); + } } Future stopScan() async { - await _bluetooth.stopScan(); + if (isAndroid) { + await _bluetoothAndr.stopScan(); + } else if (isIos) { + await _bluetoothIOS.stopScan(); + } } - Stream> get scanResult => _bluetooth.scanResults; + Stream> get scanResult { + if (isAndroid) + return _bluetoothAndr.scanResults; + else + return _bluetoothIOS.scanResults.asyncMap>( + (event) => + Future.wait(event.toList().map((e) async => BluetoothDevice() + ..name = e.device.name + ..type = e.device.type.index + ..address = e.device.id.id))); + } + Stream get isScanning => + isAndroid ? _bluetoothAndr.isScanning : _bluetoothIOS.isScanning; + + Stream get state => isAndroid + ? _bluetoothAndr.state + : _bluetoothIOS.state.asyncMap((event) => event.index); - Stream get isScanning => _bluetooth.isScanning; - Stream get state => _bluetooth.state; set device(BluetoothDevice device) => _device = device; - Future connect() async { bool response = false; if (_device == null) { response = false; } else { + // try { + // await _bluetoothAndr.connect(_device!); + // await Future.delayed(Duration(seconds: 5)); + // response = true; + // } catch (e) { + // print('Error connect $e'); + // response = false; + // } + try { - await _bluetooth.connect(_device!); - await Future.delayed(Duration(seconds: 5)); + if (isAndroid) { + await _bluetoothAndr.connect(_device!); + } else if (isIos) { + _bluetoothDeviceIOS = flutter_blue.BluetoothDevice.fromProto( + proto.BluetoothDevice( + name: _device?.name ?? '', + remoteId: _device?.address ?? '', + type: proto.BluetoothDevice_Type.valueOf(_device?.type ?? 0), + ), + ); + final List connectedDevices = + await _bluetoothIOS.connectedDevices; + final int deviceConnectedIndex = connectedDevices + .indexWhere((flutter_blue.BluetoothDevice bluetoothDevice) { + return bluetoothDevice.id == _bluetoothDeviceIOS?.id; + }); + if (deviceConnectedIndex < 0) { + await _bluetoothDeviceIOS?.connect(); + } + } + response = true; - } catch (e) { - print('Error connect $e'); + _device?.connected = true; + return Future.value(response); + } on Exception catch (error) { + print('$runtimeType - Error $error'); response = false; + _device?.connected = false; + return Future.value(response); } } return response; @@ -46,7 +109,11 @@ class BluePrintService extends BaseService { Future disconnect() async { bool response = false; try { - await _bluetooth.disconnect(); + if (isAndroid) { + await _bluetoothAndr.disconnect(); + } else if (isIos) { + await _bluetoothDeviceIOS?.disconnect(); + } response = true; } catch (e) { print('Error $e'); @@ -55,21 +122,37 @@ class BluePrintService extends BaseService { return response; } - Future printBytes(Uint8List bytes, { int chunkSizeBytes = 20, int queueSleepTimeMs = 20 }) async { - Map config = Map(); - 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)); - } + Future printBytes(Uint8List bytes, + {int chunkSizeBytes = 20, int queueSleepTimeMs = 20}) async { + if (isAndroid) { + Map config = Map(); + 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)); + for (var i = 0; i < chunks.length; i += 1) { + await _bluetoothAndr.rawBytes(config, chunks[i]); + await Future.delayed(Duration(milliseconds: queueSleepTimeMs)); + } + } else if (isIos) { + final List bluetoothServices = + await _bluetoothDeviceIOS?.discoverServices() ?? + []; + final flutter_blue.BluetoothService bluetoothService = + bluetoothServices.firstWhere( + (flutter_blue.BluetoothService service) => service.isPrimary, + ); + final flutter_blue.BluetoothCharacteristic characteristic = + bluetoothService.characteristics.firstWhere( + (flutter_blue.BluetoothCharacteristic bluetoothCharacteristic) => + bluetoothCharacteristic.properties.write, + ); + await characteristic.write(bytes, withoutResponse: true); } - return true; } } diff --git a/lib/views/settings/setting_printer_view.dart b/lib/views/settings/setting_printer_view.dart index 0b33dc3..2316462 100644 --- a/lib/views/settings/setting_printer_view.dart +++ b/lib/views/settings/setting_printer_view.dart @@ -49,7 +49,13 @@ class _SettingPrinterViewState extends State { try { final SettingState state = Redux.store!.state.settingState!; printerManager.device = state.printerBT!; - await printerManager.connect(); + bool isConnected = await printerManager.connect(); + if(isConnected == false) { + await Future.delayed(Duration(seconds: 3)); + await printerManager.disconnect(); + return; + } + await Future.delayed(Duration(seconds: 3)); bool isIos = Platform.isIOS; int chunkSizeBytes = 3096; int queueSleepTimeMs = 100; diff --git a/pubspec.lock b/pubspec.lock index 7bf0b18..74dc448 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -78,6 +78,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "1.15.0" + convert: + dependency: transitive + description: + name: convert + url: "https://pub.dartlang.org" + source: hosted + version: "3.0.1" crypto: dependency: transitive description: @@ -160,6 +167,13 @@ packages: description: flutter source: sdk version: "0.0.0" + flutter_blue: + dependency: "direct main" + description: + name: flutter_blue + url: "https://pub.dartlang.org" + source: hosted + version: "0.8.0" flutter_lock_screen: dependency: "direct main" description: @@ -491,7 +505,7 @@ packages: name: rxdart url: "https://pub.dartlang.org" source: hosted - version: "0.27.3" + version: "0.26.0" shared_preferences: dependency: "direct main" description: diff --git a/pubspec.yaml b/pubspec.yaml index 08515ac..3053480 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -40,6 +40,7 @@ dependencies: permission_handler: ^8.3.0 bluetooth_print: path: ../bluetooth_print + flutter_blue: ^0.8.0 dev_dependencies: flutter_test: sdk: flutter From 95aeb07fc891b1fb71cc149298148acad39b2c4c Mon Sep 17 00:00:00 2001 From: Serik Uvaissov Date: Tue, 1 Feb 2022 14:05:33 +0600 Subject: [PATCH 4/6] ios version --- ios/Flutter/AppFrameworkInfo.plist | 2 +- ios/Podfile.lock | 83 +++++++++++-------- ios/Runner.xcodeproj/project.pbxproj | 49 +++++++---- .../xcshareddata/xcschemes/Runner.xcscheme | 2 +- lib/core/services/blue_print_service.dart | 58 +++++++------ 5 files changed, 114 insertions(+), 80 deletions(-) diff --git a/ios/Flutter/AppFrameworkInfo.plist b/ios/Flutter/AppFrameworkInfo.plist index 6b4c0f7..f2872cf 100644 --- a/ios/Flutter/AppFrameworkInfo.plist +++ b/ios/Flutter/AppFrameworkInfo.plist @@ -21,6 +21,6 @@ CFBundleVersion 1.0 MinimumOSVersion - 8.0 + 9.0 diff --git a/ios/Podfile.lock b/ios/Podfile.lock index 0ad49a9..597e813 100644 --- a/ios/Podfile.lock +++ b/ios/Podfile.lock @@ -1,98 +1,111 @@ PODS: - - barcode_scan (0.0.1): + - barcode_scan2 (0.0.1): - Flutter - MTBBarcodeScanner - SwiftProtobuf + - bluetooth_print (0.0.1): + - Flutter - charset_converter (0.0.1): - Flutter - device_info (0.0.1): - Flutter - - esys_flutter_share (0.0.1): - - Flutter - Flutter (1.0.0) - - flutter_bluetooth_basic (0.0.1): + - flutter_blue (0.0.1): - Flutter + - flutter_blue/Protos (= 0.0.1) + - flutter_blue/Protos (0.0.1): + - Flutter + - Protobuf (~> 3.11.4) - FMDB (2.7.5): - FMDB/standard (= 2.7.5) - FMDB/standard (2.7.5) - local_auth (0.0.1): - Flutter - MTBBarcodeScanner (5.0.11) - - path_provider (0.0.1): + - path_provider_ios (0.0.1): - Flutter - "permission_handler (5.1.0+2)": - Flutter - - shared_preferences (0.0.1): + - Protobuf (3.11.4) + - shared_preferences_ios (0.0.1): - Flutter - sqflite (0.0.2): - Flutter - FMDB (>= 2.7.5) - - SwiftProtobuf (1.9.0) - - url_launcher (0.0.1): + - SwiftProtobuf (1.18.0) + - url_launcher_ios (0.0.1): + - Flutter + - vocsy_esys_flutter_share (0.0.1): - Flutter DEPENDENCIES: - - barcode_scan (from `.symlinks/plugins/barcode_scan/ios`) + - barcode_scan2 (from `.symlinks/plugins/barcode_scan2/ios`) + - bluetooth_print (from `.symlinks/plugins/bluetooth_print/ios`) - charset_converter (from `.symlinks/plugins/charset_converter/ios`) - device_info (from `.symlinks/plugins/device_info/ios`) - - esys_flutter_share (from `.symlinks/plugins/esys_flutter_share/ios`) - Flutter (from `Flutter`) - - flutter_bluetooth_basic (from `.symlinks/plugins/flutter_bluetooth_basic/ios`) + - flutter_blue (from `.symlinks/plugins/flutter_blue/ios`) - local_auth (from `.symlinks/plugins/local_auth/ios`) - - path_provider (from `.symlinks/plugins/path_provider/ios`) + - path_provider_ios (from `.symlinks/plugins/path_provider_ios/ios`) - permission_handler (from `.symlinks/plugins/permission_handler/ios`) - - shared_preferences (from `.symlinks/plugins/shared_preferences/ios`) + - shared_preferences_ios (from `.symlinks/plugins/shared_preferences_ios/ios`) - sqflite (from `.symlinks/plugins/sqflite/ios`) - - url_launcher (from `.symlinks/plugins/url_launcher/ios`) + - url_launcher_ios (from `.symlinks/plugins/url_launcher_ios/ios`) + - vocsy_esys_flutter_share (from `.symlinks/plugins/vocsy_esys_flutter_share/ios`) SPEC REPOS: trunk: - FMDB - MTBBarcodeScanner + - Protobuf - SwiftProtobuf EXTERNAL SOURCES: - barcode_scan: - :path: ".symlinks/plugins/barcode_scan/ios" + barcode_scan2: + :path: ".symlinks/plugins/barcode_scan2/ios" + bluetooth_print: + :path: ".symlinks/plugins/bluetooth_print/ios" charset_converter: :path: ".symlinks/plugins/charset_converter/ios" device_info: :path: ".symlinks/plugins/device_info/ios" - esys_flutter_share: - :path: ".symlinks/plugins/esys_flutter_share/ios" Flutter: :path: Flutter - flutter_bluetooth_basic: - :path: ".symlinks/plugins/flutter_bluetooth_basic/ios" + flutter_blue: + :path: ".symlinks/plugins/flutter_blue/ios" local_auth: :path: ".symlinks/plugins/local_auth/ios" - path_provider: - :path: ".symlinks/plugins/path_provider/ios" + path_provider_ios: + :path: ".symlinks/plugins/path_provider_ios/ios" permission_handler: :path: ".symlinks/plugins/permission_handler/ios" - shared_preferences: - :path: ".symlinks/plugins/shared_preferences/ios" + shared_preferences_ios: + :path: ".symlinks/plugins/shared_preferences_ios/ios" sqflite: :path: ".symlinks/plugins/sqflite/ios" - url_launcher: - :path: ".symlinks/plugins/url_launcher/ios" + url_launcher_ios: + :path: ".symlinks/plugins/url_launcher_ios/ios" + vocsy_esys_flutter_share: + :path: ".symlinks/plugins/vocsy_esys_flutter_share/ios" SPEC CHECKSUMS: - barcode_scan: a5c27959edfafaa0c771905bad0b29d6d39e4479 + barcode_scan2: 0af2bb63c81b4565aab6cd78278e4c0fa136dbb0 + bluetooth_print: da8f47e8881a0b1f0fd8d73cd6fa84c9bd41ac28 charset_converter: 215c7b04932ec2b9ba43be96a9bc34afed3e5322 device_info: d7d233b645a32c40dfdc212de5cf646ca482f175 - esys_flutter_share: 403498dab005b36ce1f8d7aff377e81f0621b0b4 - Flutter: 434fef37c0980e73bb6479ef766c45957d4b510c - flutter_bluetooth_basic: 0e4e27e22b50b3a25cc1d1e131953feb4af414f4 + Flutter: 50d75fe2f02b26cc09d224853bb45737f8b3214a + flutter_blue: eeb381dc4727a0954dede73515f683865494b370 FMDB: 2ce00b547f966261cd18927a3ddb07cb6f3db82a - local_auth: 25938960984c3a7f6e3253e3f8d962fdd16852bd + local_auth: ef62030a2731330b95df7ef1331bd15f6a64b8a6 MTBBarcodeScanner: f453b33c4b7dfe545d8c6484ed744d55671788cb - path_provider: abfe2b5c733d04e238b0d8691db0cfd63a27a93c + path_provider_ios: 7d7ce634493af4477d156294792024ec3485acd5 permission_handler: ccb20a9fad0ee9b1314a52b70b76b473c5f8dab0 - shared_preferences: af6bfa751691cdc24be3045c43ec037377ada40d + Protobuf: 176220c526ad8bd09ab1fb40a978eac3fef665f7 + shared_preferences_ios: aef470a42dc4675a1cdd50e3158b42e3d1232b32 sqflite: 6d358c025f5b867b29ed92fc697fd34924e11904 - SwiftProtobuf: ecbec1be9036d15655f6b3443a1c4ea693c97932 - url_launcher: 6fef411d543ceb26efce54b05a0a40bfd74cbbef + SwiftProtobuf: c3c12645230d9b09c72267e0de89468c5543bd86 + url_launcher_ios: 02f1989d4e14e998335b02b67a7590fa34f971af + vocsy_esys_flutter_share: 98b79fad467203ababde56a7289ac90da6a4ddf5 PODFILE CHECKSUM: 5aafc9b59da66d8d46f05cbbbd21261eb9757176 diff --git a/ios/Runner.xcodeproj/project.pbxproj b/ios/Runner.xcodeproj/project.pbxproj index 8e18353..d0a9a9c 100644 --- a/ios/Runner.xcodeproj/project.pbxproj +++ b/ios/Runner.xcodeproj/project.pbxproj @@ -3,7 +3,7 @@ archiveVersion = 1; classes = { }; - objectVersion = 46; + objectVersion = 50; objects = { /* Begin PBXBuildFile section */ @@ -166,7 +166,7 @@ 97C146E61CF9000F007C117D /* Project object */ = { isa = PBXProject; attributes = { - LastUpgradeCheck = 1020; + LastUpgradeCheck = 1300; ORGANIZATIONNAME = "The Chromium Authors"; TargetAttributes = { 97C146ED1CF9000F007C117D = { @@ -256,31 +256,35 @@ "${PODS_ROOT}/Target Support Files/Pods-Runner/Pods-Runner-frameworks.sh", "${BUILT_PRODUCTS_DIR}/FMDB/FMDB.framework", "${BUILT_PRODUCTS_DIR}/MTBBarcodeScanner/MTBBarcodeScanner.framework", + "${BUILT_PRODUCTS_DIR}/Protobuf/protobuf.framework", "${BUILT_PRODUCTS_DIR}/SwiftProtobuf/SwiftProtobuf.framework", - "${BUILT_PRODUCTS_DIR}/barcode_scan/barcode_scan.framework", + "${BUILT_PRODUCTS_DIR}/barcode_scan2/barcode_scan2.framework", "${BUILT_PRODUCTS_DIR}/charset_converter/charset_converter.framework", "${BUILT_PRODUCTS_DIR}/device_info/device_info.framework", - "${BUILT_PRODUCTS_DIR}/esys_flutter_share/esys_flutter_share.framework", + "${BUILT_PRODUCTS_DIR}/flutter_blue/flutter_blue.framework", "${BUILT_PRODUCTS_DIR}/local_auth/local_auth.framework", - "${BUILT_PRODUCTS_DIR}/path_provider/path_provider.framework", - "${BUILT_PRODUCTS_DIR}/shared_preferences/shared_preferences.framework", + "${BUILT_PRODUCTS_DIR}/path_provider_ios/path_provider_ios.framework", + "${BUILT_PRODUCTS_DIR}/shared_preferences_ios/shared_preferences_ios.framework", "${BUILT_PRODUCTS_DIR}/sqflite/sqflite.framework", - "${BUILT_PRODUCTS_DIR}/url_launcher/url_launcher.framework", + "${BUILT_PRODUCTS_DIR}/url_launcher_ios/url_launcher_ios.framework", + "${BUILT_PRODUCTS_DIR}/vocsy_esys_flutter_share/vocsy_esys_flutter_share.framework", ); name = "[CP] Embed Pods Frameworks"; outputPaths = ( "${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/FMDB.framework", "${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/MTBBarcodeScanner.framework", + "${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/protobuf.framework", "${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/SwiftProtobuf.framework", - "${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/barcode_scan.framework", + "${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/barcode_scan2.framework", "${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/charset_converter.framework", "${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/device_info.framework", - "${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/esys_flutter_share.framework", + "${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/flutter_blue.framework", "${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/local_auth.framework", - "${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/path_provider.framework", - "${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/shared_preferences.framework", + "${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/path_provider_ios.framework", + "${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/shared_preferences_ios.framework", "${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/sqflite.framework", - "${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/url_launcher.framework", + "${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/url_launcher_ios.framework", + "${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/vocsy_esys_flutter_share.framework", ); runOnlyForDeploymentPostprocessing = 0; shellPath = /bin/sh; @@ -376,7 +380,7 @@ GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; GCC_WARN_UNUSED_FUNCTION = YES; GCC_WARN_UNUSED_VARIABLE = YES; - IPHONEOS_DEPLOYMENT_TARGET = 8.0; + IPHONEOS_DEPLOYMENT_TARGET = 9.0; MTL_ENABLE_DEBUG_INFO = NO; SDKROOT = iphoneos; SUPPORTED_PLATFORMS = iphoneos; @@ -401,7 +405,10 @@ "$(PROJECT_DIR)/Flutter", ); INFOPLIST_FILE = Runner/Info.plist; - LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; + LD_RUNPATH_SEARCH_PATHS = ( + "$(inherited)", + "@executable_path/Frameworks", + ); LIBRARY_SEARCH_PATHS = ( "$(inherited)", "$(PROJECT_DIR)/Flutter", @@ -463,7 +470,7 @@ GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; GCC_WARN_UNUSED_FUNCTION = YES; GCC_WARN_UNUSED_VARIABLE = YES; - IPHONEOS_DEPLOYMENT_TARGET = 8.0; + IPHONEOS_DEPLOYMENT_TARGET = 9.0; MTL_ENABLE_DEBUG_INFO = YES; ONLY_ACTIVE_ARCH = YES; SDKROOT = iphoneos; @@ -512,7 +519,7 @@ GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; GCC_WARN_UNUSED_FUNCTION = YES; GCC_WARN_UNUSED_VARIABLE = YES; - IPHONEOS_DEPLOYMENT_TARGET = 8.0; + IPHONEOS_DEPLOYMENT_TARGET = 9.0; MTL_ENABLE_DEBUG_INFO = NO; SDKROOT = iphoneos; SUPPORTED_PLATFORMS = iphoneos; @@ -538,7 +545,10 @@ "$(PROJECT_DIR)/Flutter", ); INFOPLIST_FILE = Runner/Info.plist; - LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; + LD_RUNPATH_SEARCH_PATHS = ( + "$(inherited)", + "@executable_path/Frameworks", + ); LIBRARY_SEARCH_PATHS = ( "$(inherited)", "$(PROJECT_DIR)/Flutter", @@ -570,7 +580,10 @@ "$(PROJECT_DIR)/Flutter", ); INFOPLIST_FILE = Runner/Info.plist; - LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; + LD_RUNPATH_SEARCH_PATHS = ( + "$(inherited)", + "@executable_path/Frameworks", + ); LIBRARY_SEARCH_PATHS = ( "$(inherited)", "$(PROJECT_DIR)/Flutter", diff --git a/ios/Runner.xcodeproj/xcshareddata/xcschemes/Runner.xcscheme b/ios/Runner.xcodeproj/xcshareddata/xcschemes/Runner.xcscheme index a28140c..3db53b6 100644 --- a/ios/Runner.xcodeproj/xcshareddata/xcschemes/Runner.xcscheme +++ b/ios/Runner.xcodeproj/xcshareddata/xcschemes/Runner.xcscheme @@ -1,6 +1,6 @@ printBytes(Uint8List bytes, {int chunkSizeBytes = 20, int queueSleepTimeMs = 20}) async { if (isAndroid) { - Map config = Map(); - 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 _bluetoothAndr.rawBytes(config, chunks[i]); - await Future.delayed(Duration(milliseconds: queueSleepTimeMs)); - } + await _printAndroid(bytes, chunkSizeBytes, queueSleepTimeMs); } else if (isIos) { - final List bluetoothServices = - await _bluetoothDeviceIOS?.discoverServices() ?? - []; - final flutter_blue.BluetoothService bluetoothService = - bluetoothServices.firstWhere( - (flutter_blue.BluetoothService service) => service.isPrimary, - ); - final flutter_blue.BluetoothCharacteristic characteristic = - bluetoothService.characteristics.firstWhere( - (flutter_blue.BluetoothCharacteristic bluetoothCharacteristic) => - bluetoothCharacteristic.properties.write, - ); - await characteristic.write(bytes, withoutResponse: true); + await _printIos(bytes); } return true; } + + Future _printIos(Uint8List bytes) async { + final List bluetoothServices = + await _bluetoothDeviceIOS?.discoverServices() ?? + []; + final flutter_blue.BluetoothService bluetoothService = + bluetoothServices.firstWhere( + (flutter_blue.BluetoothService service) => service.isPrimary, + ); + final flutter_blue.BluetoothCharacteristic characteristic = + bluetoothService.characteristics.firstWhere( + (flutter_blue.BluetoothCharacteristic bluetoothCharacteristic) => + bluetoothCharacteristic.properties.write, + ); + await characteristic.write(bytes, withoutResponse: true); + } + + Future _printAndroid(Uint8List bytes, int chunkSizeBytes, int queueSleepTimeMs) async { + Map config = Map(); + 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 _bluetoothAndr.rawBytes(config, chunks[i]); + await Future.delayed(Duration(milliseconds: queueSleepTimeMs)); + } + } } From 005c00071d7995a1bd0448dc7ab05738ba26b9a8 Mon Sep 17 00:00:00 2001 From: Serik Uvaissov Date: Fri, 18 Feb 2022 09:43:28 +0600 Subject: [PATCH 5/6] ios version - 1 --- lib/core/services/blue_print_service.dart | 43 ++++++++++++-------- lib/views/check/image_show_container.dart | 13 +++++- lib/views/settings/setting_printer_view.dart | 27 +++++++----- 3 files changed, 54 insertions(+), 29 deletions(-) diff --git a/lib/core/services/blue_print_service.dart b/lib/core/services/blue_print_service.dart index 32fed42..74936aa 100644 --- a/lib/core/services/blue_print_service.dart +++ b/lib/core/services/blue_print_service.dart @@ -114,6 +114,8 @@ class BluePrintService extends BaseService { } else if (isIos) { await _bluetoothDeviceIOS?.disconnect(); } + + print('disconnected'); response = true; } catch (e) { print('Error $e'); @@ -124,16 +126,30 @@ class BluePrintService extends BaseService { Future printBytes(Uint8List bytes, {int chunkSizeBytes = 20, int queueSleepTimeMs = 20}) async { - if (isAndroid) { - await _printAndroid(bytes, chunkSizeBytes, queueSleepTimeMs); - } else if (isIos) { - await _printIos(bytes); + Map config = Map(); + 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) { + if (isAndroid) { + await _printAndroid(chunks[i], config); + } else if (isIos) { + await _printIos(Uint8List.fromList(chunks[i]), config); + } + + await Future.delayed(Duration(milliseconds: queueSleepTimeMs)); + } + + + return true; } - Future _printIos(Uint8List bytes) async { + Future _printIos(Uint8List bytes,Map config) async { final List bluetoothServices = await _bluetoothDeviceIOS?.discoverServices() ?? []; @@ -149,18 +165,9 @@ class BluePrintService extends BaseService { await characteristic.write(bytes, withoutResponse: true); } - Future _printAndroid(Uint8List bytes, int chunkSizeBytes, int queueSleepTimeMs) async { - Map config = Map(); - 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 _bluetoothAndr.rawBytes(config, chunks[i]); - await Future.delayed(Duration(milliseconds: queueSleepTimeMs)); - } + Future _printAndroid(List chunk ,Map config) async { + + + await _bluetoothAndr.rawBytes(config, chunk); } } diff --git a/lib/views/check/image_show_container.dart b/lib/views/check/image_show_container.dart index 3568aab..24401d6 100644 --- a/lib/views/check/image_show_container.dart +++ b/lib/views/check/image_show_container.dart @@ -61,6 +61,17 @@ class _ImageShowContainerState extends State { } } + @override + void dispose() { + disconnect(); + super.dispose(); + } + + + void disconnect() async { + await printerManager.disconnect(); + } + void _print() async { final SettingState state = Redux.store!.state.settingState!; if (state.printerBT == null) { @@ -107,7 +118,7 @@ class _ImageShowContainerState extends State { print(e); } await Future.delayed(Duration(seconds: 15)); - await printerManager.disconnect(); + setState(() { _printing = false; }); diff --git a/lib/views/settings/setting_printer_view.dart b/lib/views/settings/setting_printer_view.dart index 2316462..040e5b1 100644 --- a/lib/views/settings/setting_printer_view.dart +++ b/lib/views/settings/setting_printer_view.dart @@ -41,6 +41,15 @@ class _SettingPrinterViewState extends State { super.initState(); _permission(); } + @override + void dispose() { + disconnect(); + super.dispose(); + } + + disconnect() async { + await printerManager.disconnect(); + } void _testPrint() async { setState(() { @@ -49,12 +58,11 @@ class _SettingPrinterViewState extends State { try { final SettingState state = Redux.store!.state.settingState!; printerManager.device = state.printerBT!; - bool isConnected = await printerManager.connect(); - if(isConnected == false) { - await Future.delayed(Duration(seconds: 3)); - await printerManager.disconnect(); - return; - } + + await disconnect(); + await Future.delayed(Duration(seconds: 1)); + + await printerManager.connect(); await Future.delayed(Duration(seconds: 3)); bool isIos = Platform.isIOS; int chunkSizeBytes = 3096; @@ -77,14 +85,14 @@ class _SettingPrinterViewState extends State { Uint8List.fromList(await testTicketImage(paper)), chunkSizeBytes: chunkSizeBytes, queueSleepTimeMs: queueSleepTimeMs); - _dialogService.showDialog(description: 'result is $res'); + //_dialogService.showDialog(description: 'result is $res'); } else { final bool res = await printerManager.printBytes( Uint8List.fromList(await printTextCheck( paper, state.printerEncoding!, exampleJson['check_text'])), chunkSizeBytes: chunkSizeBytes, queueSleepTimeMs: queueSleepTimeMs); - _dialogService.showDialog(description: 'result is $res'); + //_dialogService.showDialog(description: 'result is $res'); } } catch (e) { print('ERROR'); @@ -92,8 +100,7 @@ class _SettingPrinterViewState extends State { } //10 sec safe disconnect await Future.delayed(Duration(seconds: 14)); - await printerManager.disconnect(); - await Future.delayed(Duration(seconds: 3)); + setState(() { _printing = false; }); From d23036ebf050914fef56d79d86925df84d83a000 Mon Sep 17 00:00:00 2001 From: suvaysov Date: Mon, 21 Mar 2022 10:31:20 +0600 Subject: [PATCH 6/6] fix problems --- android/app/build.gradle | 4 +-- android/app/src/main/AndroidManifest.xml | 4 +-- android/build.gradle | 2 +- .../gradle/wrapper/gradle-wrapper.properties | 2 +- pubspec.lock | 27 ++++++++++++------- pubspec.yaml | 14 +++++----- 6 files changed, 30 insertions(+), 23 deletions(-) diff --git a/android/app/build.gradle b/android/app/build.gradle index 6cc7126..4743e58 100644 --- a/android/app/build.gradle +++ b/android/app/build.gradle @@ -34,7 +34,7 @@ if (keystorePropertiesFile.exists()) { android { - compileSdkVersion 30 + compileSdkVersion 31 sourceSets { main.java.srcDirs += 'src/main/kotlin' @@ -47,7 +47,7 @@ android { defaultConfig { applicationId "kz.com.aman.kassa" minSdkVersion 21 - targetSdkVersion 30 + targetSdkVersion 31 versionCode flutterVersionCode.toInteger() versionName flutterVersionName testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" diff --git a/android/app/src/main/AndroidManifest.xml b/android/app/src/main/AndroidManifest.xml index 6c9b830..c07c065 100644 --- a/android/app/src/main/AndroidManifest.xml +++ b/android/app/src/main/AndroidManifest.xml @@ -14,9 +14,9 @@ - + =2.15.0 <3.0.0" - flutter: ">=2.5.0" + flutter: ">=2.10.0-0" diff --git a/pubspec.yaml b/pubspec.yaml index 3053480..99b1513 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -12,28 +12,28 @@ dependencies: redux_thunk: ^0.4.0 redux_persist: ^0.9.0 redux_persist_flutter: ^0.9.0 - responsive_builder: ^0.4.1 + responsive_builder: ^0.4.2 provider: ^6.0.2 logger: ^1.1.0 get_it: ^7.2.0 equatable: ^2.0.3 http: ^0.13.4 sqflite: ^2.0.2 - path_provider: ^2.0.8 - google_fonts: ^2.2.0 + path_provider: ^2.0.9 + google_fonts: ^2.3.1 material_design_icons_flutter: ^5.0.6595 intl: ^0.17.0 barcode_scan2: ^4.2.0 device_info: ^2.0.3 vocsy_esys_flutter_share: ^1.0.0 auto_size_text: ^3.0.0 - url_launcher: ^6.0.18 + url_launcher: ^6.0.20 qr_flutter: ^4.0.0 mask_text_input_formatter: ^2.1.0 - flutter_screenutil: ^5.1.0 - shared_preferences: ^2.0.12 + flutter_screenutil: ^5.3.1 + shared_preferences: ^2.0.13 flutter_lock_screen: ^2.0.1 - local_auth: ^1.1.10 + local_auth: ^1.1.11 #esc_pos_bluetooth: ^0.4.1 esc_pos_utils: ^1.1.0 charset_converter: ^2.0.0