|
@@ -23,36 +23,37 @@ class MyApp extends StatefulWidget {
|
23
|
23
|
class _MyAppState extends State<MyApp> {
|
24
|
24
|
bool _isDiscovering = false;
|
25
|
25
|
List<BluetoothPrinter> _discoveredBluetoothPrinters = [];
|
26
|
|
- String _platformVersion = 'Unknown';
|
|
26
|
+ String? _connectedPrinter;
|
27
|
27
|
final _flutterZsdkPlugin = FlutterZsdk();
|
|
28
|
+ String? _selectedBluetoothPrinterMacAddress;
|
28
|
29
|
|
29
|
30
|
@override
|
30
|
31
|
void initState() {
|
31
|
32
|
super.initState();
|
32
|
|
- initPlatformState();
|
|
33
|
+ // initPlatformState();
|
33
|
34
|
}
|
34
|
35
|
|
35
|
36
|
// Platform messages are asynchronous, so we initialize in an async method.
|
36
|
|
- Future<void> initPlatformState() async {
|
37
|
|
- String platformVersion;
|
38
|
|
- // Platform messages may fail, so we use a try/catch PlatformException.
|
39
|
|
- // We also handle the message potentially returning null.
|
40
|
|
- try {
|
41
|
|
- platformVersion =
|
42
|
|
- await _flutterZsdkPlugin.getPlatformVersion() ?? 'Unknown platform version';
|
43
|
|
- } on PlatformException {
|
44
|
|
- platformVersion = 'Failed to get platform version.';
|
45
|
|
- }
|
46
|
|
-
|
47
|
|
- // If the widget was removed from the tree while the asynchronous platform
|
48
|
|
- // message was in flight, we want to discard the reply rather than calling
|
49
|
|
- // setState to update our non-existent appearance.
|
50
|
|
- if (!mounted) return;
|
51
|
|
-
|
52
|
|
- setState(() {
|
53
|
|
- _platformVersion = platformVersion;
|
54
|
|
- });
|
55
|
|
- }
|
|
37
|
+ // Future<void> initPlatformState() async {
|
|
38
|
+ // String platformVersion;
|
|
39
|
+ // // Platform messages may fail, so we use a try/catch PlatformException.
|
|
40
|
+ // // We also handle the message potentially returning null.
|
|
41
|
+ // try {
|
|
42
|
+ // platformVersion =
|
|
43
|
+ // await _flutterZsdkPlugin.getPlatformVersion() ?? 'Unknown platform version';
|
|
44
|
+ // } on PlatformException {
|
|
45
|
+ // platformVersion = 'Failed to get platform version.';
|
|
46
|
+ // }
|
|
47
|
+
|
|
48
|
+ // // If the widget was removed from the tree while the asynchronous platform
|
|
49
|
+ // // message was in flight, we want to discard the reply rather than calling
|
|
50
|
+ // // setState to update our non-existent appearance.
|
|
51
|
+ // if (!mounted) return;
|
|
52
|
+
|
|
53
|
+ // setState(() {
|
|
54
|
+ // _platformVersion = platformVersion;
|
|
55
|
+ // });
|
|
56
|
+ // }
|
56
|
57
|
|
57
|
58
|
StreamSubscription? _bluetoothPrinterSubscription;
|
58
|
59
|
|
|
@@ -107,6 +108,25 @@ class _MyAppState extends State<MyApp> {
|
107
|
108
|
}
|
108
|
109
|
}
|
109
|
110
|
|
|
111
|
+ Future<void> openConnection() async {
|
|
112
|
+ print('invoked openConnection from dart');
|
|
113
|
+ try {
|
|
114
|
+ await _flutterZsdkPlugin.openConnection(_selectedBluetoothPrinterMacAddress ?? '');
|
|
115
|
+ _connectedPrinter = _selectedBluetoothPrinterMacAddress ?? 'Unknown printer';
|
|
116
|
+ print('Connection opened successfully from dart');
|
|
117
|
+
|
|
118
|
+ } on FlutterZsdkException catch (e) {
|
|
119
|
+ inspect(e);
|
|
120
|
+ showSnackBar(e.message);
|
|
121
|
+
|
|
122
|
+ } catch (e) {
|
|
123
|
+ inspect(e);
|
|
124
|
+ showSnackBar('Unexpected error while connecting to bluetooth printers');
|
|
125
|
+ }
|
|
126
|
+
|
|
127
|
+ setState(() {});
|
|
128
|
+ }
|
|
129
|
+
|
110
|
130
|
@override
|
111
|
131
|
Widget build(BuildContext context) {
|
112
|
132
|
return Scaffold(
|
|
@@ -118,9 +138,16 @@ class _MyAppState extends State<MyApp> {
|
118
|
138
|
crossAxisAlignment: CrossAxisAlignment.center,
|
119
|
139
|
mainAxisAlignment: MainAxisAlignment.center,
|
120
|
140
|
children: [
|
121
|
|
- Text('Running on: $_platformVersion\n'),
|
|
141
|
+ Text('Connected printer: ${_connectedPrinter ?? "Unknown printer"}\n'),
|
|
142
|
+ Text('Selected printer Mac Address: $_selectedBluetoothPrinterMacAddress\n'),
|
122
|
143
|
SizedBox(height: 20),
|
123
|
144
|
|
|
145
|
+ ElevatedButton(
|
|
146
|
+ child: Text('Connect to $_selectedBluetoothPrinterMacAddress'),
|
|
147
|
+ onPressed: _selectedBluetoothPrinterMacAddress == null || _connectedPrinter != null ? null : openConnection,
|
|
148
|
+ ),
|
|
149
|
+ SizedBox(height: 20),
|
|
150
|
+
|
124
|
151
|
ElevatedButton(
|
125
|
152
|
child: Text(_isDiscovering ? 'Discovering bluetooth printers...' : 'Discover nearby bluetooth printers'),
|
126
|
153
|
onPressed: _isDiscovering ? null : () async {
|
|
@@ -130,9 +157,20 @@ class _MyAppState extends State<MyApp> {
|
130
|
157
|
SizedBox(height: 20),
|
131
|
158
|
|
132
|
159
|
for (var printer in _discoveredBluetoothPrinters) ... [
|
133
|
|
- Text(printer.friendlyName),
|
134
|
|
- Text(printer.macAddress),
|
135
|
|
- SizedBox(height: 10)
|
|
160
|
+ InkWell(
|
|
161
|
+ onTap: () {
|
|
162
|
+ _selectedBluetoothPrinterMacAddress = printer.macAddress;
|
|
163
|
+ setState(() {});
|
|
164
|
+ },
|
|
165
|
+ child: Column(
|
|
166
|
+ mainAxisSize: MainAxisSize.min,
|
|
167
|
+ children: [
|
|
168
|
+ Text(printer.friendlyName),
|
|
169
|
+ Text(printer.macAddress),
|
|
170
|
+ SizedBox(height: 10)
|
|
171
|
+ ],
|
|
172
|
+ ),
|
|
173
|
+ )
|
136
|
174
|
]
|
137
|
175
|
]
|
138
|
176
|
|