Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
E
esc_pos_print_plus
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Dio Maulana
esc_pos_print_plus
Commits
998d07f8
Commit
998d07f8
authored
Jan 15, 2020
by
Andrey
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Code refactoring
parent
20a729f4
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
17 additions
and
88 deletions
+17
-88
main.dart
example/blue/lib/main.dart
+8
-11
printer_bluetooth_manager.dart
lib/src/printer_bluetooth_manager.dart
+9
-77
No files found.
example/blue/lib/main.dart
View file @
998d07f8
...
@@ -39,7 +39,7 @@ class _MyHomePageState extends State<MyHomePage> {
...
@@ -39,7 +39,7 @@ class _MyHomePageState extends State<MyHomePage> {
super
.
initState
();
super
.
initState
();
printerManager
.
scanResults
.
listen
((
devices
)
async
{
printerManager
.
scanResults
.
listen
((
devices
)
async
{
print
(
'UI: Devices found
${devices.length}
'
);
//
print('UI: Devices found ${devices.length}');
setState
(()
{
setState
(()
{
_devices
=
devices
;
_devices
=
devices
;
});
});
...
@@ -57,17 +57,14 @@ class _MyHomePageState extends State<MyHomePage> {
...
@@ -57,17 +57,14 @@ class _MyHomePageState extends State<MyHomePage> {
printerManager
.
stopScan
();
printerManager
.
stopScan
();
}
}
// Future _runDelayed(int seconds) {
// return Future<dynamic>.delayed(Duration(seconds: seconds));
// }
void
_testPrint
(
PrinterBluetooth
printer
)
async
{
void
_testPrint
(
PrinterBluetooth
printer
)
async
{
try
{
printerManager
.
selectPrinter
(
printer
);
printerManager
.
selectPrinter
(
printer
);
printerManager
.
printLine
(
'hello'
).
then
((
val
)
{
printerManager
.
printLine
(
'hello'
);
showToast
(
'Success!'
);
}
catch
(
e
)
{
}).
catchError
((
dynamic
e
)
{
print
(
e
.
toString
());
print
(
'catched:
${e.toString()}
'
);
}
showToast
(
e
.
toString
());
});
}
}
@override
@override
...
...
lib/src/printer_bluetooth_manager.dart
View file @
998d07f8
...
@@ -26,83 +26,19 @@ import 'package:flutter_bluetooth_basic/flutter_bluetooth_basic.dart';
...
@@ -26,83 +26,19 @@ import 'package:flutter_bluetooth_basic/flutter_bluetooth_basic.dart';
class
PrinterBluetooth
{
class
PrinterBluetooth
{
PrinterBluetooth
(
this
.
_device
);
PrinterBluetooth
(
this
.
_device
);
final
BluetoothDevice
_device
;
final
BluetoothDevice
_device
;
// final BluetoothManager _bluetoothManager = BluetoothManager.instance;
// final PrinterBluetoothManager _manager = PrinterBluetoothManager();
// 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
/// Printer Bluetooth Manager
class
PrinterBluetoothManager
{
class
PrinterBluetoothManager
{
// PrinterBluetoothManager() {
// }
final
BluetoothManager
_bluetoothManager
=
BluetoothManager
.
instance
;
final
BluetoothManager
_bluetoothManager
=
BluetoothManager
.
instance
;
// bool _connected = false;
bool
_isScanning
=
false
;
bool
_isScanning
=
false
;
bool
_isPrinting
=
false
;
bool
_isPrinting
=
false
;
StreamSubscription
_scanResultsSubscription
;
StreamSubscription
_scanResultsSubscription
;
...
@@ -125,6 +61,7 @@ class PrinterBluetoothManager {
...
@@ -125,6 +61,7 @@ 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
());
});
});
...
@@ -148,7 +85,7 @@ class PrinterBluetoothManager {
...
@@ -148,7 +85,7 @@ class PrinterBluetoothManager {
_selectedPrinter
=
printer
;
_selectedPrinter
=
printer
;
}
}
void
printLine
(
String
text
)
async
{
Future
<
void
>
printLine
(
String
text
)
async
{
const
int
timeout
=
5
;
const
int
timeout
=
5
;
print
(
'============
$_isScanning
'
);
print
(
'============
$_isScanning
'
);
if
(
_selectedPrinter
==
null
)
{
if
(
_selectedPrinter
==
null
)
{
...
@@ -175,31 +112,26 @@ class PrinterBluetoothManager {
...
@@ -175,31 +112,26 @@ class PrinterBluetoothManager {
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
(
_selectedPrinter
.
_device
.
connected
==
null
||
if
(
_selectedPrinter
.
_device
.
connected
==
null
||
!
_selectedPrinter
.
_device
.
connected
)
{
!
_selectedPrinter
.
_device
.
connected
)
{
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
_bluetoothManager
.
writeData
(
bytes
);
await
_bluetoothManager
.
writeData
(
bytes
);
// TODO data sent
// TODO
Notify
data sent
}
}
// 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
_bluetoothManager
.
disconnect
();
await
_bluetoothManager
.
disconnect
();
_isPrinting
=
false
;
_isPrinting
=
false
;
});
});
// _connected = true;
break
;
break
;
case
BluetoothManager
.
DISCONNECTED
:
case
BluetoothManager
.
DISCONNECTED
:
print
(
'********************* DISCONNECTED'
);
print
(
'********************* DISCONNECTED'
);
// _connected = false;
break
;
break
;
default
:
default
:
break
;
break
;
}
}
// return 0;
});
});
// Printing timeout
// Printing timeout
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment