bluetooth_print 1.0.1
bluetooth_print: ^1.0.1 copied to clipboard
A new flutter plugin project connect to bluetooth thermal printer (Gprinter).
Introduction #
BluetoothPrint is a bluetooth plugin for Flutter, a new mobile SDK to help developers build bluetooth thermal printer apps for both iOS and Android.(for example, Gprinter pt-280、pt-380、gp-1324、gp-2120 eg.)
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. |
print test message | ✅ | ✅ | print device test message. |
print text | ✅ | ✅ | print custom text, support layout. |
print image | ✅ | ✅ | print image. |
print qrcode | ✅ | ✅ | print qrcode,support change size. |
print barcode | ✅ | ✅ | print barcode |
Usage #
To use this plugin :
- add the dependency to your pubspec.yaml file.
dependencies:
flutter:
sdk: flutter
bluetooth_print:
- init a BluetoothPrint instance
import 'package:bluetooth_print/bluetooth_print.dart';
import 'package:bluetooth_print/bluetooth_print_model.dart';
BluetoothPrint bluetoothPrint = BluetoothPrint.instance;
scan #
// begin scan
bluetoothPrint.startScan(timeout: Duration(seconds: 4));
// get devices
StreamBuilder<List<BluetoothDevice>>(
stream: bluetoothPrint.scanResults,
initialData: [],
builder: (c, snapshot) => Column(
children: snapshot.data.map((d) => ListTile(
title: Text(d.name??''),
subtitle: Text(d.address),
onTap: () async {
setState(() {
_device = d;
});
},
trailing: _device!=null && _device.address == d.address?Icon(
Icons.check,
color: Colors.green,
):null,
)).toList(),
),
),
connect #
await bluetoothPrint.connect(_device);
disconnect #
await bluetoothPrint.disconnect();
listen state #
bluetoothPrint.state.listen((state) {
print('cur device status: $state');
switch (state) {
case BluetoothPrint.CONNECTED:
setState(() {
_connected = true;
});
break;
case BluetoothPrint.DISCONNECTED:
setState(() {
_connected = false;
});
break;
default:
break;
}
});
Troubleshooting #
ios import third party library
[Please Read link: https://www.jianshu.com/p/a8a05ab9b895](https://www.jianshu.com/p/a8a05ab9b895)
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>