Bluetooth Functionality with Flutter

Author image By Manish Patel  |  Monday, September 30, 2019 04:36 PM  |  3 min read  |   1054

Using Bluetooth Functionality

When it comes to using Bluetooth along with flutter, there is close to no documentation available. Whatever few articles are available, they are either too technical to bog you down, or are not clearly explained and become overwhelming. We will help you by demonstrating some basic concepts to implement Bluetooth functionality with flutter in your app.

We will be using the “flutter Bluetooth serial” plugin in the app to add Bluetooth. This plugin is implemented from another parent plugin called “flutter blue”. Although it is a new plugin and is not entirely bug-free minds you it will definitely do your job.

Note: Before moving forward it would be important for you to know that this plugin will work only for android devices.

Implementations:

Bluetooth plugin for flutter can be easily used. Just follow these steps:

Firstly, add this dependency in your “pubspec.yaml” file:

Dependency

The code of the app in the “main.dart” file will look like this:

main.dart

Now we will create a StatefulWidget called the “Bluetooth App”. Then we will define some variables and a key in the _BluetoothAppStore. Along with this, we will also get an instance of FlutterBluetoothSerial in this class.

This class will enable to control and retrieve Bluetooth information:

StatefulWidget

Now we will be implementing the critical portion of the app. We have to get the list of paired Bluetooth devices and check whether Bluetooth is connected. This will be done asynchronously. Then we will also have to create a list of Bluetooth devices to be shown in the UI later.

These operations must be done in a “future” method, which will be called in initState().

Now it is time to move on to the UI, which is the most pretty part of flutter. The code will be pretty long but it would mostly be easy to read if you are somewhat familiar with the Flutter widget. Post completing the UI, you will have to implement some methods.

Now it is time to implement some methods. First, we will start with the _getDeviceItems() method:

Now that we have dealt with the UI, it’s time to use 4 methods. We will be using the connect and disconnect methods for this example. Then we will be implementing a method to display a snack bar to the user when no Bluetooth devices are connected when the user tries to connect.

Now we are mostly finished. We are left with two methods, one for sending a message when to turn on the Bluetooth and the other foe switching it off.

Bluetooth and the other foe

After failing all these steps, the Dart work required to make this work is now complete. But still now, when we run the app, it will crash:

Dart

To fix this, we are required to add the SDK to the AndroidManifest. Follow these steps after navigating to your project folder: android -> app -> src -> main -> AndroidManifest.xml

Add these 2 lines of code to your AndroidManifest.xml file:

AndroidManifest.xml

If you want world-class flutter services then you can hire flutter developers from India. Concetto labs have in-house flutter app developers that are available to make your dream projects a reality.

 

Looking For Flutter App Developer

That’s exactly what we do

Contact Us

 

 


Author image

Manish Patel

Manish Patel is a Co-Founder of Concetto Labs, a leading mobile app development company specialized in android and iOS app development. We provide a one-stop solution for all IT related services.

Why Our Client Love Us?

Our integrity and process focuses largely on providing every customer the best recommendations for their respective business. Our clients become recurring customers because we always go beyond their expectations to deliver the best solutions.

about-us-strategies

We brainstorm a lot!

Because brainstorming leads to new thoughts & ideas. We believe in discussing & bridging the gap leading to nicer suggestions & application.

about-us-strategies

Believe in Innovation!

Innovation is like Jugaad. It only comes when you give it a try. We believe that extraordinary things come with innovation which help you stand & lead in the crowd.

about-us-strategies

Creative Developers!

Our developers are always keen to develop creative ideas. There is “NEVER a NO.” They have an eye on the market facts, thus develop in the latest on going environment.

about-us-strategies

Value for Money!

We understand the value of money & thus with a modular approach serve you the best quote for your application & web services. The features we promise, we deliver. #notohiddencosts

Trusted By

webcore
sandvik
AArete
ladyboss
IHG
Lucidya
i.am
Zs
MB Aerospace
intercontinetal Hotel & Resort
NTPC
Wipro
neo
Coupay