flutter plugin for zebra multiplatform sdk

main.dart 5.7KB

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