bluetooth_print_plus 2.4.0 bluetooth_print_plus: ^2.4.0 copied to clipboard
bluetooth_print_plus is a flutter plugin for bluetooth thermal printer, support Android & iOS, supports tspl/tsc、cpcl、esc pos.
Introduction #
Bluetooth Print Plus is a Bluetooth plugin used to print thermal printers in Flutter, a new mobile SDK to help developers build bluetooth thermal printer apps for iOS and Android.
Important, important, important. First, you need to run the demo to confirm the printer command type ! ! ! now support tspl/tsc、cpcl、esc pos. If this plugin is helpful to you, please give it a like, Thanks.FAQ Support #
Buy Me A Coffee/请我喝杯咖啡 #
Plan #
Version | plan |
---|---|
1.1.x | blue and tsc command, esc print image command |
1.5.x | support cpcl command |
2.x.x | improve esc command |
3.x.x | support zpl command |
Features #
Android | iOS | Description | |
---|---|---|---|
scan | ✅ | ✅ | Starts a scan for Bluetooth Low Energy devices. |
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. |
Usage #
To use this plugin : #
- add the dependency to your pubspec.yaml file.
dependencies:
flutter:
sdk: flutter
bluetooth_print_plus: ^2.4.0
Add permissions for Bluetooth #
We need to add the permission to use Bluetooth and access location:
Android
In the android/app/src/main/AndroidManifest.xml let’s add:
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_SCAN" />
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
IOS
In the ios/Runner/Info.plist let’s add:
<key>NSBluetoothAlwaysUsageDescription</key>
<string>Need BLE permission</string>
<key>NSBluetoothPeripheralUsageDescription</key>
<string>Need BLE permission</string>
init BluetoothPrintPlus instance #
import 'package:bluetooth_print_plus/bluetooth_print_plus.dart';
final _bluetoothPrintPlus = BluetoothPrintPlus.instance;
BluetoothPrintPlus useful property #
_bluetoothPrintPlus.isBlueOn;
_bluetoothPrintPlus.isScanning;
_bluetoothPrintPlus.isConnected;
listen #
- state
/// listen blue state
_bluetoothPrintPlus.blueState.listen((event) {
print('********** blueState change: $event **********');
/// blue state changed, do something...
});
/// listen connect state
_bluetoothPrintPlus.connectState.listen((event) {
print('********** connectState change: $event **********');
/// connect state changed, do something...
});
- received Data
_bluetoothPrintPlus.receivedData.listen((data) {
print('********** received data: $data **********');
/// received data, do something...
});
scan #
// begin scan
_bluetoothPrintPlus.startScan(timeout: const Duration(seconds: 8));
// get devices
StreamBuilder<List<BluetoothDevice>>(
stream: _bluetoothPrintPlus.scanResults,
initialData: [],
builder: (c, snapshot) => ListView(
children: snapshot.data!.map((d) => Container(
padding: const EdgeInsets.only(left: 10, right: 10, bottom: 5),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [ .... ],
),
)).toList(),
),
)
connect #
await _bluetoothPrintPlus.connect(_device);
disconnect #
await _bluetoothPrintPlus.disconnect();
or
await BluetoothPrintPlus.instance.disconnect();
print/write #
/// for example: write tsc command
final ByteData bytes = await rootBundle.load("assets/dithered-image.png");
final Uint8List image = bytes.buffer.asUint8List();
await tscCommand.cleanCommand();
await tscCommand.size(width: 76, height: 130);
await tscCommand.cls(); // most after size
await tscCommand.image(image: image, x: 50, y: 60);
await tscCommand.print(1);
final cmd = await tscCommand.getCommand();
if (cmd == null) return;
BluetoothPrintPlus.instance.write(cmd);
/// for example: write cpcl command
await cpclCommand.cleanCommand();
await cpclCommand.size(width: 76 * 8, height: 76 * 8);
await cpclCommand.image(image: image, x: 10, y: 10);
await cpclCommand.print();
final cmd = await cpclCommand.getCommand();
if (cmd == null) return;
BluetoothPrintPlus.instance.write(cmd);
/// for example: write esc command
await escCommand.cleanCommand();
await escCommand.print();
await escCommand.image(image: image);
await escCommand.print();
final cmd = await escCommand.getCommand();
if (cmd == null) return;
BluetoothPrintPlus.instance.write(cmd);
Troubleshooting #
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>