This article will help you use Bluetooth functionality with Flutter.
There is little documentation to no documentation on using Bluetooth in Flutter. In this article, I will help you by demonstrating some basic concepts needed to implement Bluetooth functionality in your app.
Firstly, plugin/dependency we will be using in this app to add Bluetooth is “flutter_bluetooth_serial”, this plugin is implemented from another parent plugin called “flutter_blue”. This is a very new plugin, the only plugin for bluetooth available as of now. It contains a few bugs but trust me, this will surely get your job done for most basic projects.
Note: Before we go any further, it is worth mentioning that this plugin will only work for Android
Add this dependency in your “pubspec.yaml” file :
In the “main.dart” file the base code of the app will look like this:
Now, let’s create a
StatefulWidget called “BluetoothApp”. In
_BluetoothAppState, we need to define some variables and a Key. We also have to get an instance of
FlutterBluetoothSerial in this class. This class will allow us to control and retrieve Bluetooth information.
Now, it’s time for implementing the critical portion of the app. We have to get the list of Paired Bluetooth devices and check whether the Bluetooth is connected. This is done asynchronously. We also have to create a list of devices, to be shown in the UI later.
These operations should be done in a “Future” method, which should be called from
Time to move on to the UI , the most beautiful part of Flutter coding. The code would be a little bit long but it would mostly contain easily readable code, if you are somewhat familiar with the Flutter Widgets. After completing this UI, we have to implement some methods.
So, now it’s time for implementing the remaining methods. At first let us start with the
With the UI out of the way, we are left with four methods. For this example, we will be implementing the
disconnect methods. We’ll also implement a method to display a “SnackBar” to the user if there are no Bluetooth device is selected when the user tries to connect.
At this point, we are almost finished. We are now left with two methods, one for sending a message to turn on Bluetooth and the other for sending a message to turn off Bluetooth.
That’s it! the Dart code required to make this work is now complete. That said, if we try running our app it will crash:
To fix this, we need to add the
sdk to the
AndroidManifest. Navigate to your project folder and follow these steps: android -> app -> src -> main -> AndroidManifest.xml
Add these two lines of code in your “AndroidManifest.xml” file :
As I said at the beginning of this article, this plugin contains some bugs and is still under development.
Below are some screenshots showing various phases. If the user doesn’t have permission, the first thing the user will see is a prompt to grant the app location access. This is completely normal, just click “Allow” and everything should be fine.
You are free to modify the code to add more functionality to the app.
The GitHub repo link for this project is:
Using Bluetooth plugin in Flutter (flutter_bluetooth_serial) - sbis04/flutter_bluetooth
If you like this project, please give “Stars” (⭐️) to my GitHub repo.
Check out my other articles
Flutter: Getting Started
If you haven’t yet started developing apps with Flutter, you should get on-board as soon as possible because it is…
Flutter: Building WearOS app
This article will help you to build a Flutter Wear OS (Android Wear) app from scratch.
Flutter: Stateful vs Stateless Widget
In this article, I will show you what is the difference between Stateful and Stateless Widget.
If you want to support me and want me to continue writing Flutter articles and building some interesting Flutter projects, please contribute to my Patreon page below: