Introduction
Bluetooth printer is a wireless printer that uses Bluetooth technology to connect to a computer or other Bluetooth-enabled device. Its main benefit is that it is portable without having to hook up to any cables. In this article, I am going to show you how to create a Flutter mobile app to scan, connect and print from your mobile phone to a Bluetooth printer. In order for you to understand, visualise and learn easily, I have attached many screenshots in this article. Now let’s get our hands dirty.
Prerequisite
- Download and Install Visual Studio Code IDE
- Download and Install Flutter SDK
- Bluetooth Printer (Epson TM-80P)
- Android Mobile Phone (Samsung Galaxy S21 Ultra 5G)
There are 2 types of Bluetooth Technologies:
- Bluetooth Classic
- Bluetooth Low Energy (BLE)
For Android, it is a more open platform. So it can support both Bluetooth technologies. Unfortunately for iOS, it can only support BLE generally. Therefore you will need to find out what Bluetooth technology your Bluetooth printer is using. I’m using Epson TM-80P Bluetooth printer in this tutorial. It is using Bluetooth 3.0 (EDR supported) which is Bluetooth Classic. So iOS devices will not be able to connect to this Bluetooth printer.
Setup Development Environment
First, you need to download and install an IDE. I choose Visual Studio Code because it is lightweight. In addition, there are many extensions and tools that you can install into the IDE to make your work easier. It is definitely the best IDE for a beginner or a productive tool for a professional programmer. Now let’s start setting up the environment on your machine.
- Go to https://code.visualstudio.com, download and install Visual Studio Code for your operating system (OS).
2. Go to https://docs.flutter.dev/get-started/install to download and install Flutter SDK for your operating system (OS). You will need to follow the instructions there to setup your development environment for Android and iOS platforms.
Create Flutter App
3. Launch the Visual Studio Code that you have installed just now and open a new Terminal.
4. Navigate to your development folder and type “flutter create <YOUR_APP_NAME>” to create a new flutter app with the default template.
5. Click on the “Explorer” icon on the left panel and then click “Open Folder” to open the flutter app development folder that you have created just now.
6. If everything is all right, you should see the source code of the flutter default template as shown below.
7. Install a third party bluetooth printer plugin called “blue_print_pos” by typing “flutter pub add blue_print_pos” in Terminal. Note that this plugin only supports Android and iOS platforms.
8. Install another third party plugin called “permission_handler” to handle the Bluetooth permission.
9. Add permission to use Bluetooth for Android app at AndroidManifest.xml.
10. Add permission to use Bluetooth for iOS app at Info.plist.
11. This bluetooth printer can print image in jpg format. So you can search for any jpg image, create a new folder called “assets”, rename it to logo.jpg and put the image inside. After that, you will need to define your image file in pubspec.yaml.
12. Finally you can copy the whole main.dart into your IDE. This file contains all the code to scan, connect and print from the bluetooth printer.
13. If you want to compile your code into APK to be installed on your mobile phone. You can type “flutter build apk” in your Terminal. Then you can get your APK file at ../build/app/outputs/flutter-apk/app-release.apk.
Scan, Connect and Print from Bluetooth Printer
14. Turn on your bluetooth printer.
15. Install and launch the Flutter APK in your mobile phone. Click the search button to scan for Bluetooth devices. You will need to grant 2 permissions in order to use Bluetooth in the Flutter app. After that, it should list out all the detected bluetooth devices nearby.
16. Click on the Bluetooth Printer to connect to it. After it is connected, it will show up a “Test Print” button. Click on the button and the Bluetooth printer will start printing.
Conclusion
In this articles, you learned how to create a Flutter Android app to scan, connect and print from mobile phone to a Bluetooth Printer. You may try changing the source code to print a different image, different text or different QR code. You may even challenge yourself to test the source code on iOS device. However you will need to find a BLE Bluetooth Printer first. Please do let me know if it is working on iOS in the comment below.
You may find the full source code for the above Flutter app at GitHub.
If you found this tutorial useful, please do leave a comment. If you don’t have time for that, please at least give me a clap or better still, hold down your mouse button on the clap to give more claps, it’s free! Your support is what motivates me writing more and more contents. Thank you.