flutter_blue 0.5.0

  • README.md
  • CHANGELOG.md
  • Example
  • Installing
  • Versions
  • 93

pub package


FlutterBlue



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
scanSubscription.cancel();

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
deviceConnection.cancel();

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);
    print(value);
}

// 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);
    print(value);
}

// 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 #

AndroidiOSDescription
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 #

AndroidiOSDescription
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<BluetoothDeviceState>
  • 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.

example/README.md

flutter_blue_example #

Demonstrates how to use the flutter_blue 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:


dependencies:
  flutter_blue: ^0.5.0

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_blue/flutter_blue.dart';
  
Version Uploaded Documentation Archive
0.5.0 Feb 4, 2019 Go to the documentation of flutter_blue 0.5.0 Download flutter_blue 0.5.0 archive
0.4.2 Dec 29, 2018 Go to the documentation of flutter_blue 0.4.2 Download flutter_blue 0.4.2 archive
0.4.1 Oct 9, 2018 Go to the documentation of flutter_blue 0.4.1 Download flutter_blue 0.4.1 archive
0.4.0 Aug 11, 2018 Go to the documentation of flutter_blue 0.4.0 Download flutter_blue 0.4.0 archive
0.3.3 Apr 3, 2018 Go to the documentation of flutter_blue 0.3.3 Download flutter_blue 0.3.3 archive
0.3.2 Apr 3, 2018 Go to the documentation of flutter_blue 0.3.2 Download flutter_blue 0.3.2 archive
0.3.1 Mar 9, 2018 Go to the documentation of flutter_blue 0.3.1 Download flutter_blue 0.3.1 archive
0.3.0 Mar 7, 2018 Go to the documentation of flutter_blue 0.3.0 Download flutter_blue 0.3.0 archive
0.2.4 Feb 9, 2018 Go to the documentation of flutter_blue 0.2.4 Download flutter_blue 0.2.4 archive
0.2.3 Oct 11, 2017 Go to the documentation of flutter_blue 0.2.3 Download flutter_blue 0.2.3 archive

All 16 versions...

Popularity:
Describes how popular the package is relative to other packages. [more]
96
Health:
Code health derived from static analysis. [more]
98
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
80
Overall:
Weighted score of the above. [more]
93
Learn more about scoring.

We analyzed this package on May 22, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.3.1
  • pana: 0.12.16
  • Flutter: 1.5.4-hotfix.2

Platforms

Detected platforms: Flutter

References Flutter, and has no conflicting libraries.

Health suggestions

Fix lib/gen/flutterblue.pbenum.dart. (-1.99 points)

Analysis of lib/gen/flutterblue.pbenum.dart reported 4 hints:

line 11 col 7: Name types using UpperCamelCase.

line 39 col 7: Name types using UpperCamelCase.

line 61 col 7: Name types using UpperCamelCase.

line 79 col 7: Name types using UpperCamelCase.

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.

Format lib/gen/flutterblue.pb.dart.

Run flutter format to format lib/gen/flutterblue.pb.dart.

Format lib/gen/flutterblue.pbjson.dart.

Run flutter format to format lib/gen/flutterblue.pbjson.dart.

Format lib/gen/flutterblue.pbserver.dart.

Run flutter format to format lib/gen/flutterblue.pbserver.dart.

Maintenance suggestions

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.

Dependencies

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.10.5 0.10.8 0.13.11
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
flutter_test