DEV Community

Cover image for Flutter thermal printer paired but silent? Three checks before you blame the code
Stacklyn Labs
Stacklyn Labs

Posted on

Flutter thermal printer paired but silent? Three checks before you blame the code

The shop opens tomorrow. Your Flutter app finds the receipt printer, the phone's Settings screen says "paired", you tap Print, and the paper stays blank. Or worse, it prints, but every item name wraps and the price lands on the next line.

This post covers three causes that show up before code pages or logos: the connection, the Bluetooth type, and the paper width.

First rule out the boring stuff. Thermal paper marks on one side only, so check it is loaded the right way up. Many printers print a self-test page if you hold the feed button while powering on. If the self-test prints, the hardware is fine and the problem is in your app.

1. Paired is not connected

Android's Bluetooth docs keep these apart. Paired means the two devices know each other and share a link key. Connected means they currently share an RFCOMM channel and can send data. A printer can sit in Settings as paired for months while nothing in your app is connected to it.

Three things break the connection.

Another phone holds the printer. RFCOMM allows one connected client per channel at a time. If the owner's personal phone connected yesterday and is still nearby with Bluetooth on, your connect call fails. Android's socket connect gives up after about 12 seconds. Turn Bluetooth off on nearby phones, power-cycle the printer, and try again.

Android 12 permissions. Apps targeting Android 12 (API 31) or higher need BLUETOOTH_CONNECT to talk to paired devices and BLUETOOTH_SCAN to find new ones. Both are runtime permissions, so declaring them in the manifest is not enough:

<!-- Android 11 and lower -->
<uses-permission android:name="android.permission.BLUETOOTH" android:maxSdkVersion="30" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" android:maxSdkVersion="30" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" android:maxSdkVersion="30" />

<!-- Android 12 and higher -->
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
<uses-permission android:name="android.permission.BLUETOOTH_SCAN"
    android:usesPermissionFlags="neverForLocation" />
Enter fullscreen mode Exit fullscreen mode

Your app ignores the result. In print_bluetooth_thermal, both connect and writeBytes return Future<bool>. Its isPermissionBluetoothGranted getter only checks the permission, it doesn't request it. If you never read those results, a failed connection looks exactly like a printer that ignored you.

Test the link with the smallest payload there is:

final connected =
    await PrintBluetoothThermal.connect(macPrinterAddress: mac);
if (!connected) throw StateError('Printer did not accept the connection');

// ESC @ (initialise), then "TEST", then three line feeds
final ok = await PrintBluetoothThermal.writeBytes(
    [0x1B, 0x40, 0x54, 0x45, 0x53, 0x54, 0x0A, 0x0A, 0x0A]);
if (!ok) throw StateError('Write failed');
Enter fullscreen mode Exit fullscreen mode

If TEST comes out, the link works. Anything still wrong is about the bytes you send.

2. Works on Android, invisible on iPhone

No code fixes this one, so ask about it before you write any code.

Many low-cost receipt printers speak only Bluetooth Classic with the Serial Port Profile (SPP): open a socket, stream bytes. iOS doesn't offer that to ordinary apps. Core Bluetooth reaches a Classic device only if it supports GATT over Classic. SPP-style communication needs Apple's MFi Program and the External Accessory framework, and the accessory's manufacturer decides which apps may talk to it. A generic marketplace printer is unlikely to have your app on that list.

The packages follow the same line:

Package What it says
blue_thermal_printer Android only
print_bluetooth_thermal Paired devices on Android, nearby (BLE) devices on iOS
flutter_blue_plus Bluetooth Classic not supported on any platform

So the question for the client is simple: which printer do you own, and do you need iPhones? Android only works with a Classic SPP printer. If iOS matters too, they need a printer that also speaks BLE, an MFi printer whose vendor approves your app, or a network printer. If a printer shows up on Android and not on iOS, check whether it advertises BLE before you debug anything else.

3. 58mm vs 80mm: do the width maths

Columns that drift and lines that wrap usually mean the layout was built for one paper width and printed on the other. Many low-cost thermal printers run at 203 dpi, which is 8 dots per millimetre, and the printable area is narrower than the paper. Citizen's CT-S300 spec gives:

58 mm paper 80 mm paper
Printable width 48 mm, 384 dots 72 mm, 576 dots
Font A (12 x 24 dots) 32 characters 48 characters
Font B (9 x 17 dots) 42 characters 64 characters

Characters per line is dots divided by character width: 384 / 12 = 32. Not every printer is 203 dpi, so read the spec sheet for the actual model.

esc_pos_utils_plus has these widths built in: PaperSize.mm58 is 384 dots and PaperSize.mm80 is 576. Two mistakes follow from that.

  • A row padded to 48 characters for 80 mm wraps at 32 on 58 mm, and the price lands under the item name.
  • A Generator set to mm80 on a 58 mm printer centres text and images for 576 dots on a 384-dot head.

Derive column widths from the paper size instead of hard-coding them:

final paper = settings.paperSize; // PaperSize.mm58 or PaperSize.mm80
final gen = Generator(paper, await CapabilityProfile.load());

// Font A is 12 dots wide on a 203 dpi head
final cols = paper == PaperSize.mm58 ? 32 : 48;
final nameWidth = cols - 10; // leave 10 characters for the price

String line(String name, String price) =>
    name.padRight(nameWidth).substring(0, nameWidth) +
    price.padLeft(cols - nameWidth);
Enter fullscreen mode Exit fullscreen mode

Make paper size a setting the shop can change, and store each sale as data, not as printed bytes. That way any receipt can be rebuilt at either width.

The order to check

  1. Self-test page prints? Then the hardware is fine.
  2. connect and writeBytes both return true, and a TEST print works?
  3. iPhone involved? Confirm the printer speaks BLE.
  4. Generator paper size and column widths match the real paper.

Further reading: Flutter Bluetooth Thermal Printer Not Printing: Fixes.

Which printer has given you the most trouble from Flutter, and was it the connection, iOS, or the paper width?

Top comments (0)