flutter_ble 0.5.3


Introduction #

FlutterBle is a bluetooth plugin for Flutter, a new mobile SDK to help developers build modern apps for iOS and Android. fork of FlutterBlue

Cross-Platform Bluetooth LE #

FlutterBle aims to offer the most from both platforms (iOS and Android).

Using the FlutterBle instance, you can scan for and connect to nearby devices (BluetoothDevice). Once connected to a device, the BluetoothDevice object can discover services (BluetoothService), characteristics (BluetoothCharacteristic), and descriptors (BluetoothDescriptor). The BluetoothDevice object is then used to directly interact with characteristics and descriptors.

Usage #

Obtain an instance #

FlutterBle flutterBlue = FlutterBle.instance;

Scan for devices #

/// Start scanning
var scanSubscription = flutterBlue.scan().listen((scanResult) {
    // do something with scan result

/// Stop scanning

Connect to a device #

/// Create a connection to the device
var deviceConnection = flutterBlue.connect(device).listen((s) {
    if(s == BluetoothDeviceState.connected) {
        // device is connected, do something

/// Disconnect from device

Discover services #

List<BluetoothService> services = await device.discoverServices();
services.forEach((service) {
    // do something with service

Read and write characteristics #

// Reads all characteristics
var characteristics = service.characteristics;
for(BluetoothCharacteristic c in characteristics) {
    List<int> value = await device.readCharacteristic(c);

// Writes to a characteristic
await device.writeCharacteristic(c, [0x12, 0x34])

Read and write descriptors #

// Reads all descriptors
var descriptors = characteristic.descriptors;
for(BluetoothDescriptor d in descriptors) {
    List<int> value = await device.readDescriptor(d);

// Writes to a descriptor
await device.writeDescriptor(d, [0x12, 0x34])

Set notifications #

await device.setNotifyValue(characteristic, true);
device.onValueChanged(characteristic).listen((value) {
    // do something with new value

Reference #

FlutterBle API #

scanStarts a scan for Bluetooth Low Energy devices.
connectEstablishes a connection to the Bluetooth Device.
stateGets the current state of the Bluetooth Adapter.
onStateChangedStream of state changes for the Bluetooth Adapter.

BluetoothDevice API #

discoverServicesDiscovers services offered by the remote device as well as their characteristics and descriptors.
servicesGets a list of services. Requires that discoverServices() has completed.
readCharacteristicRetrieves the value of a specified characteristic.
readDescriptorRetrieves the value of a specified descriptor.
writeCharacteristicWrites the value of a characteristic.
writeDescriptorWrites the value of a descriptor.
setNotifyValueSets notifications or indications on the specified characteristic.
onValueChangedNotifies when the characteristic's value has changed.
stateGets the current state of the Bluetooth Device.
onStateChangedNotifies of state changes for the Bluetooth Device.

Troubleshooting #

Scanning for service UUID's doesn't return any results #

Make sure the device is advertising which service UUID's it supports. This is found in the advertisement packet as UUID 16 bit complete list or UUID 128 bit complete list.

0.5.2 #

  • Added direct support for BLE scale protocol v1 (broadcast info)
  • Added direct suppor for Glucose Measurement BLE service

0.5.1 #

  • Upgrade Android protobuf dependencies
  • Refresh iOS build files

0.5.0 #

  • Breaking change. Migrate from the deprecated original Android Support Library to AndroidX. This shouldn't result in any functional changes, but it requires any Android apps using this plugin to also migrate if they're using the original support library.

0.4.2+1 #

  • Upgrade Android Gradle plugin to 3.3.0
  • Refresh iOS build files

0.4.2 #

  • Set the verbosity of log messages with setLogLevel
  • Updated iOS and Android project files
  • autoConnect now configurable for Android
  • Various bug fixes

0.4.1 #

  • Fixed bug where setNotifyValue wasn't properly awaitable.
  • Various UI bug fixes to example app.
  • Removed unnecessary intl dependencies in example app.

0.4.0 #

  • Breaking change. Manufacturer Data is now a Map of manufacturer ID's.
  • Service UUID's, service data, tx power level packets fixed in advertising data.
  • Example app updated to show advertising data.
  • Various other bug fixes.

0.3.4 #

  • Updated to use the latest protobuf (^0.9.0+1)
  • Updated other dependencies

0.3.3 #

  • scan withServices to filter by service UUID's (iOS)
  • Error handled when trying to scan with adapter off (Android)

0.3.2 #

  • Runtime permissions for Android
  • scan withServices to filter by service UUID's (Android)
  • Scan mode can be specified (Android)
  • Now targets the latest android SDK
  • Dart 2 compatibility

0.3.1 #

  • Now allows simultaneous notifications of characteristics
  • Fixed bug on iOS that was returning discoverServices too early

0.3.0 #

  • iOS support added
  • Bug fixed in example causing discoverServices to be called multiple times
  • Various other bug fixes

0.2.4 #

  • Breaking change. Upgraded to Gradle 4.1 and Android Studio Gradle plugin 3.0.1. Older Flutter projects need to upgrade their Gradle setup as well in order to use this version of the plugin. Instructions can be found here.

0.2.3 #

  • Bug fixes

0.2.2 #

  • Breaking changes:
  • startScan renamed to scan
  • ScanResult now returns a BluetoothDevice
  • connect now takes a BluetoothDevice and returns Stream
  • Added parameter timeout to connect
  • Automatic disconnect on deviceConnection.cancel()

0.2.1 #

  • Breaking change. Removed stopScan from API, use scanSubscription.cancel() instead
  • Automatically stops scan when startScan subscription is canceled (thanks to @brianegan)
  • Added timeout parameter to startScan
  • Updated example app to show new scan functionality

0.2.0 #

  • Added state and onStateChanged for BluetoothDevice
  • Updated example to show new functionality

0.1.1 #

  • Fixed image for pub.dartlang.org

0.1.0 #

  • Characteristic notifications/indications.
  • Merged in Guid library, removed from pubspec.yaml.

0.0.1 - September 1st, 2017 #

  • Initial Release.


flutter_ble_example #

Demonstrates how to use the flutter_ble plugin.

Getting Started #

This project is a starting point for a Flutter application.

A few resources to get you started if this is your first Flutter project:

For help getting started with Flutter, view our online documentation, which offers tutorials, samples, guidance on mobile development, and a full API reference.

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:

  flutter_ble: ^0.5.3

2. Install it

You can install packages from the command line:

with Flutter:

$ flutter pub get

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:

import 'package:flutter_ble/flutter_ble.dart';
Describes how popular the package is relative to other packages. [more]
Code health derived from static analysis. [more]
Reflects how tidy and up-to-date the package is. [more]
Weighted score of the above. [more]
Learn more about scoring.

We analyzed this package on Feb 18, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.7.1
  • pana: 0.13.5
  • Flutter: 1.12.13+hotfix.7

Health suggestions

Fix lib/src/bluetooth_descriptor.dart. (-0.50 points)

Analysis of lib/src/bluetooth_descriptor.dart reported 1 hint:

line 8 col 21: Name non-constant identifiers using lowerCamelCase.

Fix lib/src/flutter_ble.dart. (-0.50 points)

Analysis of lib/src/flutter_ble.dart reported 1 hint:

line 25 col 35: This function has a return type of 'Future', but doesn't end with a return statement.

Fix lib/src/helpers.dart. (-0.50 points)

Analysis of lib/src/helpers.dart reported 1 hint:

line 8 col 9: The value of the local variable 'deviceKind' isn't used.

Fix additional 7 files with analysis or formatting issues.

Additional issues in the following files:

  • lib/gen/flutterblue.pb.dart (Run flutter format to format lib/gen/flutterblue.pb.dart.)
  • lib/gen/flutterblue.pbenum.dart (Run flutter format to format lib/gen/flutterblue.pbenum.dart.)
  • lib/gen/flutterblue.pbjson.dart (Run flutter format to format lib/gen/flutterblue.pbjson.dart.)
  • lib/gen/flutterblue.pbserver.dart (Run flutter format to format lib/gen/flutterblue.pbserver.dart.)
  • lib/src/bluetooth_service.dart (Run flutter format to format lib/src/bluetooth_service.dart.)
  • lib/src/constants.dart (Run flutter format to format lib/src/constants.dart.)
  • lib/src/guid.dart (Run flutter format to format lib/src/guid.dart.)

Maintenance issues and suggestions

Homepage URL doesn't exist. (-20 points)

At the time of the analysis the homepage field https://gitlab.longev.ai/open-source/flutter_ble was unreachable.

Support latest dependencies. (-10 points)

The version constraint in pubspec.yaml does not support the latest published versions for 1 dependency (protobuf).

The package description is too short. (-20 points)

Add more detail to the description field of pubspec.yaml. Use 60 to 180 characters to describe the package, what it does, and its target use case.


Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
convert ^2.1.1 2.1.1
flutter 0.0.0
protobuf ^0.13.11 0.13.16+1 1.0.1
Transitive dependencies
charcode 1.1.3
collection 1.14.11 1.14.12
fixnum 0.10.11
meta 1.1.8
sky_engine 0.0.99
typed_data 1.1.6
vector_math 2.0.8
Dev dependencies