bluetooth 0.0.2

  • Example
  • Installing
  • Versions
  • 38

pub package


Introduction #

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

Cross-Platform Bluetooth LE #

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

Using the FlutterBlue 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 #

FlutterBlue flutterBlue = FlutterBlue.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 #

FlutterBlue 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.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

0.1.0 #

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

0.0.1 - September 1st, 2017 #

  • Initial Release.


bluetooth_example #

Demonstrates how to use the bluetooth plugin.

Getting Started #

For help getting started with Flutter, view our online documentation.

Use this package as a library

1. Depend on it

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

  bluetooth: ^0.0.2

2. Install it

You can install packages from the command line:

with pub:

$ pub get

with Flutter:

$ flutter pub get

Alternatively, your editor might support pub get or 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:bluetooth/flutter_blue.dart';
import 'package:bluetooth/gen/flutterblue.pb.dart';
import 'package:bluetooth/gen/flutterblue.pbenum.dart';
import 'package:bluetooth/gen/flutterblue.pbjson.dart';
import 'package:bluetooth/gen/flutterblue.pbserver.dart';
Version Uploaded Documentation Archive
0.0.2 Mar 18, 2019 Go to the documentation of bluetooth 0.0.2 Download bluetooth 0.0.2 archive
0.0.1 Mar 12, 2019 Go to the documentation of bluetooth 0.0.1 Download bluetooth 0.0.1 archive
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 Jun 26, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.3.2
  • pana: 0.12.18
  • Flutter: 1.5.4-hotfix.2


Detected platforms:

Error(s) prevent platform classification:

Error(s) in lib/flutter_blue.dart: Target of URI doesn't exist: 'package:uuid_enhanced/uuid.dart'.

Health issues and suggestions

Fix lib/src/bluetooth_device.dart. (-99.68 points)

Analysis of lib/src/bluetooth_device.dart failed with 20 errors, including:

line 8 col 9: Undefined class 'DeviceIdentifier'.

line 16 col 18: Undefined class 'DeviceIdentifier'.

line 22 col 20: Undefined name 'FlutterBlue'.

line 30 col 11: Undefined name 'FlutterBlue'.

line 39 col 12: Undefined name 'FlutterBlue'.

Fix lib/src/bluetooth_characteristic.dart. (-89.99 points)

Analysis of lib/src/bluetooth_characteristic.dart failed with 8 errors, including:

line 8 col 9: Undefined class 'Uuid'.

line 12 col 37: Undefined class 'Uuid'.

line 19 col 41: Undefined name 'Uuid'.

line 34 col 28: Undefined class 'Uuid'.

line 49 col 9: Undefined class 'Uuid'.

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

Analysis of lib/src/bluetooth_descriptor.dart failed with 6 errors, 1 hint, including:

line 8 col 16: Undefined class 'Uuid'.

line 8 col 28: Undefined name 'Uuid'.

line 10 col 9: Undefined class 'Uuid'.

line 11 col 9: Undefined class 'Uuid'.

line 22 col 16: Undefined name 'Uuid'.

Fix additional 7 files with analysis or formatting issues. (-114.38 points)

Additional issues in the following files:

  • lib/src/bluetooth_service.dart (4 errors)
  • lib/flutter_blue.dart (2 errors, 1 hint)
  • lib/gen/flutterblue.pbenum.dart (4 hints)
  • lib/gen/flutterblue.pb.dart (Run flutter format to format lib/gen/flutterblue.pb.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/flutter_blue.dart ()

Maintenance issues and suggestions

Fix platform conflicts. (-20 points)

Error(s) prevent platform classification:

Error(s) in lib/flutter_blue.dart: Target of URI doesn't exist: 'package:uuid_enhanced/uuid.dart'.

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

At the time of the analysis the homepage field was unreachable.

Support latest dependencies. (-5 points)

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

Fix analysis_options.yaml.

The analyzer can't parse analysis_options.yaml.

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 is pre-v0.1 release. (-10 points)

While nothing is inherently wrong with versions of 0.0.*, it might mean that the author is still experimenting with the general direction of the API.


Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0-dev.68.0 <3.0.0
convert ^2.1.1 2.1.1
flutter 0.0.0
protobuf ^0.13.4 0.13.12
Transitive dependencies
charcode 1.1.2
collection 1.14.11
fixnum 0.10.9
meta 1.1.6 1.1.7
sky_engine 0.0.99
typed_data 1.1.6
vector_math 2.0.8
Dev dependencies
perfectionist ^2.0.0
uuid_enhanced ^3.0.2