Commit 57840358 authored by Andrey's avatar Andrey

Fixed double printing bug. Print custom text.

parent 998d07f8
...@@ -59,7 +59,7 @@ class _MyHomePageState extends State<MyHomePage> { ...@@ -59,7 +59,7 @@ class _MyHomePageState extends State<MyHomePage> {
void _testPrint(PrinterBluetooth printer) async { void _testPrint(PrinterBluetooth printer) async {
printerManager.selectPrinter(printer); printerManager.selectPrinter(printer);
printerManager.printLine('hello').then((val) { printerManager.printLine('====== @@@ hello\n\n\n').then((val) {
showToast('Success!'); showToast('Success!');
}).catchError((dynamic e) { }).catchError((dynamic e) {
print('catched: ${e.toString()}'); print('catched: ${e.toString()}');
......
...@@ -34,20 +34,17 @@ class PrinterBluetooth { ...@@ -34,20 +34,17 @@ class PrinterBluetooth {
/// Printer Bluetooth Manager /// Printer Bluetooth Manager
class PrinterBluetoothManager { class PrinterBluetoothManager {
// PrinterBluetoothManager() {
// }
final BluetoothManager _bluetoothManager = BluetoothManager.instance; final BluetoothManager _bluetoothManager = BluetoothManager.instance;
bool _isScanning = false; bool _isScanning = false;
bool _isPrinting = false; bool _isPrinting = false;
bool _isConnected = false;
StreamSubscription _scanResultsSubscription; StreamSubscription _scanResultsSubscription;
StreamSubscription _isScanningSubscription; StreamSubscription _isScanningSubscription;
PrinterBluetooth _selectedPrinter; PrinterBluetooth _selectedPrinter;
Stream<bool> get isScanningStream => _bluetoothManager.isScanning; Stream<bool> get isScanningStream => _bluetoothManager.isScanning;
BehaviorSubject<List<PrinterBluetooth>> _scanResults = final BehaviorSubject<List<PrinterBluetooth>> _scanResults =
BehaviorSubject.seeded([]); BehaviorSubject.seeded([]);
Stream<List<PrinterBluetooth>> get scanResults => _scanResults.stream; Stream<List<PrinterBluetooth>> get scanResults => _scanResults.stream;
...@@ -61,11 +58,9 @@ class PrinterBluetoothManager { ...@@ -61,11 +58,9 @@ class PrinterBluetoothManager {
_bluetoothManager.startScan(timeout: Duration(seconds: 4)); _bluetoothManager.startScan(timeout: Duration(seconds: 4));
_scanResultsSubscription = _bluetoothManager.scanResults.listen((devices) { _scanResultsSubscription = _bluetoothManager.scanResults.listen((devices) {
// print('====scan result');
_scanResults.add(devices.map((d) => PrinterBluetooth(d)).toList()); _scanResults.add(devices.map((d) => PrinterBluetooth(d)).toList());
}); });
// TODO move listener to constructor
_isScanningSubscription = _isScanningSubscription =
_bluetoothManager.isScanning.listen((isScanningCurrent) async { _bluetoothManager.isScanning.listen((isScanningCurrent) async {
// If isScanning value changed (scan just stopped) // If isScanning value changed (scan just stopped)
...@@ -87,7 +82,6 @@ class PrinterBluetoothManager { ...@@ -87,7 +82,6 @@ class PrinterBluetoothManager {
Future<void> printLine(String text) async { Future<void> printLine(String text) async {
const int timeout = 5; const int timeout = 5;
print('============ $_isScanning');
if (_selectedPrinter == null) { if (_selectedPrinter == null) {
throw Exception('Print failed (Select a printer first)'); throw Exception('Print failed (Select a printer first)');
} }
...@@ -113,9 +107,8 @@ class PrinterBluetoothManager { ...@@ -113,9 +107,8 @@ class PrinterBluetoothManager {
case BluetoothManager.CONNECTED: case BluetoothManager.CONNECTED:
print('********************* CONNECTED'); print('********************* CONNECTED');
// To avoid double call // To avoid double call
if (_selectedPrinter._device.connected == null || if (!_isConnected) {
!_selectedPrinter._device.connected) { final List<int> bytes = latin1.encode(text).toList();
final List<int> bytes = latin1.encode('test!\n\n\n').toList();
await _bluetoothManager.writeData(bytes); await _bluetoothManager.writeData(bytes);
// TODO Notify data sent // TODO Notify data sent
} }
...@@ -125,9 +118,11 @@ class PrinterBluetoothManager { ...@@ -125,9 +118,11 @@ class PrinterBluetoothManager {
await _bluetoothManager.disconnect(); await _bluetoothManager.disconnect();
_isPrinting = false; _isPrinting = false;
}); });
_isConnected = true;
break; break;
case BluetoothManager.DISCONNECTED: case BluetoothManager.DISCONNECTED:
print('********************* DISCONNECTED'); print('********************* DISCONNECTED');
_isConnected = false;
break; break;
default: default:
break; break;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment