Flutter Bluetooth Low Energy

Eric Pietrowicz
3 min readMay 13, 2020

Developing native looking cross platform applications to communicate with Bluetooth Low Energy device makes it really easy to spin up an array of IoT projects.

We’ll be using the FlutterBlue package to handle the low level BLE stack calls. The example provided in the documentation is a little hard to follow so we’ll walk through a quick easy way to get interface with your IoT project using Flutter.

For now, during BLE development, you’ll need a physical device to deploy your development code to. A mobile emulator will not work as it cannot access Bluetooth.

First, let’s start out with a new project and add the FlutterBlue package to the pubspec.yaml file. Make sure to double check the official documentation for the latest version, but the dependencies should now look like this:

Now import the FlutterBlue package on the top of you main.dart file.

import ‘package:flutter_blue/flutter_blue.dart’;

Lastly, if developing on Android, we’ll need to updated the minSdk for this project as the library requires a version greater than 19 and the template code defaults to 16.

Navigate to android > app > build.gradle

Under defaultConfig {} update the minSdkVersion to 19.

Perfect, now we’re ready to start re-tooling the starter code to connect to your custom BLE device.

Start by removing the variable int _counter = 0; at the top of your homepage widget and replace it with an instance of FlutterBlue:

final FlutterBlue flutterBlue = FlutterBlue.instance;

Then, we’ll gut the _incrementCounter() function and fill it with our BLE scanner code. Delete all of the existing code and replace it with the following:

One thing to notice is the line: if (result.device.name == “customGlow”) this is the name of my BLE device I want to connect to. It’s likely that you’ll have a number of Bluetooth devices nearby that your phone will identify during its scan. You only want to connect to your custom BLE device, so ignore everything else.

You’ll notice that _connectDevice(); is not yet defined. Let’s do that now just above our scanning function.

Again, you’ll notice that _getLEDChar(); is not defined. This is the function that will run through the list of Bluetooth services that our BLE device is advertising and connect only to the services and characteristics that we’re interested in.

Let’s define _getLEDChar(); now:

Now we’re getting close. There are only a few things left to define. You’ll see that _myService, _myChar, and _ledChar are still undefined. Just underneath where we defined the instance of FlutterBlue, let’s add those variables now:

Let’s talk about these values. _myService and _myChar are something that is defined on our custom IoT device that we are trying to connect to.

Basically the flow goes like this:

  1. Look for any device advertising with a name of “customGlow”
  2. Connect to that device
  3. Grab all of the services “customGlow” has
  4. If any of that list of services is what I’m looking for, grab that service’s characteristics
  5. If any of that list of characteristics is what I’m looking for, assign it to a variable of type BluetoothCharacteristic named _ledChar

Now that we have the our BluetoothCharacteristic _ledChar, we can do whatever we like, whether it’s a read or a write command. Let’s add a new button to write a value to our _ledChar.

In the body of our build function remove the two Text widgets displaying the counter variable (that we deleted). Replace these two widgets with a FloatingActionButton() that writes to our _ledChar when pressed:

The write command takes a list of 8bit integers. My characteristic is configured for a 32 bit integer, so I’m writing four different 8bit values.

Now we can press a button to connect to our device and write a value to its characteristics.

Checkout the full code for this tutorial below:

--

--