A library to discover printers, and send printer commands.
This library allows to print esc commands to printers in different platforms such as android, ios, windows and different interfaces as Bluetooth and BLE
Inspired by flutter_pos_printer_platform.
- Android, iOS and Windows support
- Scan for bluetooth devices
- Send raw
List<int> bytes
data to a device, review this library to generate ESC/POS commands flutter_esc_pos_utils.
Android | iOS | Windows | Description | |
---|---|---|---|---|
Bluetooth classic interface | ✅ | 🔳 | 🔳 | Allows connection with classic bt devices. |
Bluetooth low energy (BLE) interface | ✅ | ✅ | 🔳 | Allows connection with bt BLE devices. |
scan | ✅ | ✅ | ✅ | Starts a scan for only Bluetooth devices or network devices(Android/iOS). |
connect | ✅ | ✅ | ✅ | Establishes a connection to the device. |
disconnect | ✅ | ✅ | ✅ | Cancels an active or pending connection to the device. |
state | ✅ | ✅ | ✅ | Stream of state changes for the Bluetooth Device. |
✅ | ✅ | ✅ | print bytes. |
For a full example please check /example folder. Here are only the most important parts of the code to illustrate how to use the library.
Generate bytes to print through flutter_esc_pos_utils.
import 'package:esc_pos_utils/esc_pos_utils.dart';
final profile = await CapabilityProfile.load();
final generator = Generator(PaperSize.mm58, profile);
List<int> bytes = [];
bytes += generator.text('Test Print', styles: const PosStyles(align: PosAlign.center));
bytes += generator.text('Product 1');
bytes += generator.text('Product 2');
Allow to connect bluetooth (classic and BLE), USB and network devices
smart_bluetooth_pos_printer is compatible only from version 21 of Android SDK so you should change this in android/app/build.gradle:
In build.gradle set
defaultConfig {
...
minSdkVersion 21
targetSdkVersion 31
...
select type of device PrinterType
( bluetooth, usb, network)
if select bluetooth you can send optional params
- isBle -> allow to connect with bluetooth that supports this technology
- autoconnect -> allow to reconnect when state of device is None
Allow to connect bluetooth (BLE) and network devices
import 'package:smart_bluetooth_pos_printer/smart_bluetooth_pos_printer.dart';
var printerManager = PrinterManager.instance;
var devices = [];
_scan({bool isBle = false}) {
// Find printers
PrinterManager.instance.discovery(isBle: isBle).listen((device) {
devices.add(device);
});
}
_connectDevice(PrinterDevice selectedPrinter, {bool reconnect = false, bool isBle = false}) async {
await printerManager.connect(
model: BluetoothPrinterInput(
name: selectedPrinter!.deviceName,
address: selectedPrinter!.address!,
isBle: selectedPrinter!.isBle ?? false,
autoConnect: _reconnect));
}
_disconnectDevice() async {
await PrinterManager.instance.disconnect();
}
PrinterManager.instance.stateBluetooth.listen((status) {
log(' ----------------- status bt $status ------------------ ');
});
_sendBytesToPrint(List<int> bytes) async {
PrinterManager.instance.send(bytes: bytes);
}
error:'State restoration of CBCentralManager is only allowed for applications that have specified the "bluetooth-central" background mode' info.plist add:
<key>NSBluetoothAlwaysUsageDescription</key>
<string>Allow App use bluetooth?</string>
<key>NSBluetoothPeripheralUsageDescription</key>
<string>Allow App use bluetooth?</string>
<key>UIBackgroundModes</key>
<array>
<string>bluetooth-central</string>
<string>bluetooth-peripheral</string>
</array>
If you think that this project has helped you with your developments, you can support this project, any support is much appreciated.