flutter plugin for zebra multiplatform sdk

main.dart 10.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. import 'dart:developer';
  2. import 'dart:math';
  3. import 'package:flutter/material.dart';
  4. import 'dart:async';
  5. import 'package:flutter/services.dart';
  6. import 'package:flutter_zsdk/flutter_zsdk.dart';
  7. import 'package:flutter_zsdk/src/models/flutter_zsdk_exception.dart';
  8. import 'package:flutter_zsdk_example/models/bluetooth_printer.dart';
  9. void main() {
  10. runApp(const MaterialApp(home: MyApp()));
  11. }
  12. class MyApp extends StatefulWidget {
  13. const MyApp({super.key});
  14. @override
  15. State<MyApp> createState() => _MyAppState();
  16. }
  17. class _MyAppState extends State<MyApp> {
  18. final _formKey = GlobalKey<FormState>();
  19. bool _isDiscovering = false;
  20. List<BluetoothPrinter> _discoveredBluetoothPrinters = [];
  21. String? _connectedPrinter;
  22. final _flutterZsdkPlugin = FlutterZsdk();
  23. String? _selectedBluetoothPrinterMacAddress;
  24. bool _disconnecting = false;
  25. TextEditingController _zplDataController = TextEditingController();
  26. @override
  27. void initState() {
  28. super.initState();
  29. // initPlatformState();
  30. }
  31. // Platform messages are asynchronous, so we initialize in an async method.
  32. // Future<void> initPlatformState() async {
  33. // String platformVersion;
  34. // // Platform messages may fail, so we use a try/catch PlatformException.
  35. // // We also handle the message potentially returning null.
  36. // try {
  37. // platformVersion =
  38. // await _flutterZsdkPlugin.getPlatformVersion() ?? 'Unknown platform version';
  39. // } on PlatformException {
  40. // platformVersion = 'Failed to get platform version.';
  41. // }
  42. // // If the widget was removed from the tree while the asynchronous platform
  43. // // message was in flight, we want to discard the reply rather than calling
  44. // // setState to update our non-existent appearance.
  45. // if (!mounted) return;
  46. // setState(() {
  47. // _platformVersion = platformVersion;
  48. // });
  49. // }
  50. StreamSubscription? _bluetoothPrinterSubscription;
  51. void showSnackBar(String message) {
  52. ScaffoldMessenger.of(context).showSnackBar(
  53. SnackBar(content: Text(message))
  54. );
  55. }
  56. Future<void> discoverBluetoothDevices() async {
  57. try {
  58. Stream<dynamic> stream = await _flutterZsdkPlugin.findBluetoothPrinters();
  59. _bluetoothPrinterSubscription = stream.listen((event) {
  60. print(event);
  61. if (event == 'SOS') {
  62. _isDiscovering = true;
  63. }
  64. if (event is List) {
  65. _discoveredBluetoothPrinters.clear();
  66. for (var printer in event) {
  67. BluetoothPrinter bluetoothPrinter = BluetoothPrinter.fromMap(printer);
  68. _discoveredBluetoothPrinters.add(bluetoothPrinter);
  69. }
  70. }
  71. if (event == 'EOS') {
  72. _isDiscovering = false;
  73. showSnackBar('Bluetooth discovery finished, found ${_discoveredBluetoothPrinters.length} printers');
  74. _bluetoothPrinterSubscription?.cancel();
  75. }
  76. setState(() {});
  77. });
  78. } on PlatformException catch (e) {
  79. inspect(e);
  80. showSnackBar(e.message.toString());
  81. } on FlutterZsdkException catch (e) {
  82. inspect(e);
  83. showSnackBar(e.message);
  84. } catch (e) {
  85. inspect(e);
  86. showSnackBar('Unexpected error while discovering bluetooth printers');
  87. }
  88. }
  89. Future<void> openConnection() async {
  90. print('invoked openConnection from dart');
  91. try {
  92. await _flutterZsdkPlugin.openConnection(_selectedBluetoothPrinterMacAddress ?? '');
  93. _connectedPrinter = _selectedBluetoothPrinterMacAddress ?? 'Unknown printer';
  94. print('Connection opened successfully from dart');
  95. } on FlutterZsdkException catch (e) {
  96. inspect(e);
  97. showSnackBar(e.message);
  98. } catch (e) {
  99. inspect(e);
  100. showSnackBar('Unexpected error while connecting to bluetooth printers');
  101. }
  102. setState(() {});
  103. }
  104. Future<void> closeConnection() async {
  105. print('invoked closeConnection from dart');
  106. try {
  107. setState(() {
  108. _disconnecting = true;
  109. });
  110. await _flutterZsdkPlugin.closeConnection();
  111. _connectedPrinter = null;
  112. _disconnecting = false;
  113. print('Connection closed successfully from dart');
  114. } on FlutterZsdkException catch (e) {
  115. inspect(e);
  116. showSnackBar(e.message);
  117. } catch (e) {
  118. inspect(e);
  119. showSnackBar('Unexpected error while disconnecting bluetooth printers');
  120. }
  121. setState(() {});
  122. }
  123. Future<void> printZplOverBluetooth() async {
  124. if(!_formKey.currentState!.validate()) {
  125. return;
  126. }
  127. print('invoked printZplOverBluetooth from dart');
  128. try {
  129. await _flutterZsdkPlugin.printZplOverBluetooth(_zplDataController.text);
  130. print('printZplOverBluetooth successfully from dart');
  131. } on FlutterZsdkException catch (e) {
  132. inspect(e);
  133. showSnackBar(e.message);
  134. } catch (e) {
  135. inspect(e);
  136. showSnackBar('Unexpected error when send data to printers');
  137. }
  138. setState(() {});
  139. }
  140. Future<void> checkConnectionStatus() async {
  141. print('invoked checkConnectionStatus from dart');
  142. try{
  143. bool isConnected = await _flutterZsdkPlugin.isConnected();
  144. print('isConnected from dart: $isConnected');
  145. } on FlutterZsdkException catch (e) {
  146. inspect(e);
  147. showSnackBar(e.message.toString());
  148. } catch (e) {
  149. inspect(e);
  150. showSnackBar('Unexpected error while checking connection status');
  151. }
  152. }
  153. Future<void> calibrate() async {
  154. print('invoked calibrate from dart');
  155. try{
  156. await _flutterZsdkPlugin.calibrate();
  157. } on FlutterZsdkException catch (e) {
  158. inspect(e);
  159. showSnackBar(e.message.toString());
  160. } catch (e) {
  161. inspect(e);
  162. showSnackBar('Unexpected error while calibrating printer');
  163. }
  164. }
  165. Future<void> debugTest() async {
  166. print('invoked debugTest from dart');
  167. try{
  168. await _flutterZsdkPlugin.debugTest();
  169. } on FlutterZsdkException catch (e) {
  170. inspect(e);
  171. showSnackBar(e.message.toString());
  172. } catch (e) {
  173. inspect(e);
  174. showSnackBar('Unexpected error while checking connection status');
  175. }
  176. }
  177. @override
  178. Widget build(BuildContext context) {
  179. return Scaffold(
  180. appBar: AppBar(
  181. title: const Text('Plugin example app'),
  182. ),
  183. body: Form(
  184. key: _formKey,
  185. child: Center(
  186. child: SingleChildScrollView(
  187. child: Column(
  188. crossAxisAlignment: CrossAxisAlignment.center,
  189. mainAxisAlignment: MainAxisAlignment.center,
  190. children: [
  191. Text('Connected printer: ${_connectedPrinter ?? "Unknown printer"}\n'),
  192. Text('Selected printer Mac Address: $_selectedBluetoothPrinterMacAddress\n'),
  193. SizedBox(height: 20),
  194. ElevatedButton(
  195. child: Text('Connect to $_selectedBluetoothPrinterMacAddress'),
  196. onPressed: _selectedBluetoothPrinterMacAddress == null ? null : openConnection,
  197. ),
  198. SizedBox(height: 20),
  199. ElevatedButton(
  200. child: Text(_disconnecting ? 'Disconnecting...' : 'Disconnect from $_connectedPrinter'),
  201. onPressed: _connectedPrinter == null || _disconnecting ? null : closeConnection,
  202. ),
  203. SizedBox(height: 20),
  204. // debug button
  205. ElevatedButton(
  206. child: Text('Debug'),
  207. onPressed: debugTest
  208. ),
  209. SizedBox(height: 20),
  210. // calibrate button
  211. ElevatedButton(
  212. child: Text('Calibrate'),
  213. onPressed: _connectedPrinter == null || _disconnecting ? null : calibrate
  214. ),
  215. SizedBox(height: 20),
  216. TextFormField(
  217. controller: _zplDataController,
  218. decoration: InputDecoration(
  219. hintText: 'Enter ZPL data here'
  220. ),
  221. validator: (value) {
  222. if (value == null || value.isEmpty) {
  223. return 'Please enter some text';
  224. }
  225. return null;
  226. },
  227. ),
  228. ElevatedButton(
  229. child: Text('Print zpl over bluetooth'),
  230. onPressed: _connectedPrinter == null ? null : printZplOverBluetooth,
  231. ),
  232. SizedBox(height: 20),
  233. ElevatedButton(
  234. child: Text('Check connection status'),
  235. onPressed: checkConnectionStatus,
  236. ),
  237. SizedBox(height: 20),
  238. ElevatedButton(
  239. child: Text(_isDiscovering ? 'Discovering bluetooth printers...' : 'Discover nearby bluetooth printers'),
  240. onPressed: _isDiscovering ? null : () async {
  241. discoverBluetoothDevices();
  242. },
  243. ),
  244. SizedBox(height: 20),
  245. for (var printer in _discoveredBluetoothPrinters) ... [
  246. InkWell(
  247. onTap: () {
  248. _selectedBluetoothPrinterMacAddress = printer.macAddress;
  249. setState(() {});
  250. },
  251. child: Column(
  252. mainAxisSize: MainAxisSize.min,
  253. children: [
  254. Text(printer.friendlyName),
  255. Text(printer.macAddress),
  256. SizedBox(height: 10)
  257. ],
  258. ),
  259. )
  260. ]
  261. ]
  262. ),
  263. ),
  264. ),
  265. ),
  266. );
  267. }
  268. }