flutter plugin for zebra multiplatform sdk

main.dart 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. bool _isDiscovering = false;
  19. List<BluetoothPrinter> _discoveredBluetoothPrinters = [];
  20. String? _connectedPrinter;
  21. final _flutterZsdkPlugin = FlutterZsdk();
  22. String? _selectedBluetoothPrinterMacAddress;
  23. bool _disconnecting = false;
  24. @override
  25. void initState() {
  26. super.initState();
  27. // initPlatformState();
  28. }
  29. // Platform messages are asynchronous, so we initialize in an async method.
  30. // Future<void> initPlatformState() async {
  31. // String platformVersion;
  32. // // Platform messages may fail, so we use a try/catch PlatformException.
  33. // // We also handle the message potentially returning null.
  34. // try {
  35. // platformVersion =
  36. // await _flutterZsdkPlugin.getPlatformVersion() ?? 'Unknown platform version';
  37. // } on PlatformException {
  38. // platformVersion = 'Failed to get platform version.';
  39. // }
  40. // // If the widget was removed from the tree while the asynchronous platform
  41. // // message was in flight, we want to discard the reply rather than calling
  42. // // setState to update our non-existent appearance.
  43. // if (!mounted) return;
  44. // setState(() {
  45. // _platformVersion = platformVersion;
  46. // });
  47. // }
  48. StreamSubscription? _bluetoothPrinterSubscription;
  49. void showSnackBar(String message) {
  50. ScaffoldMessenger.of(context).showSnackBar(
  51. SnackBar(content: Text(message))
  52. );
  53. }
  54. Future<void> discoverBluetoothDevices() async {
  55. try {
  56. Stream<dynamic> stream = await _flutterZsdkPlugin.findBluetoothPrinters();
  57. _bluetoothPrinterSubscription = stream.listen((event) {
  58. print(event);
  59. if (event == 'SOS') {
  60. _isDiscovering = true;
  61. }
  62. if (event is List) {
  63. _discoveredBluetoothPrinters.clear();
  64. for (var printer in event) {
  65. BluetoothPrinter bluetoothPrinter = BluetoothPrinter.fromMap(printer);
  66. _discoveredBluetoothPrinters.add(bluetoothPrinter);
  67. }
  68. }
  69. if (event == 'EOS') {
  70. _isDiscovering = false;
  71. showSnackBar('Bluetooth discovery finished, found ${_discoveredBluetoothPrinters.length} printers');
  72. _bluetoothPrinterSubscription?.cancel();
  73. }
  74. setState(() {});
  75. });
  76. } on PlatformException catch (e) {
  77. inspect(e);
  78. showSnackBar(e.message.toString());
  79. } on FlutterZsdkException catch (e) {
  80. inspect(e);
  81. showSnackBar(e.message);
  82. } catch (e) {
  83. inspect(e);
  84. showSnackBar('Unexpected error while discovering bluetooth printers');
  85. }
  86. }
  87. Future<void> openConnection() async {
  88. print('invoked openConnection from dart');
  89. try {
  90. await _flutterZsdkPlugin.openConnection(_selectedBluetoothPrinterMacAddress ?? '');
  91. _connectedPrinter = _selectedBluetoothPrinterMacAddress ?? 'Unknown printer';
  92. print('Connection opened successfully from dart');
  93. } on FlutterZsdkException catch (e) {
  94. inspect(e);
  95. showSnackBar(e.message);
  96. } catch (e) {
  97. inspect(e);
  98. showSnackBar('Unexpected error while connecting to bluetooth printers');
  99. }
  100. setState(() {});
  101. }
  102. Future<void> closeConnection() async {
  103. print('invoked closeConnection from dart');
  104. try {
  105. setState(() {
  106. _disconnecting = true;
  107. });
  108. await _flutterZsdkPlugin.closeConnection();
  109. _connectedPrinter = null;
  110. _disconnecting = false;
  111. print('Connection closed successfully from dart');
  112. } on FlutterZsdkException catch (e) {
  113. inspect(e);
  114. showSnackBar(e.message);
  115. } catch (e) {
  116. inspect(e);
  117. showSnackBar('Unexpected error while disconnecting bluetooth printers');
  118. }
  119. setState(() {});
  120. }
  121. Future<void> printZplOverBluetooth() async {
  122. print('invoked printZplOverBluetooth from dart');
  123. try {
  124. await _flutterZsdkPlugin.printZplOverBluetooth();
  125. print('printZplOverBluetooth successfully from dart');
  126. } on FlutterZsdkException catch (e) {
  127. inspect(e);
  128. showSnackBar(e.message);
  129. } catch (e) {
  130. inspect(e);
  131. showSnackBar('Unexpected error when send data to printers');
  132. }
  133. setState(() {});
  134. }
  135. @override
  136. Widget build(BuildContext context) {
  137. return Scaffold(
  138. appBar: AppBar(
  139. title: const Text('Plugin example app'),
  140. ),
  141. body: Center(
  142. child: Column(
  143. crossAxisAlignment: CrossAxisAlignment.center,
  144. mainAxisAlignment: MainAxisAlignment.center,
  145. children: [
  146. Text('Connected printer: ${_connectedPrinter ?? "Unknown printer"}\n'),
  147. Text('Selected printer Mac Address: $_selectedBluetoothPrinterMacAddress\n'),
  148. SizedBox(height: 20),
  149. ElevatedButton(
  150. child: Text('Connect to $_selectedBluetoothPrinterMacAddress'),
  151. onPressed: _selectedBluetoothPrinterMacAddress == null || _connectedPrinter != null ? null : openConnection,
  152. ),
  153. SizedBox(height: 20),
  154. ElevatedButton(
  155. child: Text(_disconnecting ? 'Disconnecting...' : 'Disconnect from $_connectedPrinter'),
  156. onPressed: _connectedPrinter == null || _disconnecting ? null : closeConnection,
  157. ),
  158. SizedBox(height: 20),
  159. ElevatedButton(
  160. child: Text('Print zpl over bluetooth'),
  161. onPressed: _connectedPrinter == null ? null : printZplOverBluetooth,
  162. ),
  163. SizedBox(height: 20),
  164. ElevatedButton(
  165. child: Text('Check connection status'),
  166. onPressed: () async {
  167. bool isConnected =await _flutterZsdkPlugin.isConnected();
  168. print('isConnected from dart: $isConnected');
  169. },
  170. ),
  171. SizedBox(height: 20),
  172. ElevatedButton(
  173. child: Text(_isDiscovering ? 'Discovering bluetooth printers...' : 'Discover nearby bluetooth printers'),
  174. onPressed: _isDiscovering ? null : () async {
  175. discoverBluetoothDevices();
  176. },
  177. ),
  178. SizedBox(height: 20),
  179. for (var printer in _discoveredBluetoothPrinters) ... [
  180. InkWell(
  181. onTap: () {
  182. _selectedBluetoothPrinterMacAddress = printer.macAddress;
  183. setState(() {});
  184. },
  185. child: Column(
  186. mainAxisSize: MainAxisSize.min,
  187. children: [
  188. Text(printer.friendlyName),
  189. Text(printer.macAddress),
  190. SizedBox(height: 10)
  191. ],
  192. ),
  193. )
  194. ]
  195. ]
  196. ),
  197. ),
  198. );
  199. }
  200. }