Commit 20a729f4 authored by Andrey's avatar Andrey

print function moved to manager class

parent b412622d
...@@ -63,7 +63,8 @@ class _MyHomePageState extends State<MyHomePage> { ...@@ -63,7 +63,8 @@ class _MyHomePageState extends State<MyHomePage> {
void _testPrint(PrinterBluetooth printer) async { void _testPrint(PrinterBluetooth printer) async {
try { try {
printer.printLine('hello'); printerManager.selectPrinter(printer);
printerManager.printLine('hello');
} catch (e) { } catch (e) {
print(e.toString()); print(e.toString());
} }
......
...@@ -27,24 +27,136 @@ class PrinterBluetooth { ...@@ -27,24 +27,136 @@ class PrinterBluetooth {
PrinterBluetooth(this._device); PrinterBluetooth(this._device);
final BluetoothDevice _device; final BluetoothDevice _device;
// final BluetoothManager _bluetoothManager = BluetoothManager.instance; // final BluetoothManager _bluetoothManager = BluetoothManager.instance;
final PrinterBluetoothManager _manager = PrinterBluetoothManager(); // final PrinterBluetoothManager _manager = PrinterBluetoothManager();
bool _isPrinting = false; // bool _isPrinting = false;
String get name => _device.name; String get name => _device.name;
String get address => _device.address; String get address => _device.address;
int get type => _device.type; int get type => _device.type;
// Future _runDelayed(int seconds) {
// return Future<dynamic>.delayed(Duration(seconds: seconds));
// }
// void printLine(String text) async {
// const int timeout = 5;
// print('============ ${_manager._isScanning}');
// // if (_bluetoothManager.is) {
// // showToast('Print failed (scanning in progress)');
// // return;
// // }
// if (_isPrinting) {
// throw Exception('Print failed (another printing in progress)');
// }
// _isPrinting = true;
// // We have to rescan before connecting, otherwise we can connect only once
// await _manager._bluetoothManager.startScan(timeout: Duration(seconds: 1));
// await _manager._bluetoothManager.stopScan();
// // Connect
// await _manager._bluetoothManager.connect(_device);
// // Subscribe to the events
// _manager._bluetoothManager.state.listen((state) async {
// switch (state) {
// case BluetoothManager.CONNECTED:
// print('********************* CONNECTED');
// // to avoid double call
// // if (!_connected) {
// if (_device.connected == null || !_device.connected) {
// print('@@@@SEND DATA......');
// final List<int> bytes = latin1.encode('test!\n\n\n').toList();
// await _manager._bluetoothManager.writeData(bytes);
// // showToast('Data sent'); TODO
// // return 0;
// }
// // TODO sending disconnect signal should be event-based
// _runDelayed(3).then((dynamic v) async {
// print('@@@@DISCONNECTING......');
// await _manager._bluetoothManager.disconnect();
// _isPrinting = false;
// });
// // _connected = true;
// break;
// case BluetoothManager.DISCONNECTED:
// print('********************* DISCONNECTED');
// // _connected = false;
// break;
// default:
// break;
// }
// // return 0;
// });
// // Printing timeout
// _runDelayed(timeout).then((dynamic v) async {
// if (_isPrinting) {
// _isPrinting = false;
// throw Exception('Print failed (timeout)');
// }
// });
// }
}
/// Printer Bluetooth Manager
class PrinterBluetoothManager {
final BluetoothManager _bluetoothManager = BluetoothManager.instance;
// bool _connected = false;
bool _isScanning = false;
bool _isPrinting = false;
StreamSubscription _scanResultsSubscription;
StreamSubscription _isScanningSubscription;
PrinterBluetooth _selectedPrinter;
Stream<bool> get isScanningStream => _bluetoothManager.isScanning;
BehaviorSubject<List<PrinterBluetooth>> _scanResults =
BehaviorSubject.seeded([]);
Stream<List<PrinterBluetooth>> get scanResults => _scanResults.stream;
Future _runDelayed(int seconds) { Future _runDelayed(int seconds) {
return Future<dynamic>.delayed(Duration(seconds: seconds)); return Future<dynamic>.delayed(Duration(seconds: seconds));
} }
void startScan(Duration timeout) async {
_scanResults.add(<PrinterBluetooth>[]);
_bluetoothManager.startScan(timeout: Duration(seconds: 4));
_scanResultsSubscription = _bluetoothManager.scanResults.listen((devices) {
_scanResults.add(devices.map((d) => PrinterBluetooth(d)).toList());
});
// TODO move listener to constructor
_isScanningSubscription =
_bluetoothManager.isScanning.listen((isScanningCurrent) async {
// If isScanning value changed (scan just stopped)
if (_isScanning && !isScanningCurrent) {
_scanResultsSubscription.cancel();
_isScanningSubscription.cancel();
}
_isScanning = isScanningCurrent;
});
}
void stopScan() async {
await _bluetoothManager.stopScan();
}
void selectPrinter(PrinterBluetooth printer) {
_selectedPrinter = printer;
}
void printLine(String text) async { void printLine(String text) async {
const int timeout = 5; const int timeout = 5;
print('============ ${_manager._isScanning}'); print('============ $_isScanning');
// if (_bluetoothManager.is) { if (_selectedPrinter == null) {
// showToast('Print failed (scanning in progress)'); throw Exception('Print failed (Select a printer first)');
// return; }
// } if (_isScanning) {
throw Exception('Print failed (scanning in progress)');
}
if (_isPrinting) { if (_isPrinting) {
throw Exception('Print failed (another printing in progress)'); throw Exception('Print failed (another printing in progress)');
} }
...@@ -52,30 +164,30 @@ class PrinterBluetooth { ...@@ -52,30 +164,30 @@ class PrinterBluetooth {
_isPrinting = true; _isPrinting = true;
// We have to rescan before connecting, otherwise we can connect only once // We have to rescan before connecting, otherwise we can connect only once
await _manager._bluetoothManager.startScan(timeout: Duration(seconds: 1)); await _bluetoothManager.startScan(timeout: Duration(seconds: 1));
await _manager._bluetoothManager.stopScan(); await _bluetoothManager.stopScan();
// Connect // Connect
await _manager._bluetoothManager.connect(_device); await _bluetoothManager.connect(_selectedPrinter._device);
// Subscribe to the events // Subscribe to the events
_manager._bluetoothManager.state.listen((state) async { _bluetoothManager.state.listen((state) async {
switch (state) { switch (state) {
case BluetoothManager.CONNECTED: case BluetoothManager.CONNECTED:
print('********************* CONNECTED'); print('********************* CONNECTED');
// to avoid double call // to avoid double call
// if (!_connected) { // if (!_connected) {
if (_device.connected == null || !_device.connected) { if (_selectedPrinter._device.connected == null ||
!_selectedPrinter._device.connected) {
print('@@@@SEND DATA......'); print('@@@@SEND DATA......');
final List<int> bytes = latin1.encode('test!\n\n\n').toList(); final List<int> bytes = latin1.encode('test!\n\n\n').toList();
await _manager._bluetoothManager.writeData(bytes); await _bluetoothManager.writeData(bytes);
// showToast('Data sent'); TODO // TODO data sent
// return 0;
} }
// TODO sending disconnect signal should be event-based // TODO sending disconnect signal should be event-based
_runDelayed(3).then((dynamic v) async { _runDelayed(3).then((dynamic v) async {
print('@@@@DISCONNECTING......'); print('@@@@DISCONNECTING......');
await _manager._bluetoothManager.disconnect(); await _bluetoothManager.disconnect();
_isPrinting = false; _isPrinting = false;
}); });
// _connected = true; // _connected = true;
...@@ -89,6 +201,7 @@ class PrinterBluetooth { ...@@ -89,6 +201,7 @@ class PrinterBluetooth {
} }
// return 0; // return 0;
}); });
// Printing timeout // Printing timeout
_runDelayed(timeout).then((dynamic v) async { _runDelayed(timeout).then((dynamic v) async {
if (_isPrinting) { if (_isPrinting) {
...@@ -98,45 +211,3 @@ class PrinterBluetooth { ...@@ -98,45 +211,3 @@ class PrinterBluetooth {
}); });
} }
} }
/// Bluetooth printer
/// // TODO rename to BluetoothScanner
class PrinterBluetoothManager {
final BluetoothManager _bluetoothManager = BluetoothManager.instance;
// bool _connected = false;
bool _isScanning = false;
// bool _isPrinting = false;
StreamSubscription _scanResultsSubscription;
StreamSubscription _isScanningSubscription;
Stream<bool> get isScanningStream => _bluetoothManager.isScanning;
BehaviorSubject<List<PrinterBluetooth>> _scanResults =
BehaviorSubject.seeded([]);
Stream<List<PrinterBluetooth>> get scanResults => _scanResults.stream;
void startScan(Duration timeout) async {
_scanResults.add(<PrinterBluetooth>[]);
_bluetoothManager.startScan(timeout: Duration(seconds: 4));
_scanResultsSubscription = _bluetoothManager.scanResults.listen((devices) {
_scanResults.add(devices.map((d) => PrinterBluetooth(d)).toList());
});
// TODO move listener to constructor
_isScanningSubscription =
_bluetoothManager.isScanning.listen((isScanningCurrent) async {
// If isScanning value changed (scan just stopped)
if (_isScanning && !isScanningCurrent) {
_scanResultsSubscription.cancel();
_isScanningSubscription.cancel();
}
_isScanning = isScanningCurrent;
});
}
void stopScan() async {
await _bluetoothManager.stopScan();
}
}
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