From 3f79c10a463a1d9707f9be6effa132a0dc063ce8 Mon Sep 17 00:00:00 2001 From: Ivan Ray Altomera Date: Thu, 23 Jul 2020 17:41:26 +0800 Subject: [PATCH 01/21] feat(printer_bluetooth_manager): add timeout --- lib/src/printer_bluetooth_manager.dart | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/src/printer_bluetooth_manager.dart b/lib/src/printer_bluetooth_manager.dart index ba78b32..0db1b99 100644 --- a/lib/src/printer_bluetooth_manager.dart +++ b/lib/src/printer_bluetooth_manager.dart @@ -75,10 +75,10 @@ class PrinterBluetoothManager { List bytes, { int chunkSizeBytes = 20, int queueSleepTimeMs = 20, + int timeout = 5, }) async { final Completer completer = Completer(); - const int timeout = 5; if (_selectedPrinter == null) { return Future.value(PosPrintResult.printerNotSelected); } else if (_isScanning.value) { @@ -146,6 +146,7 @@ class PrinterBluetoothManager { Ticket ticket, { int chunkSizeBytes = 20, int queueSleepTimeMs = 20, + int timeout = 5, }) async { if (ticket == null || ticket.bytes.isEmpty) { return Future.value(PosPrintResult.ticketEmpty); @@ -154,6 +155,7 @@ class PrinterBluetoothManager { ticket.bytes, chunkSizeBytes: chunkSizeBytes, queueSleepTimeMs: queueSleepTimeMs, + timeout: timeout, ); } } From 49da8fdc65edea49a498c8612a3d980d9f6562da Mon Sep 17 00:00:00 2001 From: Ivan Ray Altomera Date: Fri, 24 Jul 2020 17:33:52 +0800 Subject: [PATCH 02/21] fix(printer_bluetooth_manager): listener and timeout --- lib/src/printer_bluetooth_manager.dart | 84 +++++++++++++------------- 1 file changed, 43 insertions(+), 41 deletions(-) diff --git a/lib/src/printer_bluetooth_manager.dart b/lib/src/printer_bluetooth_manager.dart index 0db1b99..b7b5e1f 100644 --- a/lib/src/printer_bluetooth_manager.dart +++ b/lib/src/printer_bluetooth_manager.dart @@ -39,6 +39,10 @@ class PrinterBluetoothManager { BehaviorSubject.seeded([]); Stream> get scanResults => _scanResults.stream; + List _bufferedBytes = []; + int _queueSleepTimeMs = 20; + int _chunkSizeBytes = 20; + Future _runDelayed(int seconds) { return Future.delayed(Duration(seconds: seconds)); } @@ -69,12 +73,27 @@ class PrinterBluetoothManager { void selectPrinter(PrinterBluetooth printer) { _selectedPrinter = printer; + _bluetoothManager.state.listen((state) async { + switch (state) { + case BluetoothManager.CONNECTED: + _isConnected = true; + if (_bufferedBytes.isNotEmpty) { + await _writePending(); + } + await _bluetoothManager.disconnect(); + break; + case BluetoothManager.DISCONNECTED: + _isConnected = false; + break; + default: + break; + } + print('BluetoothManager.STATE => $state'); + }); } Future writeBytes( List bytes, { - int chunkSizeBytes = 20, - int queueSleepTimeMs = 20, int timeout = 5, }) async { final Completer completer = Completer(); @@ -87,8 +106,6 @@ class PrinterBluetoothManager { return Future.value(PosPrintResult.printInProgress); } - _isPrinting = true; - // We have to rescan before connecting, otherwise we can connect only once await _bluetoothManager.startScan(timeout: Duration(seconds: 1)); await _bluetoothManager.stopScan(); @@ -96,47 +113,13 @@ class PrinterBluetoothManager { // Connect await _bluetoothManager.connect(_selectedPrinter._device); - // Subscribe to the events - _bluetoothManager.state.listen((state) async { - switch (state) { - case BluetoothManager.CONNECTED: - // To avoid double call - if (!_isConnected) { - 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 _bluetoothManager.writeData(chunks[i]); - sleep(Duration(milliseconds: queueSleepTimeMs)); - } - - completer.complete(PosPrintResult.success); - } - // TODO sending disconnect signal should be event-based - _runDelayed(3).then((dynamic v) async { - await _bluetoothManager.disconnect(); - _isPrinting = false; - }); - _isConnected = true; - break; - case BluetoothManager.DISCONNECTED: - _isConnected = false; - break; - default: - break; - } - }); - // Printing timeout _runDelayed(timeout).then((dynamic v) async { if (_isPrinting) { _isPrinting = false; completer.complete(PosPrintResult.timeout); } + completer.complete(PosPrintResult.success); }); return completer.future; @@ -151,11 +134,30 @@ class PrinterBluetoothManager { if (ticket == null || ticket.bytes.isEmpty) { return Future.value(PosPrintResult.ticketEmpty); } + + _bufferedBytes = ticket.bytes; + _queueSleepTimeMs = queueSleepTimeMs; + _chunkSizeBytes = chunkSizeBytes; + return writeBytes( ticket.bytes, - chunkSizeBytes: chunkSizeBytes, - queueSleepTimeMs: queueSleepTimeMs, timeout: timeout, ); } + + Future _writePending() async { + final len = _bufferedBytes.length; + List> chunks = []; + for (var i = 0; i < len; i += _chunkSizeBytes) { + var end = (i + _chunkSizeBytes < len) ? i + _chunkSizeBytes : len; + chunks.add(_bufferedBytes.sublist(i, end)); + } + _isPrinting = true; + for (var i = 0; i < chunks.length; i += 1) { + await _bluetoothManager.writeData(chunks[i]); + sleep(Duration(milliseconds: _queueSleepTimeMs)); + } + _isPrinting = false; + _bufferedBytes = []; + } } From 642a5ee10559eb818587cbb8b81a67cfd75979fb Mon Sep 17 00:00:00 2001 From: Ivan Ray Altomera Date: Tue, 18 Aug 2020 18:07:43 +0800 Subject: [PATCH 03/21] fix: disconnect after timeout --- lib/src/printer_bluetooth_manager.dart | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/src/printer_bluetooth_manager.dart b/lib/src/printer_bluetooth_manager.dart index b7b5e1f..c01317d 100644 --- a/lib/src/printer_bluetooth_manager.dart +++ b/lib/src/printer_bluetooth_manager.dart @@ -8,9 +8,11 @@ import 'dart:async'; import 'dart:io'; + import 'package:esc_pos_utils/esc_pos_utils.dart'; -import 'package:rxdart/rxdart.dart'; import 'package:flutter_bluetooth_basic/flutter_bluetooth_basic.dart'; +import 'package:rxdart/rxdart.dart'; + import './enums.dart'; /// Bluetooth printer @@ -80,7 +82,6 @@ class PrinterBluetoothManager { if (_bufferedBytes.isNotEmpty) { await _writePending(); } - await _bluetoothManager.disconnect(); break; case BluetoothManager.DISCONNECTED: _isConnected = false; @@ -120,6 +121,7 @@ class PrinterBluetoothManager { completer.complete(PosPrintResult.timeout); } completer.complete(PosPrintResult.success); + await _bluetoothManager.disconnect(); }); return completer.future; From 8c1065a31dd13c61c56e247e39a134b6d9a9d975 Mon Sep 17 00:00:00 2001 From: louieseno Date: Thu, 24 Sep 2020 14:47:15 +0800 Subject: [PATCH 04/21] feat: add printlabel function --- example/blue/pubspec.lock | 385 +++++++++++++++++++++++++ lib/src/printer_bluetooth_manager.dart | 20 ++ pubspec.lock | 252 ++++++++++++++++ 3 files changed, 657 insertions(+) create mode 100644 example/blue/pubspec.lock create mode 100644 pubspec.lock diff --git a/example/blue/pubspec.lock b/example/blue/pubspec.lock new file mode 100644 index 0000000..91fcb94 --- /dev/null +++ b/example/blue/pubspec.lock @@ -0,0 +1,385 @@ +# Generated by pub +# See https://dart.dev/tools/pub/glossary#lockfile +packages: + archive: + dependency: transitive + description: + name: archive + url: "https://pub.dartlang.org" + source: hosted + version: "2.0.13" + args: + dependency: transitive + description: + name: args + url: "https://pub.dartlang.org" + source: hosted + version: "1.6.0" + async: + dependency: transitive + description: + name: async + url: "https://pub.dartlang.org" + source: hosted + version: "2.4.2" + boolean_selector: + dependency: transitive + description: + name: boolean_selector + url: "https://pub.dartlang.org" + source: hosted + version: "2.0.0" + characters: + dependency: transitive + description: + name: characters + url: "https://pub.dartlang.org" + source: hosted + version: "1.0.0" + charcode: + dependency: transitive + description: + name: charcode + url: "https://pub.dartlang.org" + source: hosted + version: "1.1.3" + charset_converter: + dependency: "direct main" + description: + name: charset_converter + url: "https://pub.dartlang.org" + source: hosted + version: "1.0.3" + clock: + dependency: transitive + description: + name: clock + url: "https://pub.dartlang.org" + source: hosted + version: "1.0.1" + collection: + dependency: transitive + description: + name: collection + url: "https://pub.dartlang.org" + source: hosted + version: "1.14.13" + convert: + dependency: transitive + description: + name: convert + url: "https://pub.dartlang.org" + source: hosted + version: "2.1.1" + crypto: + dependency: transitive + description: + name: crypto + url: "https://pub.dartlang.org" + source: hosted + version: "2.1.5" + csslib: + dependency: transitive + description: + name: csslib + url: "https://pub.dartlang.org" + source: hosted + version: "0.16.2" + cupertino_icons: + dependency: "direct main" + description: + name: cupertino_icons + url: "https://pub.dartlang.org" + source: hosted + version: "0.1.3" + esc_pos_bluetooth: + dependency: "direct main" + description: + path: "../.." + relative: true + source: path + version: "0.2.8" + esc_pos_utils: + dependency: "direct main" + description: + name: esc_pos_utils + url: "https://pub.dartlang.org" + source: hosted + version: "0.3.6" + fake_async: + dependency: transitive + description: + name: fake_async + url: "https://pub.dartlang.org" + source: hosted + version: "1.1.0" + ffi: + dependency: transitive + description: + name: ffi + url: "https://pub.dartlang.org" + source: hosted + version: "0.1.3" + file: + dependency: transitive + description: + name: file + url: "https://pub.dartlang.org" + source: hosted + version: "5.2.1" + flutter: + dependency: "direct main" + 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.5" + flutter_test: + dependency: "direct dev" + description: flutter + source: sdk + version: "0.0.0" + gbk_codec: + dependency: transitive + description: + name: gbk_codec + url: "https://pub.dartlang.org" + source: hosted + version: "0.3.2" + hex: + dependency: transitive + description: + name: hex + url: "https://pub.dartlang.org" + source: hosted + version: "0.1.2" + html: + dependency: transitive + description: + name: html + url: "https://pub.dartlang.org" + source: hosted + version: "0.14.0+3" + image: + dependency: "direct main" + description: + name: image + url: "https://pub.dartlang.org" + source: hosted + version: "2.1.17" + intl: + dependency: "direct main" + description: + name: intl + url: "https://pub.dartlang.org" + source: hosted + version: "0.16.1" + json_annotation: + dependency: transitive + description: + name: json_annotation + url: "https://pub.dartlang.org" + source: hosted + version: "3.1.0" + matcher: + dependency: transitive + description: + name: matcher + url: "https://pub.dartlang.org" + source: hosted + version: "0.12.8" + meta: + dependency: transitive + description: + name: meta + url: "https://pub.dartlang.org" + source: hosted + version: "1.1.8" + oktoast: + dependency: "direct main" + description: + name: oktoast + url: "https://pub.dartlang.org" + source: hosted + version: "2.3.2" + path: + dependency: transitive + description: + name: path + url: "https://pub.dartlang.org" + source: hosted + version: "1.7.0" + path_provider: + dependency: "direct main" + description: + name: path_provider + url: "https://pub.dartlang.org" + source: hosted + version: "1.6.18" + path_provider_linux: + dependency: transitive + description: + name: path_provider_linux + url: "https://pub.dartlang.org" + source: hosted + version: "0.0.1+2" + path_provider_macos: + dependency: transitive + description: + name: path_provider_macos + url: "https://pub.dartlang.org" + source: hosted + version: "0.0.4+4" + path_provider_platform_interface: + dependency: transitive + description: + name: path_provider_platform_interface + url: "https://pub.dartlang.org" + source: hosted + version: "1.0.3" + path_provider_windows: + dependency: transitive + description: + name: path_provider_windows + url: "https://pub.dartlang.org" + source: hosted + version: "0.0.4+1" + petitparser: + dependency: transitive + description: + name: petitparser + url: "https://pub.dartlang.org" + source: hosted + version: "3.0.4" + platform: + dependency: transitive + description: + name: platform + url: "https://pub.dartlang.org" + source: hosted + version: "2.2.1" + plugin_platform_interface: + dependency: transitive + description: + name: plugin_platform_interface + url: "https://pub.dartlang.org" + source: hosted + version: "1.0.2" + process: + dependency: transitive + description: + name: process + url: "https://pub.dartlang.org" + source: hosted + version: "3.0.13" + qr: + dependency: transitive + description: + name: qr + url: "https://pub.dartlang.org" + source: hosted + version: "1.3.0" + qr_flutter: + dependency: "direct main" + description: + name: qr_flutter + url: "https://pub.dartlang.org" + source: hosted + version: "3.2.0" + rxdart: + dependency: transitive + description: + name: rxdart + url: "https://pub.dartlang.org" + source: hosted + version: "0.23.1" + sky_engine: + dependency: transitive + description: flutter + source: sdk + version: "0.0.99" + source_span: + dependency: transitive + description: + name: source_span + url: "https://pub.dartlang.org" + source: hosted + version: "1.7.0" + stack_trace: + dependency: transitive + description: + name: stack_trace + url: "https://pub.dartlang.org" + source: hosted + version: "1.9.5" + stream_channel: + dependency: transitive + description: + name: stream_channel + url: "https://pub.dartlang.org" + source: hosted + version: "2.0.0" + string_scanner: + dependency: transitive + description: + name: string_scanner + url: "https://pub.dartlang.org" + source: hosted + version: "1.0.5" + term_glyph: + dependency: transitive + description: + name: term_glyph + url: "https://pub.dartlang.org" + source: hosted + version: "1.1.0" + test_api: + dependency: transitive + description: + name: test_api + url: "https://pub.dartlang.org" + source: hosted + version: "0.2.17" + typed_data: + dependency: transitive + description: + name: typed_data + url: "https://pub.dartlang.org" + source: hosted + version: "1.2.0" + vector_math: + dependency: transitive + description: + name: vector_math + url: "https://pub.dartlang.org" + source: hosted + version: "2.0.8" + win32: + dependency: transitive + description: + name: win32 + url: "https://pub.dartlang.org" + source: hosted + version: "1.7.3" + xdg_directories: + dependency: transitive + description: + name: xdg_directories + url: "https://pub.dartlang.org" + source: hosted + version: "0.1.0" + xml: + dependency: transitive + description: + name: xml + url: "https://pub.dartlang.org" + source: hosted + version: "4.5.1" +sdks: + dart: ">=2.9.0-14.0.dev <3.0.0" + flutter: ">=1.12.13+hotfix.5 <2.0.0" diff --git a/lib/src/printer_bluetooth_manager.dart b/lib/src/printer_bluetooth_manager.dart index c01317d..b70f5c6 100644 --- a/lib/src/printer_bluetooth_manager.dart +++ b/lib/src/printer_bluetooth_manager.dart @@ -147,6 +147,26 @@ class PrinterBluetoothManager { ); } + Future printLabel( + List bytes, { + int chunkSizeBytes = 20, + int queueSleepTimeMs = 20, + int timeout = 5, + }) async { + if (bytes == null || bytes.isEmpty) { + return Future.value(PosPrintResult.ticketEmpty); + } + + _bufferedBytes = bytes; + _queueSleepTimeMs = queueSleepTimeMs; + _chunkSizeBytes = chunkSizeBytes; + + return writeBytes( + bytes, + timeout: timeout, + ); + } + Future _writePending() async { final len = _bufferedBytes.length; List> chunks = []; diff --git a/pubspec.lock b/pubspec.lock new file mode 100644 index 0000000..c600bc2 --- /dev/null +++ b/pubspec.lock @@ -0,0 +1,252 @@ +# Generated by pub +# See https://dart.dev/tools/pub/glossary#lockfile +packages: + archive: + dependency: transitive + description: + name: archive + url: "https://pub.dartlang.org" + source: hosted + version: "2.0.13" + args: + dependency: transitive + description: + name: args + url: "https://pub.dartlang.org" + source: hosted + version: "1.6.0" + async: + dependency: transitive + description: + name: async + url: "https://pub.dartlang.org" + source: hosted + version: "2.4.2" + boolean_selector: + dependency: transitive + description: + name: boolean_selector + url: "https://pub.dartlang.org" + source: hosted + version: "2.0.0" + characters: + dependency: transitive + description: + name: characters + url: "https://pub.dartlang.org" + source: hosted + version: "1.0.0" + charcode: + dependency: transitive + description: + name: charcode + url: "https://pub.dartlang.org" + source: hosted + version: "1.1.3" + clock: + dependency: transitive + description: + name: clock + url: "https://pub.dartlang.org" + source: hosted + version: "1.0.1" + collection: + dependency: transitive + description: + name: collection + url: "https://pub.dartlang.org" + source: hosted + version: "1.14.13" + convert: + dependency: transitive + description: + name: convert + url: "https://pub.dartlang.org" + source: hosted + version: "2.1.1" + crypto: + dependency: transitive + description: + name: crypto + url: "https://pub.dartlang.org" + source: hosted + version: "2.1.5" + csslib: + dependency: transitive + description: + name: csslib + url: "https://pub.dartlang.org" + source: hosted + version: "0.16.2" + esc_pos_utils: + dependency: "direct main" + description: + name: esc_pos_utils + url: "https://pub.dartlang.org" + source: hosted + version: "0.3.6" + fake_async: + dependency: transitive + description: + name: fake_async + url: "https://pub.dartlang.org" + source: hosted + version: "1.1.0" + flutter: + dependency: "direct main" + description: flutter + source: sdk + version: "0.0.0" + flutter_bluetooth_basic: + dependency: "direct main" + description: + name: flutter_bluetooth_basic + url: "https://pub.dartlang.org" + source: hosted + version: "0.1.5" + flutter_test: + dependency: "direct dev" + description: flutter + source: sdk + version: "0.0.0" + gbk_codec: + dependency: transitive + description: + name: gbk_codec + url: "https://pub.dartlang.org" + source: hosted + version: "0.3.2" + hex: + dependency: transitive + description: + name: hex + url: "https://pub.dartlang.org" + source: hosted + version: "0.1.2" + html: + dependency: transitive + description: + name: html + url: "https://pub.dartlang.org" + source: hosted + version: "0.14.0+3" + image: + dependency: transitive + description: + name: image + url: "https://pub.dartlang.org" + source: hosted + version: "2.1.17" + json_annotation: + dependency: transitive + description: + name: json_annotation + url: "https://pub.dartlang.org" + source: hosted + version: "3.1.0" + matcher: + dependency: transitive + description: + name: matcher + url: "https://pub.dartlang.org" + source: hosted + version: "0.12.8" + meta: + dependency: transitive + description: + name: meta + url: "https://pub.dartlang.org" + source: hosted + version: "1.1.8" + path: + dependency: transitive + description: + name: path + url: "https://pub.dartlang.org" + source: hosted + version: "1.7.0" + petitparser: + dependency: transitive + description: + name: petitparser + url: "https://pub.dartlang.org" + source: hosted + version: "3.0.4" + rxdart: + dependency: "direct main" + description: + name: rxdart + url: "https://pub.dartlang.org" + source: hosted + version: "0.23.1" + sky_engine: + dependency: transitive + description: flutter + source: sdk + version: "0.0.99" + source_span: + dependency: transitive + description: + name: source_span + url: "https://pub.dartlang.org" + source: hosted + version: "1.7.0" + stack_trace: + dependency: transitive + description: + name: stack_trace + url: "https://pub.dartlang.org" + source: hosted + version: "1.9.5" + stream_channel: + dependency: transitive + description: + name: stream_channel + url: "https://pub.dartlang.org" + source: hosted + version: "2.0.0" + string_scanner: + dependency: transitive + description: + name: string_scanner + url: "https://pub.dartlang.org" + source: hosted + version: "1.0.5" + term_glyph: + dependency: transitive + description: + name: term_glyph + url: "https://pub.dartlang.org" + source: hosted + version: "1.1.0" + test_api: + dependency: transitive + description: + name: test_api + url: "https://pub.dartlang.org" + source: hosted + version: "0.2.17" + typed_data: + dependency: transitive + description: + name: typed_data + url: "https://pub.dartlang.org" + source: hosted + version: "1.2.0" + vector_math: + dependency: transitive + description: + name: vector_math + url: "https://pub.dartlang.org" + source: hosted + version: "2.0.8" + xml: + dependency: transitive + description: + name: xml + url: "https://pub.dartlang.org" + source: hosted + version: "4.5.1" +sdks: + dart: ">=2.9.0-14.0.dev <3.0.0" + flutter: ">=1.12.0 <2.0.0" From 4b551affd0dc7465e8b60e82adeebc978782c037 Mon Sep 17 00:00:00 2001 From: louieseno Date: Fri, 25 Sep 2020 15:25:44 +0800 Subject: [PATCH 05/21] test: reinitialized bufferedBytes --- lib/src/printer_bluetooth_manager.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/src/printer_bluetooth_manager.dart b/lib/src/printer_bluetooth_manager.dart index b70f5c6..e4da21a 100644 --- a/lib/src/printer_bluetooth_manager.dart +++ b/lib/src/printer_bluetooth_manager.dart @@ -156,7 +156,7 @@ class PrinterBluetoothManager { if (bytes == null || bytes.isEmpty) { return Future.value(PosPrintResult.ticketEmpty); } - + _bufferedBytes = []; _bufferedBytes = bytes; _queueSleepTimeMs = queueSleepTimeMs; _chunkSizeBytes = chunkSizeBytes; From fa27d53b14f405b9e44202611699988f3b015651 Mon Sep 17 00:00:00 2001 From: louieseno Date: Fri, 20 Nov 2020 13:20:07 +0800 Subject: [PATCH 06/21] refactor: move disconnect on write bytes and add 2 seconds for scan --- example/blue/pubspec.lock | 38 +++++++++++++------------- lib/src/printer_bluetooth_manager.dart | 15 ++++++---- pubspec.lock | 38 +++++++++++++------------- 3 files changed, 48 insertions(+), 43 deletions(-) diff --git a/example/blue/pubspec.lock b/example/blue/pubspec.lock index 91fcb94..e1077d2 100644 --- a/example/blue/pubspec.lock +++ b/example/blue/pubspec.lock @@ -21,28 +21,28 @@ packages: name: async url: "https://pub.dartlang.org" source: hosted - version: "2.4.2" + version: "2.5.0-nullsafety.1" boolean_selector: dependency: transitive description: name: boolean_selector url: "https://pub.dartlang.org" source: hosted - version: "2.0.0" + version: "2.1.0-nullsafety.1" characters: dependency: transitive description: name: characters url: "https://pub.dartlang.org" source: hosted - version: "1.0.0" + version: "1.1.0-nullsafety.3" charcode: dependency: transitive description: name: charcode url: "https://pub.dartlang.org" source: hosted - version: "1.1.3" + version: "1.2.0-nullsafety.1" charset_converter: dependency: "direct main" description: @@ -56,14 +56,14 @@ packages: name: clock url: "https://pub.dartlang.org" source: hosted - version: "1.0.1" + version: "1.1.0-nullsafety.1" collection: dependency: transitive description: name: collection url: "https://pub.dartlang.org" source: hosted - version: "1.14.13" + version: "1.15.0-nullsafety.3" convert: dependency: transitive description: @@ -112,7 +112,7 @@ packages: name: fake_async url: "https://pub.dartlang.org" source: hosted - version: "1.1.0" + version: "1.2.0-nullsafety.1" ffi: dependency: transitive description: @@ -192,14 +192,14 @@ packages: name: matcher url: "https://pub.dartlang.org" source: hosted - version: "0.12.8" + version: "0.12.10-nullsafety.1" meta: dependency: transitive description: name: meta url: "https://pub.dartlang.org" source: hosted - version: "1.1.8" + version: "1.3.0-nullsafety.3" oktoast: dependency: "direct main" description: @@ -213,7 +213,7 @@ packages: name: path url: "https://pub.dartlang.org" source: hosted - version: "1.7.0" + version: "1.8.0-nullsafety.1" path_provider: dependency: "direct main" description: @@ -309,56 +309,56 @@ packages: name: source_span url: "https://pub.dartlang.org" source: hosted - version: "1.7.0" + version: "1.8.0-nullsafety.2" stack_trace: dependency: transitive description: name: stack_trace url: "https://pub.dartlang.org" source: hosted - version: "1.9.5" + version: "1.10.0-nullsafety.1" stream_channel: dependency: transitive description: name: stream_channel url: "https://pub.dartlang.org" source: hosted - version: "2.0.0" + version: "2.1.0-nullsafety.1" string_scanner: dependency: transitive description: name: string_scanner url: "https://pub.dartlang.org" source: hosted - version: "1.0.5" + version: "1.1.0-nullsafety.1" term_glyph: dependency: transitive description: name: term_glyph url: "https://pub.dartlang.org" source: hosted - version: "1.1.0" + version: "1.2.0-nullsafety.1" test_api: dependency: transitive description: name: test_api url: "https://pub.dartlang.org" source: hosted - version: "0.2.17" + version: "0.2.19-nullsafety.2" typed_data: dependency: transitive description: name: typed_data url: "https://pub.dartlang.org" source: hosted - version: "1.2.0" + version: "1.3.0-nullsafety.3" vector_math: dependency: transitive description: name: vector_math url: "https://pub.dartlang.org" source: hosted - version: "2.0.8" + version: "2.1.0-nullsafety.3" win32: dependency: transitive description: @@ -381,5 +381,5 @@ packages: source: hosted version: "4.5.1" sdks: - dart: ">=2.9.0-14.0.dev <3.0.0" + dart: ">=2.10.0-110 <2.11.0" flutter: ">=1.12.13+hotfix.5 <2.0.0" diff --git a/lib/src/printer_bluetooth_manager.dart b/lib/src/printer_bluetooth_manager.dart index e4da21a..b5fd3e1 100644 --- a/lib/src/printer_bluetooth_manager.dart +++ b/lib/src/printer_bluetooth_manager.dart @@ -1,7 +1,7 @@ /* * esc_pos_bluetooth * Created by Andrey Ushakov - * + * * Copyright (c) 2019-2020. All rights reserved. * See LICENSE for distribution and usage details. */ @@ -10,6 +10,7 @@ import 'dart:async'; import 'dart:io'; import 'package:esc_pos_utils/esc_pos_utils.dart'; +import 'package:flutter/cupertino.dart'; import 'package:flutter_bluetooth_basic/flutter_bluetooth_basic.dart'; import 'package:rxdart/rxdart.dart'; @@ -44,6 +45,7 @@ class PrinterBluetoothManager { List _bufferedBytes = []; int _queueSleepTimeMs = 20; int _chunkSizeBytes = 20; + int _timeOut = 5; Future _runDelayed(int seconds) { return Future.delayed(Duration(seconds: seconds)); @@ -108,7 +110,7 @@ class PrinterBluetoothManager { } // We have to rescan before connecting, otherwise we can connect only once - await _bluetoothManager.startScan(timeout: Duration(seconds: 1)); + await _bluetoothManager.startScan(timeout: Duration(seconds: _timeOut + 2)); await _bluetoothManager.stopScan(); // Connect @@ -121,7 +123,7 @@ class PrinterBluetoothManager { completer.complete(PosPrintResult.timeout); } completer.complete(PosPrintResult.success); - await _bluetoothManager.disconnect(); + // await _bluetoothManager.disconnect(); }); return completer.future; @@ -140,7 +142,7 @@ class PrinterBluetoothManager { _bufferedBytes = ticket.bytes; _queueSleepTimeMs = queueSleepTimeMs; _chunkSizeBytes = chunkSizeBytes; - + _timeOut = timeout; return writeBytes( ticket.bytes, timeout: timeout, @@ -160,7 +162,7 @@ class PrinterBluetoothManager { _bufferedBytes = bytes; _queueSleepTimeMs = queueSleepTimeMs; _chunkSizeBytes = chunkSizeBytes; - + _timeOut = timeout; return writeBytes( bytes, timeout: timeout, @@ -181,5 +183,8 @@ class PrinterBluetoothManager { } _isPrinting = false; _bufferedBytes = []; + _runDelayed(_timeOut).then((dynamic v) async { + await _bluetoothManager.disconnect(); + }); } } diff --git a/pubspec.lock b/pubspec.lock index c600bc2..bf375cd 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -21,42 +21,42 @@ packages: name: async url: "https://pub.dartlang.org" source: hosted - version: "2.4.2" + version: "2.5.0-nullsafety.1" boolean_selector: dependency: transitive description: name: boolean_selector url: "https://pub.dartlang.org" source: hosted - version: "2.0.0" + version: "2.1.0-nullsafety.1" characters: dependency: transitive description: name: characters url: "https://pub.dartlang.org" source: hosted - version: "1.0.0" + version: "1.1.0-nullsafety.3" charcode: dependency: transitive description: name: charcode url: "https://pub.dartlang.org" source: hosted - version: "1.1.3" + version: "1.2.0-nullsafety.1" clock: dependency: transitive description: name: clock url: "https://pub.dartlang.org" source: hosted - version: "1.0.1" + version: "1.1.0-nullsafety.1" collection: dependency: transitive description: name: collection url: "https://pub.dartlang.org" source: hosted - version: "1.14.13" + version: "1.15.0-nullsafety.3" convert: dependency: transitive description: @@ -91,7 +91,7 @@ packages: name: fake_async url: "https://pub.dartlang.org" source: hosted - version: "1.1.0" + version: "1.2.0-nullsafety.1" flutter: dependency: "direct main" description: flutter @@ -150,21 +150,21 @@ packages: name: matcher url: "https://pub.dartlang.org" source: hosted - version: "0.12.8" + version: "0.12.10-nullsafety.1" meta: dependency: transitive description: name: meta url: "https://pub.dartlang.org" source: hosted - version: "1.1.8" + version: "1.3.0-nullsafety.3" path: dependency: transitive description: name: path url: "https://pub.dartlang.org" source: hosted - version: "1.7.0" + version: "1.8.0-nullsafety.1" petitparser: dependency: transitive description: @@ -190,56 +190,56 @@ packages: name: source_span url: "https://pub.dartlang.org" source: hosted - version: "1.7.0" + version: "1.8.0-nullsafety.2" stack_trace: dependency: transitive description: name: stack_trace url: "https://pub.dartlang.org" source: hosted - version: "1.9.5" + version: "1.10.0-nullsafety.1" stream_channel: dependency: transitive description: name: stream_channel url: "https://pub.dartlang.org" source: hosted - version: "2.0.0" + version: "2.1.0-nullsafety.1" string_scanner: dependency: transitive description: name: string_scanner url: "https://pub.dartlang.org" source: hosted - version: "1.0.5" + version: "1.1.0-nullsafety.1" term_glyph: dependency: transitive description: name: term_glyph url: "https://pub.dartlang.org" source: hosted - version: "1.1.0" + version: "1.2.0-nullsafety.1" test_api: dependency: transitive description: name: test_api url: "https://pub.dartlang.org" source: hosted - version: "0.2.17" + version: "0.2.19-nullsafety.2" typed_data: dependency: transitive description: name: typed_data url: "https://pub.dartlang.org" source: hosted - version: "1.2.0" + version: "1.3.0-nullsafety.3" vector_math: dependency: transitive description: name: vector_math url: "https://pub.dartlang.org" source: hosted - version: "2.0.8" + version: "2.1.0-nullsafety.3" xml: dependency: transitive description: @@ -248,5 +248,5 @@ packages: source: hosted version: "4.5.1" sdks: - dart: ">=2.9.0-14.0.dev <3.0.0" + dart: ">=2.10.0-110 <2.11.0" flutter: ">=1.12.0 <2.0.0" From 278b7b51c8bdd911cbc71d4f34bc38ac75f7272d Mon Sep 17 00:00:00 2001 From: louieseno Date: Fri, 20 Nov 2020 13:45:37 +0800 Subject: [PATCH 07/21] test: remove plus 2 duration on scan --- lib/src/printer_bluetooth_manager.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/src/printer_bluetooth_manager.dart b/lib/src/printer_bluetooth_manager.dart index b5fd3e1..d1df4f0 100644 --- a/lib/src/printer_bluetooth_manager.dart +++ b/lib/src/printer_bluetooth_manager.dart @@ -110,7 +110,7 @@ class PrinterBluetoothManager { } // We have to rescan before connecting, otherwise we can connect only once - await _bluetoothManager.startScan(timeout: Duration(seconds: _timeOut + 2)); + await _bluetoothManager.startScan(timeout: Duration(seconds: _timeOut)); await _bluetoothManager.stopScan(); // Connect From 00699c21e3dcf05ea5eadae551f0bb825b666223 Mon Sep 17 00:00:00 2001 From: louieseno Date: Fri, 20 Nov 2020 13:57:48 +0800 Subject: [PATCH 08/21] test: move inside timer reset printing and buffer state --- lib/src/printer_bluetooth_manager.dart | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/src/printer_bluetooth_manager.dart b/lib/src/printer_bluetooth_manager.dart index d1df4f0..2b625de 100644 --- a/lib/src/printer_bluetooth_manager.dart +++ b/lib/src/printer_bluetooth_manager.dart @@ -181,10 +181,10 @@ class PrinterBluetoothManager { await _bluetoothManager.writeData(chunks[i]); sleep(Duration(milliseconds: _queueSleepTimeMs)); } - _isPrinting = false; - _bufferedBytes = []; _runDelayed(_timeOut).then((dynamic v) async { await _bluetoothManager.disconnect(); + _isPrinting = false; + _bufferedBytes = []; }); } } From 9c498dda67c83a2d3ec56faed19619931c7c24a6 Mon Sep 17 00:00:00 2001 From: louieseno Date: Fri, 20 Nov 2020 14:11:16 +0800 Subject: [PATCH 09/21] fix: bad state complete --- lib/src/printer_bluetooth_manager.dart | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/src/printer_bluetooth_manager.dart b/lib/src/printer_bluetooth_manager.dart index 2b625de..d1df4f0 100644 --- a/lib/src/printer_bluetooth_manager.dart +++ b/lib/src/printer_bluetooth_manager.dart @@ -181,10 +181,10 @@ class PrinterBluetoothManager { await _bluetoothManager.writeData(chunks[i]); sleep(Duration(milliseconds: _queueSleepTimeMs)); } + _isPrinting = false; + _bufferedBytes = []; _runDelayed(_timeOut).then((dynamic v) async { await _bluetoothManager.disconnect(); - _isPrinting = false; - _bufferedBytes = []; }); } } From 70c0328d752695d953c1c3f5c745c695d4fc7a94 Mon Sep 17 00:00:00 2001 From: louieseno Date: Fri, 20 Nov 2020 14:37:27 +0800 Subject: [PATCH 10/21] feat: add 500 milliseconds delay before write bytes --- lib/src/printer_bluetooth_manager.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/src/printer_bluetooth_manager.dart b/lib/src/printer_bluetooth_manager.dart index d1df4f0..1405d5f 100644 --- a/lib/src/printer_bluetooth_manager.dart +++ b/lib/src/printer_bluetooth_manager.dart @@ -100,7 +100,7 @@ class PrinterBluetoothManager { int timeout = 5, }) async { final Completer completer = Completer(); - + await Future.delayed(Duration(milliseconds: 500)); if (_selectedPrinter == null) { return Future.value(PosPrintResult.printerNotSelected); } else if (_isScanning.value) { From b63d7dd28ff24be1f91313ec4d6c0d64a4a5026b Mon Sep 17 00:00:00 2001 From: louieseno Date: Wed, 24 Feb 2021 10:15:47 +0800 Subject: [PATCH 11/21] fix: timeout scan and disconnect static --- lib/src/printer_bluetooth_manager.dart | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/lib/src/printer_bluetooth_manager.dart b/lib/src/printer_bluetooth_manager.dart index 1405d5f..df7d201 100644 --- a/lib/src/printer_bluetooth_manager.dart +++ b/lib/src/printer_bluetooth_manager.dart @@ -10,7 +10,6 @@ import 'dart:async'; import 'dart:io'; import 'package:esc_pos_utils/esc_pos_utils.dart'; -import 'package:flutter/cupertino.dart'; import 'package:flutter_bluetooth_basic/flutter_bluetooth_basic.dart'; import 'package:rxdart/rxdart.dart'; @@ -108,9 +107,8 @@ class PrinterBluetoothManager { } else if (_isPrinting) { return Future.value(PosPrintResult.printInProgress); } - // We have to rescan before connecting, otherwise we can connect only once - await _bluetoothManager.startScan(timeout: Duration(seconds: _timeOut)); + await _bluetoothManager.startScan(timeout: Duration(seconds: 1)); await _bluetoothManager.stopScan(); // Connect @@ -121,9 +119,9 @@ class PrinterBluetoothManager { if (_isPrinting) { _isPrinting = false; completer.complete(PosPrintResult.timeout); + await _bluetoothManager.disconnect(); } completer.complete(PosPrintResult.success); - // await _bluetoothManager.disconnect(); }); return completer.future; @@ -138,7 +136,7 @@ class PrinterBluetoothManager { if (ticket == null || ticket.bytes.isEmpty) { return Future.value(PosPrintResult.ticketEmpty); } - + _bufferedBytes = []; _bufferedBytes = ticket.bytes; _queueSleepTimeMs = queueSleepTimeMs; _chunkSizeBytes = chunkSizeBytes; @@ -183,8 +181,10 @@ class PrinterBluetoothManager { } _isPrinting = false; _bufferedBytes = []; - _runDelayed(_timeOut).then((dynamic v) async { - await _bluetoothManager.disconnect(); - }); + if (_isConnected) { + _runDelayed(3).then((dynamic v) async { + await _bluetoothManager.disconnect(); + }); + } } } From 36176bef1d6d2f5c73fd3b9250b60fdab5fe756c Mon Sep 17 00:00:00 2001 From: louieseno Date: Tue, 2 Mar 2021 13:48:27 +0800 Subject: [PATCH 12/21] fix: separate disconnection request --- lib/src/printer_bluetooth_manager.dart | 76 +++++++++++++++++--------- 1 file changed, 50 insertions(+), 26 deletions(-) diff --git a/lib/src/printer_bluetooth_manager.dart b/lib/src/printer_bluetooth_manager.dart index df7d201..2e52050 100644 --- a/lib/src/printer_bluetooth_manager.dart +++ b/lib/src/printer_bluetooth_manager.dart @@ -10,6 +10,7 @@ import 'dart:async'; import 'dart:io'; import 'package:esc_pos_utils/esc_pos_utils.dart'; +import 'package:flutter/cupertino.dart'; import 'package:flutter_bluetooth_basic/flutter_bluetooth_basic.dart'; import 'package:rxdart/rxdart.dart'; @@ -80,12 +81,13 @@ class PrinterBluetoothManager { switch (state) { case BluetoothManager.CONNECTED: _isConnected = true; - if (_bufferedBytes.isNotEmpty) { - await _writePending(); - } + print('CONNECTED STATE'); + print('CONNECTED STATE'); break; case BluetoothManager.DISCONNECTED: _isConnected = false; + print('DISCONNECTED STATE'); + print('DISCONNECTED STATE'); break; default: break; @@ -94,7 +96,7 @@ class PrinterBluetoothManager { }); } - Future writeBytes( + Future _connectBluetooth( List bytes, { int timeout = 5, }) async { @@ -114,16 +116,23 @@ class PrinterBluetoothManager { // Connect await _bluetoothManager.connect(_selectedPrinter._device); - // Printing timeout - _runDelayed(timeout).then((dynamic v) async { - if (_isPrinting) { - _isPrinting = false; - completer.complete(PosPrintResult.timeout); - await _bluetoothManager.disconnect(); - } - completer.complete(PosPrintResult.success); - }); + return Future.value(PosPrintResult.success); + } + Future _writeRequest(timeout) async { + final Completer completer = Completer(); + if (_bufferedBytes.isNotEmpty) { + await _writePending(); + _runDelayed(timeout).then((dynamic v) async { + if (_isPrinting) { + _isPrinting = false; + completer.complete(PosPrintResult.timeout); + await _bluetoothManager.disconnect(); + print('TIMEOUT'); + } + completer.complete(PosPrintResult.success); + }); + } return completer.future; } @@ -141,10 +150,17 @@ class PrinterBluetoothManager { _queueSleepTimeMs = queueSleepTimeMs; _chunkSizeBytes = chunkSizeBytes; _timeOut = timeout; - return writeBytes( - ticket.bytes, - timeout: timeout, - ); + if (!_isConnected) { + final result = await _connectBluetooth( + ticket.bytes, + timeout: timeout, + ); + + if (result.msg != 'Success') { + return result; + } + } + return await _writeRequest(timeout); } Future printLabel( @@ -161,10 +177,23 @@ class PrinterBluetoothManager { _queueSleepTimeMs = queueSleepTimeMs; _chunkSizeBytes = chunkSizeBytes; _timeOut = timeout; - return writeBytes( - bytes, - timeout: timeout, - ); + if (!_isConnected) { + final result = await _connectBluetooth( + bytes, + timeout: timeout, + ); + if (result.msg != 'Success') { + return result; + } + } + return await _writeRequest(timeout); + } + + disconnect(timeout) { + _runDelayed(timeout).then((dynamic v) async { + await _bluetoothManager.disconnect(); + print('PENDING DISCONNECTED'); + }); } Future _writePending() async { @@ -181,10 +210,5 @@ class PrinterBluetoothManager { } _isPrinting = false; _bufferedBytes = []; - if (_isConnected) { - _runDelayed(3).then((dynamic v) async { - await _bluetoothManager.disconnect(); - }); - } } } From e8687078050d3bf8f3e7ea89c1a81a88b8baa654 Mon Sep 17 00:00:00 2001 From: louieseno Date: Tue, 9 Mar 2021 08:57:07 +0800 Subject: [PATCH 13/21] feat: check connection state --- lib/src/printer_bluetooth_manager.dart | 48 ++++++++++++++++++++++---- 1 file changed, 41 insertions(+), 7 deletions(-) diff --git a/lib/src/printer_bluetooth_manager.dart b/lib/src/printer_bluetooth_manager.dart index 2e52050..fda9c14 100644 --- a/lib/src/printer_bluetooth_manager.dart +++ b/lib/src/printer_bluetooth_manager.dart @@ -96,12 +96,37 @@ class PrinterBluetoothManager { }); } + Future _checkConnectionState() async { + Timer _stateTimer; + int _start = 10; + final Completer completer = Completer(); + const oneSec = Duration(seconds: 1); + _stateTimer = Timer.periodic( + oneSec, + (Timer timer) { + if (_start == 0 || _isConnected) { + timer.cancel(); + print('ENDTIME'); + print(_isConnected); + if (_isConnected) { + _stateTimer?.cancel(); + completer.complete(PosPrintResult.success); + } else { + _stateTimer?.cancel(); + completer.complete(PosPrintResult.timeout); + } + } else { + _start--; + } + }, + ); + return completer.future; + } + Future _connectBluetooth( List bytes, { int timeout = 5, }) async { - final Completer completer = Completer(); - await Future.delayed(Duration(milliseconds: 500)); if (_selectedPrinter == null) { return Future.value(PosPrintResult.printerNotSelected); } else if (_isScanning.value) { @@ -115,8 +140,8 @@ class PrinterBluetoothManager { // Connect await _bluetoothManager.connect(_selectedPrinter._device); - - return Future.value(PosPrintResult.success); + final result = await _checkConnectionState(); + return result; } Future _writeRequest(timeout) async { @@ -150,17 +175,21 @@ class PrinterBluetoothManager { _queueSleepTimeMs = queueSleepTimeMs; _chunkSizeBytes = chunkSizeBytes; _timeOut = timeout; + if (!_isConnected) { final result = await _connectBluetooth( ticket.bytes, timeout: timeout, ); - if (result.msg != 'Success') { return result; } } - return await _writeRequest(timeout); + if (_isConnected) { + return await _writeRequest(timeout); + } else { + return Future.value(PosPrintResult.timeout); + } } Future printLabel( @@ -186,7 +215,12 @@ class PrinterBluetoothManager { return result; } } - return await _writeRequest(timeout); + + if (_isConnected) { + return await _writeRequest(timeout); + } else { + return Future.value(PosPrintResult.timeout); + } } disconnect(timeout) { From 14cad2e2fb68646dc4da4ad77cfe1003c0fd818d Mon Sep 17 00:00:00 2001 From: louieseno Date: Mon, 15 Mar 2021 11:33:27 +0800 Subject: [PATCH 14/21] fix: await disconnect return value --- lib/src/printer_bluetooth_manager.dart | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/lib/src/printer_bluetooth_manager.dart b/lib/src/printer_bluetooth_manager.dart index fda9c14..f6c42c3 100644 --- a/lib/src/printer_bluetooth_manager.dart +++ b/lib/src/printer_bluetooth_manager.dart @@ -223,11 +223,10 @@ class PrinterBluetoothManager { } } - disconnect(timeout) { - _runDelayed(timeout).then((dynamic v) async { - await _bluetoothManager.disconnect(); - print('PENDING DISCONNECTED'); - }); + Future disconnect(timeout) async { + await Future.delayed(Duration(seconds: timeout)); + print('PENDING DISCONNECTED'); + return await _bluetoothManager.disconnect(); } Future _writePending() async { From 4e5f62d4bbe4844b841af41635b3621982a5138b Mon Sep 17 00:00:00 2001 From: louieseno Date: Mon, 5 Apr 2021 15:24:22 +0800 Subject: [PATCH 15/21] feat: disconnect complete future status --- lib/src/printer_bluetooth_manager.dart | 27 ++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/lib/src/printer_bluetooth_manager.dart b/lib/src/printer_bluetooth_manager.dart index f6c42c3..06e3426 100644 --- a/lib/src/printer_bluetooth_manager.dart +++ b/lib/src/printer_bluetooth_manager.dart @@ -224,9 +224,32 @@ class PrinterBluetoothManager { } Future disconnect(timeout) async { - await Future.delayed(Duration(seconds: timeout)); + final Completer completer = Completer(); print('PENDING DISCONNECTED'); - return await _bluetoothManager.disconnect(); + await Future.delayed(Duration(seconds: timeout)); + await _bluetoothManager.disconnect(); + Timer _stateTimer; + int _start = 10; + const oneSec = Duration(seconds: 1); + _stateTimer = Timer.periodic( + oneSec, + (Timer timer) { + if (_start == 0 || !_isConnected) { + timer.cancel(); + if (!_isConnected) { + _stateTimer?.cancel(); + print('SUCCESS DISCONNECT'); + completer.complete(PosPrintResult.success); + } else { + _stateTimer?.cancel(); + completer.complete(PosPrintResult.timeout); + } + } else { + _start--; + } + }, + ); + return completer.future; } Future _writePending() async { From 8f961bdf7826c76703dc6c21716b17203c6d0f02 Mon Sep 17 00:00:00 2001 From: louieseno Date: Thu, 22 Jul 2021 17:28:04 +0800 Subject: [PATCH 16/21] test: pass connection timeout --- lib/src/printer_bluetooth_manager.dart | 154 ++++++++++++++----------- 1 file changed, 88 insertions(+), 66 deletions(-) diff --git a/lib/src/printer_bluetooth_manager.dart b/lib/src/printer_bluetooth_manager.dart index 06e3426..f4efe04 100644 --- a/lib/src/printer_bluetooth_manager.dart +++ b/lib/src/printer_bluetooth_manager.dart @@ -45,7 +45,7 @@ class PrinterBluetoothManager { List _bufferedBytes = []; int _queueSleepTimeMs = 20; int _chunkSizeBytes = 20; - int _timeOut = 5; + int _connectionTimeOut = 5; Future _runDelayed(int seconds) { return Future.delayed(Duration(seconds: seconds)); @@ -96,33 +96,6 @@ class PrinterBluetoothManager { }); } - Future _checkConnectionState() async { - Timer _stateTimer; - int _start = 10; - final Completer completer = Completer(); - const oneSec = Duration(seconds: 1); - _stateTimer = Timer.periodic( - oneSec, - (Timer timer) { - if (_start == 0 || _isConnected) { - timer.cancel(); - print('ENDTIME'); - print(_isConnected); - if (_isConnected) { - _stateTimer?.cancel(); - completer.complete(PosPrintResult.success); - } else { - _stateTimer?.cancel(); - completer.complete(PosPrintResult.timeout); - } - } else { - _start--; - } - }, - ); - return completer.future; - } - Future _connectBluetooth( List bytes, { int timeout = 5, @@ -135,7 +108,10 @@ class PrinterBluetoothManager { return Future.value(PosPrintResult.printInProgress); } // We have to rescan before connecting, otherwise we can connect only once - await _bluetoothManager.startScan(timeout: Duration(seconds: 1)); + final devices = + await _bluetoothManager.startScan(timeout: Duration(seconds: 1)); + print(devices); + print(_selectedPrinter._device); await _bluetoothManager.stopScan(); // Connect @@ -161,12 +137,11 @@ class PrinterBluetoothManager { return completer.future; } - Future printTicket( - Ticket ticket, { - int chunkSizeBytes = 20, - int queueSleepTimeMs = 20, - int timeout = 5, - }) async { + Future printTicket(Ticket ticket, + {int chunkSizeBytes = 20, + int queueSleepTimeMs = 20, + int timeout = 5, + int connectionTimeOut = 10}) async { if (ticket == null || ticket.bytes.isEmpty) { return Future.value(PosPrintResult.ticketEmpty); } @@ -174,30 +149,29 @@ class PrinterBluetoothManager { _bufferedBytes = ticket.bytes; _queueSleepTimeMs = queueSleepTimeMs; _chunkSizeBytes = chunkSizeBytes; - _timeOut = timeout; + _connectionTimeOut = connectionTimeOut; if (!_isConnected) { - final result = await _connectBluetooth( - ticket.bytes, - timeout: timeout, - ); - if (result.msg != 'Success') { - return result; - } + await connect(ticket.bytes, timeout); + } else { + await disconnect(1); + await connect(ticket.bytes, timeout); } + if (_isConnected) { + print("PRINT REQUEST"); + print("PRINT REQUEST"); return await _writeRequest(timeout); } else { return Future.value(PosPrintResult.timeout); } } - Future printLabel( - List bytes, { - int chunkSizeBytes = 20, - int queueSleepTimeMs = 20, - int timeout = 5, - }) async { + Future printLabel(List bytes, + {int chunkSizeBytes = 20, + int queueSleepTimeMs = 20, + int timeout = 5, + int connectionTimeOut = 10}) async { if (bytes == null || bytes.isEmpty) { return Future.value(PosPrintResult.ticketEmpty); } @@ -205,15 +179,13 @@ class PrinterBluetoothManager { _bufferedBytes = bytes; _queueSleepTimeMs = queueSleepTimeMs; _chunkSizeBytes = chunkSizeBytes; - _timeOut = timeout; + _connectionTimeOut = connectionTimeOut; + if (!_isConnected) { - final result = await _connectBluetooth( - bytes, - timeout: timeout, - ); - if (result.msg != 'Success') { - return result; - } + await connect(bytes, timeout); + } else { + await disconnect(1); + await connect(bytes, timeout); } if (_isConnected) { @@ -223,22 +195,20 @@ class PrinterBluetoothManager { } } - Future disconnect(timeout) async { - final Completer completer = Completer(); - print('PENDING DISCONNECTED'); - await Future.delayed(Duration(seconds: timeout)); - await _bluetoothManager.disconnect(); + Future _checkConnectionState() async { Timer _stateTimer; - int _start = 10; + int _start = _connectionTimeOut; + final Completer completer = Completer(); const oneSec = Duration(seconds: 1); _stateTimer = Timer.periodic( oneSec, (Timer timer) { - if (_start == 0 || !_isConnected) { + if (_start == 0 || _isConnected) { timer.cancel(); - if (!_isConnected) { + print('ENDTIME'); + print(_isConnected); + if (_isConnected) { _stateTimer?.cancel(); - print('SUCCESS DISCONNECT'); completer.complete(PosPrintResult.success); } else { _stateTimer?.cancel(); @@ -252,6 +222,58 @@ class PrinterBluetoothManager { return completer.future; } + Future connect(bytes, timeout) async { + print("CONNECTING ON PRINT"); + print("CONNECTING ON PRINT"); + final result = await _connectBluetooth( + bytes, + timeout: timeout, + ); + if (result.msg != 'Success') { + return result; + } + } + + Future disconnect(timeout) async { + final Completer completer = Completer(); + try { + print('PENDING DISCONNECTED'); + await Future.delayed(Duration(seconds: timeout)); + final dynamic disconnected = await _bluetoothManager.disconnect(); + final dynamic destroy = await _bluetoothManager.destroy(); + print(disconnected); + print(destroy); + Timer _stateTimer; + int _start = _connectionTimeOut; + const oneSec = Duration(seconds: 1); + _stateTimer = Timer.periodic( + oneSec, + (Timer timer) { + print("START: $_start"); + print("STATUS: $_isConnected"); + if (_start == 0 || !_isConnected) { + timer.cancel(); + if (!_isConnected) { + _stateTimer?.cancel(); + print('SUCCESS DISCONNECT'); + completer.complete(PosPrintResult.success); + } else { + _stateTimer?.cancel(); + completer.complete(PosPrintResult.timeout); + } + } else { + _start--; + } + }, + ); + } catch (err) { + print(err); + completer.complete(PosPrintResult.timeout); + } + + return completer.future; + } + Future _writePending() async { final len = _bufferedBytes.length; List> chunks = []; From 81535f7455db0810830938a13554c5f073e764d8 Mon Sep 17 00:00:00 2001 From: louieseno Date: Fri, 23 Jul 2021 13:13:34 +0800 Subject: [PATCH 17/21] test: return posprint result --- lib/src/printer_bluetooth_manager.dart | 53 ++++++++------------------ 1 file changed, 15 insertions(+), 38 deletions(-) diff --git a/lib/src/printer_bluetooth_manager.dart b/lib/src/printer_bluetooth_manager.dart index f4efe04..ce15df6 100644 --- a/lib/src/printer_bluetooth_manager.dart +++ b/lib/src/printer_bluetooth_manager.dart @@ -51,7 +51,7 @@ class PrinterBluetoothManager { return Future.delayed(Duration(seconds: seconds)); } - void startScan(Duration timeout) async { + Future startScan(Duration timeout) async { _scanResults.add([]); _bluetoothManager.startScan(timeout: timeout); @@ -71,8 +71,9 @@ class PrinterBluetoothManager { }); } - void stopScan() async { + Future stopScan() async { await _bluetoothManager.stopScan(); + await _isScanningSubscription?.cancel(); } void selectPrinter(PrinterBluetooth printer) { @@ -108,12 +109,8 @@ class PrinterBluetoothManager { return Future.value(PosPrintResult.printInProgress); } // We have to rescan before connecting, otherwise we can connect only once - final devices = - await _bluetoothManager.startScan(timeout: Duration(seconds: 1)); - print(devices); - print(_selectedPrinter._device); - await _bluetoothManager.stopScan(); - + await startScan(Duration(seconds: 1)); + await stopScan(); // Connect await _bluetoothManager.connect(_selectedPrinter._device); final result = await _checkConnectionState(); @@ -127,9 +124,6 @@ class PrinterBluetoothManager { _runDelayed(timeout).then((dynamic v) async { if (_isPrinting) { _isPrinting = false; - completer.complete(PosPrintResult.timeout); - await _bluetoothManager.disconnect(); - print('TIMEOUT'); } completer.complete(PosPrintResult.success); }); @@ -150,20 +144,12 @@ class PrinterBluetoothManager { _queueSleepTimeMs = queueSleepTimeMs; _chunkSizeBytes = chunkSizeBytes; _connectionTimeOut = connectionTimeOut; - - if (!_isConnected) { - await connect(ticket.bytes, timeout); - } else { - await disconnect(1); - await connect(ticket.bytes, timeout); - } - - if (_isConnected) { - print("PRINT REQUEST"); - print("PRINT REQUEST"); + await stopScan(); + final result = await connect(ticket.bytes, timeout); + if (result.msg == "Success") { return await _writeRequest(timeout); } else { - return Future.value(PosPrintResult.timeout); + return result; } } @@ -181,14 +167,8 @@ class PrinterBluetoothManager { _chunkSizeBytes = chunkSizeBytes; _connectionTimeOut = connectionTimeOut; - if (!_isConnected) { - await connect(bytes, timeout); - } else { - await disconnect(1); - await connect(bytes, timeout); - } - - if (_isConnected) { + final result = await connect(bytes, timeout); + if (result == "Success") { return await _writeRequest(timeout); } else { return Future.value(PosPrintResult.timeout); @@ -229,9 +209,7 @@ class PrinterBluetoothManager { bytes, timeout: timeout, ); - if (result.msg != 'Success') { - return result; - } + return result; } Future disconnect(timeout) async { @@ -239,10 +217,9 @@ class PrinterBluetoothManager { try { print('PENDING DISCONNECTED'); await Future.delayed(Duration(seconds: timeout)); - final dynamic disconnected = await _bluetoothManager.disconnect(); - final dynamic destroy = await _bluetoothManager.destroy(); - print(disconnected); - print(destroy); + await _bluetoothManager.disconnect(); + await _bluetoothManager.destroy(); + await _isScanningSubscription?.cancel(); Timer _stateTimer; int _start = _connectionTimeOut; const oneSec = Duration(seconds: 1); From 53ea9487babeb99edc5a569a4f6dae4de5b88d98 Mon Sep 17 00:00:00 2001 From: louieseno Date: Fri, 23 Jul 2021 15:32:21 +0800 Subject: [PATCH 18/21] fix: pos print result return value --- lib/src/printer_bluetooth_manager.dart | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/src/printer_bluetooth_manager.dart b/lib/src/printer_bluetooth_manager.dart index ce15df6..650f9b0 100644 --- a/lib/src/printer_bluetooth_manager.dart +++ b/lib/src/printer_bluetooth_manager.dart @@ -166,12 +166,12 @@ class PrinterBluetoothManager { _queueSleepTimeMs = queueSleepTimeMs; _chunkSizeBytes = chunkSizeBytes; _connectionTimeOut = connectionTimeOut; - + await stopScan(); final result = await connect(bytes, timeout); if (result == "Success") { return await _writeRequest(timeout); } else { - return Future.value(PosPrintResult.timeout); + return result; } } From d2f81d9b0bee5fb1754166c3ef1c2067474fc198 Mon Sep 17 00:00:00 2001 From: louieseno Date: Fri, 23 Jul 2021 16:10:46 +0800 Subject: [PATCH 19/21] fix: print label function --- lib/src/printer_bluetooth_manager.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/src/printer_bluetooth_manager.dart b/lib/src/printer_bluetooth_manager.dart index 650f9b0..4ccd50c 100644 --- a/lib/src/printer_bluetooth_manager.dart +++ b/lib/src/printer_bluetooth_manager.dart @@ -168,7 +168,7 @@ class PrinterBluetoothManager { _connectionTimeOut = connectionTimeOut; await stopScan(); final result = await connect(bytes, timeout); - if (result == "Success") { + if (result.msg == "Success") { return await _writeRequest(timeout); } else { return result; From 101cb27027b29dfff2626a79cca14ff76a6aac86 Mon Sep 17 00:00:00 2001 From: louieseno Date: Fri, 23 Jul 2021 16:33:39 +0800 Subject: [PATCH 20/21] fix: connect write --- lib/src/printer_bluetooth_manager.dart | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/lib/src/printer_bluetooth_manager.dart b/lib/src/printer_bluetooth_manager.dart index 4ccd50c..f9b3c9e 100644 --- a/lib/src/printer_bluetooth_manager.dart +++ b/lib/src/printer_bluetooth_manager.dart @@ -144,12 +144,15 @@ class PrinterBluetoothManager { _queueSleepTimeMs = queueSleepTimeMs; _chunkSizeBytes = chunkSizeBytes; _connectionTimeOut = connectionTimeOut; - await stopScan(); - final result = await connect(ticket.bytes, timeout); - if (result.msg == "Success") { + if (_isConnected) { return await _writeRequest(timeout); } else { - return result; + final result = await connect(ticket.bytes, timeout); + if (result.msg == "Success") { + return await _writeRequest(timeout); + } else { + return result; + } } } @@ -166,12 +169,15 @@ class PrinterBluetoothManager { _queueSleepTimeMs = queueSleepTimeMs; _chunkSizeBytes = chunkSizeBytes; _connectionTimeOut = connectionTimeOut; - await stopScan(); - final result = await connect(bytes, timeout); - if (result.msg == "Success") { + if (_isConnected) { return await _writeRequest(timeout); } else { - return result; + final result = await connect(bytes, timeout); + if (result.msg == "Success") { + return await _writeRequest(timeout); + } else { + return result; + } } } From 824acca26413aafeb24bdd9a87a4aa1f5695a447 Mon Sep 17 00:00:00 2001 From: louieseno Date: Fri, 23 Jul 2021 18:31:25 +0800 Subject: [PATCH 21/21] fix: test --- lib/src/printer_bluetooth_manager.dart | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/lib/src/printer_bluetooth_manager.dart b/lib/src/printer_bluetooth_manager.dart index f9b3c9e..c7c30f9 100644 --- a/lib/src/printer_bluetooth_manager.dart +++ b/lib/src/printer_bluetooth_manager.dart @@ -45,7 +45,7 @@ class PrinterBluetoothManager { List _bufferedBytes = []; int _queueSleepTimeMs = 20; int _chunkSizeBytes = 20; - int _connectionTimeOut = 5; + int _connectionTimeOut = 10; Future _runDelayed(int seconds) { return Future.delayed(Duration(seconds: seconds)); @@ -109,8 +109,8 @@ class PrinterBluetoothManager { return Future.value(PosPrintResult.printInProgress); } // We have to rescan before connecting, otherwise we can connect only once - await startScan(Duration(seconds: 1)); - await stopScan(); + await _bluetoothManager.startScan(timeout: Duration(seconds: 1)); + await _bluetoothManager.stopScan(); // Connect await _bluetoothManager.connect(_selectedPrinter._device); final result = await _checkConnectionState(); @@ -224,8 +224,6 @@ class PrinterBluetoothManager { print('PENDING DISCONNECTED'); await Future.delayed(Duration(seconds: timeout)); await _bluetoothManager.disconnect(); - await _bluetoothManager.destroy(); - await _isScanningSubscription?.cancel(); Timer _stateTimer; int _start = _connectionTimeOut; const oneSec = Duration(seconds: 1);