Recently I faced the issue of Bluetooth printing in Flutter. But finally solved after 3 days and of course, I take a break to get inspiration, and here is the story.
I used 2 printing libraries,
esc_pos_bluetooth to solve the printing problem.
So I tried running a sample project from its repository and connected it to the printer successfully.
But there was something wrong while integrating the package into my Android project. There are no error messages, no warning, no useful information for debugging, so in other words, no hints or clues.
At first, I thought the issue came from plugin dependency. After two days of trial and error, and finally on the next day, found the issue came from
targetSdkVersion in Android Gradle.
Okay, it seems a little dramatic 😀 but here are my steps to solve this problem. On my way to finding solutions, I spent a lot of time researching to get the best results that fit my project.
At least until published this story, there are no new compatible versions of these plugins yet.
Researching some bluetooth plugins to connect and print. And found
The last two was my choice. Testing sample from its repository, the result is passed, it worked!
Then, Integrate into my project, and.. it failed! So I tried to copy the repo into a local project and got the log below
Because mywidget depends on esc_pos_bluetooth 0.2.8 which depends on esc_pos_utils ^0.3.6, esc_pos_utils ^0.3.6 is required. So, because mywidget depends on esc_pos_utils ^1.0.0, version solving failed.
I downgrade library version from version esc_pos_utils:
1.0.0 to esc_pos_utils:
0.3.6. While version esc_pos_bluetooth: 0.2.8 is no change.
I got an amazing thing, actually, no errors but also no printers found LOL! You know if everything seems fine but isn’t really working, it’s hard to debug because there’s no clue.
And … of course, I’ve made sure the printer and Bluetooth are on and connected to my phone.
Analyzing a sample project, wondering if it went well in just the sample project? I’m looking for the file what is the cause of this problem.
After several attempts with unsuccessful results as none of the files had any significant differences, I searched Google and saw if anyone else had this problem and resolved it, but I didn’t find the same problem.
So in the end, I manage to open up two Gradle files
android/app/build.gradle from the sample and the other from my project.
And… after inspecting carefully, found the
targetSdkVersion 28 (from the sample) and version 29 from my project, maybe it was the default value from
flutter-create command result.
Finally, I downgrade the target to 28, and the problem solved! So I continued to design receipts and this is the result.
Receipt printed successfully via Bluetooth thermal printer. Here are 58mm thermal printer and the receipt.
Maybe next time I’ll tell you this receipt design process 🙂 but I’m wondering how to print vertical text for labels. There is an optional parameter in
esc_pos_utils that change 90 degrees for the text but no effect.
Thank you all
And the important thing is.. thanks all for those plugins creator for Bluetooth printing in Flutter. All of you so wonderful.