A Flutter library for sending bytes to Bluetooth devices. Available on Android and iOS.
This library is only compatible for Android SDK 21+, so we need this in android/app/build.gradle
Android {
defaultConfig {
minSdkVersion 21
This library needs Bluetooth permissions on iOS, so we need this in ios/Runner/Info.plist
...
<key>NSBluetoothAlwaysUsageDescription</key>
<string>Need BLE permission for connecting to BLE devices</string>
<key>NSBluetoothPeripheralUsageDescription</key>
<string>Need BLE permission for retrieving BLE devices info</string>
...
We'll need esc_pos_gen for this example.
First prepare a bill to be printed.
...
finalList<PosComponent> components =<PosComponent>[
constPosText.center('My Store'),
constPosSeparator(),
PosListComponent.builder(
count:20,
builder: (int i) {
returnPosListComponent(
<PosComponent>[
PosRow.leftRightText(
leftText:'Product $i',
leftTextStyles:constPosStyles.defaults(),
rightText:'USD $i',
),
PosRow.leftRightText(
leftText:'1 x USD $i',
leftTextStyles:constPosStyles.defaults(
fontType:PosFontType.fontB,
),
rightText:'USD $i',
rightTextStyles:constPosStyles.defaults(
align:PosAlign.right,
fontType:PosFontType.fontB,
),
),
],
);
},
),
constPosSeparator(),
PosBarcode.code128('{A12345'.split('')),
constPosSeparator(),
constPosFeed(1),
constPosCut(),
];
finalPaper paper =Paper(
generator: generator,
components: components,
);
...Retrieve available bluetooth devices.
finalList<FluetoothDevice> devices =awaitFluetooth().getAvailableDevices();Connect to a printer.
...
finalFluetoothDevice printer = devices.first;
awaitFluetooth().connect(printer.id);Prints the bill to the printer
...
awaitFluetooth().sendBytes(paper.bytes);
...