flutter_simple_bluetooth_printer

Creator: coderz1093

Last updated:

Add to Cart

Description:

flutter simple bluetooth printer

flutter_simple_bluetooth_printer #
A library to discover printers, connect and send printer commands.
Inspired by:
flutter_pos_printer
bluetooth_print
flutter_bluetooth_serial
flutter_blue
Based on:
rxandroidble - Android
lightBlue - iOS
Features #




Android
iOS
Description




classic bluetooth


Support Connect/Print classic bluetooth devices


get paired devices


Get paried devices of Phone.


discovery


Scanning for Bluetooth Low Energy devices.


stop discovery


Stop scanning for Bluetooth Low Energy devices.


scan


Scan for Bluetooth LE devices until timeout is reached.


connect


Establishes a connection to the device.


disconnect


Cancels an connection to the device.


connect state


Stream of connect state changes for the Bluetooth Device.


current connect state


Get the lastest state of connect state.


write text


write text to the connected device.


write raw data


write Uint8List data to the connected device.



Permissions #
Android #
All required permissions are included in the library.
Including:
<!-- Request legacy Bluetooth permissions on older devices. -->
<uses-permission android:name="android.permission.BLUETOOTH" android:maxSdkVersion="30" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" android:maxSdkVersion="30" />

<!-- For Android6(API23)-Android11(API30) -->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

<!-- For Android12(API31)+ -->
<!--@see https://developer.android.google.cn/about/versions/12/features/bluetooth-permissions-->
<uses-permission android:name="android.permission.BLUETOOTH_SCAN" />
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
copied to clipboard
See https://github.com/dariuszseweryn/RxAndroidBle for details.
This plugin will handle Runtime permissions request.
iOS #
Add permissions to ios/Runner/Info.plist:
<key>NSBluetoothAlwaysUsageDescription</key>
<string>Replace with your description here</string>
<key>NSBluetoothPeripheralUsageDescription</key>
<string>Replace with your description here</string>
<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string>Replace with your description here</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>Replace with your description here</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>Replace with your description here</string>
<key>UIBackgroundModes</key>
<array>
<string>bluetooth-central</string>
<string>bluetooth-peripheral</string>
</array>
copied to clipboard
Discovery & Stop discovery #
try{
var subsription = bluetoothManager.discovery().listen((device) {
// A new device is discovered.
devices.add(device);
});
Future.delayed(Duration(seconds: 20)).then((_) {
// Remember to stop discovery after use.
bluetoothManager.stopDiscovery();
subsription.cancel();
});
} on BTException catch(e){
print(e);
}
copied to clipboard
Or listen to scanResults to get the full list of devices.
try{
bluetoothManager.discovery();
var subsription = bluetoothManager.scanResults.listen((list) {
// Every time a new device is discovered, the full list of devices is callback.
devices = list;
});
Future.delayed(Duration(seconds: 20)).then((_) {
// Remember to stop discovery after use.
bluetoothManager.stopDiscovery();
subsription.cancel();
});
} on BTException catch(e){
print(e);
}
copied to clipboard
Scan #
Similar to Discovery, but stop automatically after timeout is reached.
try{
final devices = await bluetoothManager.scan(timeout: const Duration(seconds: 10));
} on BTException catch(e){
print(e);
}
copied to clipboard
Connect #
try {
var _isConnected = await bluetoothManager.connect(address: selectedPrinter.address);
} on BTException catch (e) {
print(e);
}
copied to clipboard
Print (by Text or Unit8List) #
try {
if (!await connect()) return; // Make sure connected to device before print
final isSuccess = await bluetoothManager.writeText(text);
// Or print by Unit8List
// final isSuccess = await bluetoothManager.writeRawData(codes);
} on BTException catch (e) {
print(e);
}
copied to clipboard
Disconnect #
try {
await bluetoothManager.disconnect();
} on BTException catch (e) {
print(e);
}
copied to clipboard
Listen to connect state #
var _subscriptionBtStatus = bluetoothManager.connectState.listen((status) {
print('$status');
});
copied to clipboard
Get Paired Devices (Android Only) #
try {
final bondedDevices = await bluetoothManager.getAndroidPairedDevices();
} on BTException catch (e) {
print(e);
}
copied to clipboard

See example for full example.

License

For personal and professional use. You cannot resell or redistribute these repositories in their original state.

Customer Reviews

There are no reviews.