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.
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.
In the journey to find a solution, I spent a lot of time researching to get the best result that fit my project. At least until published this story, there are no new compatible versions of these plugins yet.
Research some Bluetooth plugin to connect and print. 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.
Analyze sample project, wondering it run successfully in sample project only? So 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 is 58mm thermal printer and the receipt
Maybe next time I will tell you the process of design this receipt 🙂
Thank you all
And the important thing is.. thanks all for those plugins creator for Bluetooth printing in Flutter. All of you so wonderful.