brady_flutter_plugin 1.0.4+2 brady_flutter_plugin: ^1.0.4+2 copied to clipboard
The official Flutter Plugin to integrate the Brady SDK within a Flutter application. The Brady SDK will allow users to discovery, connect, and print to Brady printers.
brady_flutter_plugin #
The official Flutter Plugin to integrate the Brady SDK within a Flutter application. The Brady SDK will allow users to discover, connect, and print to Brady printers.
Getting Started #
To demonstrate the basic functionalities of the Brady SDK Flutter Plugin, copy and paste the main.dart under the "example" tab into a fresh Flutter project.
main.dart #
import 'package:flutter/material.dart';
import 'dart:async';
import 'dart:convert';
import 'package:flutter/services.dart';
import 'package:brady_flutter_plugin/brady_flutter_plugin.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
String _platformVersion = 'Unknown';
final _bradyFlutterPlugin = BradyFlutterPlugin();
@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 _bradyFlutterPlugin.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;
});
}
//Starts a bluetooth scan of nearby devices. Only stops the scan when a connection is successfully made.
//.getPrinters() can be called to see all of the found devices.
Future<void> discover() async {
_bradyFlutterPlugin.startPrinterDiscovery();
//This is an example of how to use the lastConnectedPrinter to automatically connect to a printer when it's discovered
String? lastConnectedPrinter = await _bradyFlutterPlugin.getLastConnectedPrinterName();
if (lastConnectedPrinter != null) {
connect();
}
}
//Connects to a found printer via the printer's name as a String.
Future<void> connect() async {
final printerNames = await _bradyFlutterPlugin.getPrinters();
for (final printer in printerNames) {
if (printer! == "M211-PGM2112130501019") {
final connected = await _bradyFlutterPlugin.connectToPrinter(printer);
if (connected == true) {
debugPrint("CONNECTED! :)");
debugPrint(await _bradyFlutterPlugin.getPrinterDetails());
}
else {
debugPrint("FAILED TO CONNECT! :(");
}
}
}
}
//Looks for a local template, converts it to a base64 string, and sets the template to the one that will be printed next.
void setTemplate() async {
final byteData = await rootBundle.load("assets/flutter_barcode_test.BWT");
final bytes = byteData.buffer.asUint8List();
_bradyFlutterPlugin.setTemplate(base64.encode(bytes));
_bradyFlutterPlugin.setPlaceholderValue("BARCODE 1", "54321");
}
//Prints the set template.
Future<void> print() async {
final printed = await _bradyFlutterPlugin.print();
if (printed == true) {
debugPrint("PRINTING SUCCESSFUL! :)");
}
else {
debugPrint("PRINTING FAILED! :(");
}
}
//Feeds a single label out of the printer.
Future<void> feed() async {
final fed = await _bradyFlutterPlugin.feed();
if (fed == true) {
debugPrint("FED SUCCESSFUL! :)");
}
else {
debugPrint("FED FAILED! :(");
}
}
//Performs the cut command on the printer.
Future<void> cut() async {
final cut = await _bradyFlutterPlugin.cut();
if (cut == true) {
debugPrint("CUT SUCCESSFUL! :)");
}
else {
debugPrint("CUT FAILED! :(");
}
}
//Disconnects from the currently connected printer.
Future<void> disconnect() async {
final disconnected = await _bradyFlutterPlugin.disconnect();
if (disconnected == true) {
debugPrint("DISCONNECT SUCCESSFUL! :)");
}
else {
debugPrint("DISCONNECT FAILED! :(");
}
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Plugin example app'),
),
body: Center(
child: Column (
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget> [
ElevatedButton(onPressed: () {
discover();
}, child: const Text('Start Discovery')),
ElevatedButton(onPressed: () {
connect();
}, child: const Text('Connect')),
ElevatedButton(onPressed: () {
setTemplate();
}, child: const Text('Set Template')),
ElevatedButton(onPressed: () {
print();
}, child: const Text('Print')),
ElevatedButton(onPressed: () {
feed();
}, child: const Text('Feed')),
ElevatedButton(onPressed: () {
cut();
}, child: const Text('Cut')),
ElevatedButton(onPressed: () {
disconnect();
}, child: const Text('Disconnect')),
],
),
),
),
);
}
}