flutter plugin for zebra multiplatform sdk

main.dart 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 _platformVersion = 'Unknown';
  21. final _flutterZsdkPlugin = FlutterZsdk();
  22. @override
  23. void initState() {
  24. super.initState();
  25. initPlatformState();
  26. }
  27. // Platform messages are asynchronous, so we initialize in an async method.
  28. Future<void> initPlatformState() async {
  29. String platformVersion;
  30. // Platform messages may fail, so we use a try/catch PlatformException.
  31. // We also handle the message potentially returning null.
  32. try {
  33. platformVersion =
  34. await _flutterZsdkPlugin.getPlatformVersion() ?? 'Unknown platform version';
  35. } on PlatformException {
  36. platformVersion = 'Failed to get platform version.';
  37. }
  38. // If the widget was removed from the tree while the asynchronous platform
  39. // message was in flight, we want to discard the reply rather than calling
  40. // setState to update our non-existent appearance.
  41. if (!mounted) return;
  42. setState(() {
  43. _platformVersion = platformVersion;
  44. });
  45. }
  46. StreamSubscription? _bluetoothPrinterSubscription;
  47. void showSnackBar(String message) {
  48. ScaffoldMessenger.of(context).showSnackBar(
  49. SnackBar(content: Text(message))
  50. );
  51. }
  52. Future<void> discoverBluetoothDevices() async {
  53. try {
  54. Stream<dynamic> stream = await _flutterZsdkPlugin.findBluetoothPrinters();
  55. _bluetoothPrinterSubscription = stream.listen((event) {
  56. print(event);
  57. if (event == 'SOS') {
  58. _isDiscovering = true;
  59. }
  60. if (event is List) {
  61. _discoveredBluetoothPrinters.clear();
  62. for (var printer in event) {
  63. BluetoothPrinter bluetoothPrinter = BluetoothPrinter.fromMap(printer);
  64. _discoveredBluetoothPrinters.add(bluetoothPrinter);
  65. }
  66. }
  67. if (event == 'EOS') {
  68. _isDiscovering = false;
  69. showSnackBar('Bluetooth discovery finished, found ${_discoveredBluetoothPrinters.length} printers');
  70. _bluetoothPrinterSubscription?.cancel();
  71. }
  72. setState(() {});
  73. });
  74. } on PlatformException catch (e) {
  75. inspect(e);
  76. showSnackBar(e.message.toString());
  77. } on FlutterZsdkException catch (e) {
  78. inspect(e);
  79. showSnackBar(e.message);
  80. } catch (e) {
  81. inspect(e);
  82. showSnackBar('Unexpected error while discovering bluetooth printers');
  83. }
  84. }
  85. @override
  86. Widget build(BuildContext context) {
  87. return Scaffold(
  88. appBar: AppBar(
  89. title: const Text('Plugin example app'),
  90. ),
  91. body: Center(
  92. child: Column(
  93. crossAxisAlignment: CrossAxisAlignment.center,
  94. mainAxisAlignment: MainAxisAlignment.center,
  95. children: [
  96. Text('Running on: $_platformVersion\n'),
  97. SizedBox(height: 20),
  98. ElevatedButton(
  99. child: Text(_isDiscovering ? 'Discovering bluetooth printers...' : 'Discover nearby bluetooth printers'),
  100. onPressed: _isDiscovering ? null : () async {
  101. discoverBluetoothDevices();
  102. },
  103. ),
  104. SizedBox(height: 20),
  105. for (var printer in _discoveredBluetoothPrinters) ... [
  106. Text(printer.friendlyName),
  107. Text(printer.macAddress),
  108. SizedBox(height: 10)
  109. ]
  110. ]
  111. ),
  112. ),
  113. );
  114. }
  115. }