Skip to main content

flutter_simple_bluetooth_printer 0.1.4 copy "flutter_simple_bluetooth_printer: ^0.1.4" to clipboard
flutter_simple_bluetooth_printer: ^0.1.4 copied to clipboard

A Flutter plugin for connecting Bluetooth printer, support both Android and iOS.

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" />

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>

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);
}

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);
}

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);
}

Connect #

try {
  var _isConnected = await bluetoothManager.connect(address: selectedPrinter.address);
} on BTException catch (e) {
  print(e);
}
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);
}

Disconnect #

try {
  await bluetoothManager.disconnect();
} on BTException catch (e) {
  print(e);
}

Listen to connect state #

var _subscriptionBtStatus = bluetoothManager.connectState.listen((status) {
  print('$status');
});

Get Paired Devices (Android Only) #

try {
  final bondedDevices = await bluetoothManager.getAndroidPairedDevices();
} on BTException catch (e) {
  print(e);
}

See example for full example.

17
likes
120
points
198
downloads

Documentation

API reference

Publisher

unverified uploader

Weekly Downloads

A Flutter plugin for connecting Bluetooth printer, support both Android and iOS.

Repository (GitHub)
View/report issues

License

MIT (license)

Dependencies

flutter, plugin_platform_interface, rxdart

More

Packages that depend on flutter_simple_bluetooth_printer

Packages that implement flutter_simple_bluetooth_printer