123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326 |
- import 'dart:developer';
- import 'dart:math';
-
- import 'package:flutter/material.dart';
- import 'dart:async';
-
- import 'package:flutter/services.dart';
- import 'package:flutter_zsdk/flutter_zsdk.dart';
- import 'package:flutter_zsdk/src/models/flutter_zsdk_exception.dart';
- import 'package:flutter_zsdk_example/models/bluetooth_printer.dart';
-
- void main() {
- runApp(const MaterialApp(home: MyApp()));
- }
-
- class MyApp extends StatefulWidget {
- const MyApp({super.key});
-
- @override
- State<MyApp> createState() => _MyAppState();
- }
-
- class _MyAppState extends State<MyApp> {
- final _formKey = GlobalKey<FormState>();
-
- bool _isDiscovering = false;
- List<BluetoothPrinter> _discoveredBluetoothPrinters = [];
- String? _connectedPrinter;
- final _flutterZsdkPlugin = FlutterZsdk();
- String? _selectedBluetoothPrinterMacAddress;
- bool _disconnecting = false;
-
- TextEditingController _zplDataController = TextEditingController();
-
- @override
- void initState() {
- super.initState();
- // initPlatformState();
- }
-
- // Platform messages are asynchronous, so we initialize in an async method.
- // Future<void> initPlatformState() async {
- // String platformVersion;
- // // Platform messages may fail, so we use a try/catch PlatformException.
- // // We also handle the message potentially returning null.
- // try {
- // platformVersion =
- // await _flutterZsdkPlugin.getPlatformVersion() ?? 'Unknown platform version';
- // } on PlatformException {
- // platformVersion = 'Failed to get platform version.';
- // }
-
- // // If the widget was removed from the tree while the asynchronous platform
- // // message was in flight, we want to discard the reply rather than calling
- // // setState to update our non-existent appearance.
- // if (!mounted) return;
-
- // setState(() {
- // _platformVersion = platformVersion;
- // });
- // }
-
- StreamSubscription? _bluetoothPrinterSubscription;
-
- void showSnackBar(String message) {
- ScaffoldMessenger.of(context).showSnackBar(
- SnackBar(content: Text(message))
- );
- }
-
- Future<void> discoverBluetoothDevices() async {
- try {
- Stream<dynamic> stream = await _flutterZsdkPlugin.findBluetoothPrinters();
- _bluetoothPrinterSubscription = stream.listen((event) {
- print(event);
-
- if (event == 'SOS') {
- _isDiscovering = true;
-
- }
-
- if (event is List) {
- _discoveredBluetoothPrinters.clear();
- for (var printer in event) {
- BluetoothPrinter bluetoothPrinter = BluetoothPrinter.fromMap(printer);
-
- _discoveredBluetoothPrinters.add(bluetoothPrinter);
- }
-
- }
-
- if (event == 'EOS') {
- _isDiscovering = false;
- showSnackBar('Bluetooth discovery finished, found ${_discoveredBluetoothPrinters.length} printers');
- _bluetoothPrinterSubscription?.cancel();
- }
-
- setState(() {});
- });
-
- } on PlatformException catch (e) {
- inspect(e);
- showSnackBar(e.message.toString());
-
- } on FlutterZsdkException catch (e) {
- inspect(e);
- showSnackBar(e.message);
-
- } catch (e) {
- inspect(e);
- showSnackBar('Unexpected error while discovering bluetooth printers');
-
- }
- }
-
- Future<void> openConnection() async {
- print('invoked openConnection from dart');
- try {
- await _flutterZsdkPlugin.openConnection(_selectedBluetoothPrinterMacAddress ?? '');
- _connectedPrinter = _selectedBluetoothPrinterMacAddress ?? 'Unknown printer';
- print('Connection opened successfully from dart');
-
- } on FlutterZsdkException catch (e) {
- inspect(e);
- showSnackBar(e.message);
-
- } catch (e) {
- inspect(e);
- showSnackBar('Unexpected error while connecting to bluetooth printers');
- }
-
- setState(() {});
- }
-
- Future<void> closeConnection() async {
- print('invoked closeConnection from dart');
- try {
- setState(() {
- _disconnecting = true;
- });
- await _flutterZsdkPlugin.closeConnection();
- _connectedPrinter = null;
- _disconnecting = false;
- print('Connection closed successfully from dart');
-
- } on FlutterZsdkException catch (e) {
- inspect(e);
- showSnackBar(e.message);
-
- } catch (e) {
- inspect(e);
- showSnackBar('Unexpected error while disconnecting bluetooth printers');
- }
-
- setState(() {});
- }
-
- Future<void> printZplOverBluetooth() async {
- if(!_formKey.currentState!.validate()) {
- return;
- }
-
- print('invoked printZplOverBluetooth from dart');
- try {
- await _flutterZsdkPlugin.printZplOverBluetooth(_zplDataController.text);
- print('printZplOverBluetooth successfully from dart');
-
- } on FlutterZsdkException catch (e) {
- inspect(e);
- showSnackBar(e.message);
-
- } catch (e) {
- inspect(e);
- showSnackBar('Unexpected error when send data to printers');
- }
-
- setState(() {});
- }
-
- Future<void> checkConnectionStatus() async {
- print('invoked checkConnectionStatus from dart');
- try{
- bool isConnected = await _flutterZsdkPlugin.isConnected();
-
- print('isConnected from dart: $isConnected');
- } on FlutterZsdkException catch (e) {
- inspect(e);
- showSnackBar(e.message.toString());
- } catch (e) {
- inspect(e);
- showSnackBar('Unexpected error while checking connection status');
- }
- }
-
- Future<void> calibrate() async {
- print('invoked calibrate from dart');
- try{
- await _flutterZsdkPlugin.calibrate();
-
- } on FlutterZsdkException catch (e) {
- inspect(e);
- showSnackBar(e.message.toString());
- } catch (e) {
- inspect(e);
- showSnackBar('Unexpected error while calibrating printer');
- }
- }
-
- Future<void> debugTest() async {
- print('invoked debugTest from dart');
- try{
- await _flutterZsdkPlugin.debugTest();
-
- } on FlutterZsdkException catch (e) {
- inspect(e);
- showSnackBar(e.message.toString());
- } catch (e) {
- inspect(e);
- showSnackBar('Unexpected error while checking connection status');
- }
- }
-
- @override
- Widget build(BuildContext context) {
- return Scaffold(
- appBar: AppBar(
- title: const Text('Plugin example app'),
- ),
- body: Form(
- key: _formKey,
- child: Center(
- child: SingleChildScrollView(
- child: Column(
- crossAxisAlignment: CrossAxisAlignment.center,
- mainAxisAlignment: MainAxisAlignment.center,
- children: [
- Text('Connected printer: ${_connectedPrinter ?? "Unknown printer"}\n'),
- Text('Selected printer Mac Address: $_selectedBluetoothPrinterMacAddress\n'),
- SizedBox(height: 20),
-
- ElevatedButton(
- child: Text('Connect to $_selectedBluetoothPrinterMacAddress'),
- onPressed: _selectedBluetoothPrinterMacAddress == null ? null : openConnection,
- ),
- SizedBox(height: 20),
-
- ElevatedButton(
- child: Text(_disconnecting ? 'Disconnecting...' : 'Disconnect from $_connectedPrinter'),
- onPressed: _connectedPrinter == null || _disconnecting ? null : closeConnection,
- ),
- SizedBox(height: 20),
-
- // debug button
- ElevatedButton(
- child: Text('Debug'),
- onPressed: debugTest
- ),
- SizedBox(height: 20),
-
- // calibrate button
- ElevatedButton(
- child: Text('Calibrate'),
- onPressed: _connectedPrinter == null || _disconnecting ? null : calibrate
- ),
- SizedBox(height: 20),
-
- TextFormField(
- controller: _zplDataController,
- decoration: InputDecoration(
- hintText: 'Enter ZPL data here'
- ),
- validator: (value) {
- if (value == null || value.isEmpty) {
- return 'Please enter some text';
- }
- return null;
- },
- ),
-
- ElevatedButton(
- child: Text('Print zpl over bluetooth'),
- onPressed: _connectedPrinter == null ? null : printZplOverBluetooth,
- ),
- SizedBox(height: 20),
-
- ElevatedButton(
- child: Text('Check connection status'),
- onPressed: checkConnectionStatus,
- ),
- SizedBox(height: 20),
-
- ElevatedButton(
- child: Text(_isDiscovering ? 'Discovering bluetooth printers...' : 'Discover nearby bluetooth printers'),
- onPressed: _isDiscovering ? null : () async {
- discoverBluetoothDevices();
- },
- ),
- SizedBox(height: 20),
-
- for (var printer in _discoveredBluetoothPrinters) ... [
- InkWell(
- onTap: () {
- _selectedBluetoothPrinterMacAddress = printer.macAddress;
- setState(() {});
- },
- child: Column(
- mainAxisSize: MainAxisSize.min,
- children: [
- Text(printer.friendlyName),
- Text(printer.macAddress),
- SizedBox(height: 10)
- ],
- ),
- )
- ]
- ]
-
- ),
- ),
- ),
- ),
-
- );
- }
- }
|