flutter_blue 0.6.3+1

  • Readme
  • Changelog
  • Example
  • Installing
  • 98

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.

Alpha version #

This library is actively developed alongside production apps, and the API will evolve as we continue our way to version 1.0.

Please be fully prepared to deal with breaking changes.

Having trouble adapting to the latest API? I'd love to hear your use-case, please contact me.

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
flutterBlue.startScan(timeout: Duration(seconds: 4));

// Listen to scan results
var subscription = flutterBlue.scanResults.listen((scanResult) {
    // do something with scan result
    device = scanResult.device;
    print('${device.name} found! rssi: ${scanResult.rssi}');
});

// Stop scanning
flutterBlue.stopScan();

Connect to a device #

// Connect to the device
await device.connect();

// Disconnect from device
device.disconnect();

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

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

Read and write descriptors #

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

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

Set notifications and listen to changes #

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

Read the MTU and request larger size #

final mtu = await device.mtu.first;
await device.requestMtu(512);

Note that iOS will not allow that you request the MTU size, but will always try to negotiate the highest possible MTU (iOS supports up to MTU size 185)

Reference #

FlutterBlue API #

AndroidiOSDescription
scanStarts a scan for Bluetooth Low Energy devices.
stateStream of state changes for the Bluetooth Adapter.
isAvailableChecks whether the device supports Bluetooth.
isOnChecks if Bluetooth functionality is turned on.

BluetoothDevice API #

AndroidiOSDescription
connectEstablishes a connection to the device.
disconnectCancels an active or pending connection to the device.
discoverServicesDiscovers services offered by the remote device as well as their characteristics and descriptors.
servicesGets a list of services. Requires that discoverServices() has completed.
stateStream of state changes for the Bluetooth Device.
mtuStream of mtu size changes.
requestMtuRequest to change the MTU for the device.

BluetoothCharacteristic API #

AndroidiOSDescription
readRetrieves the value of the characteristic.
writeWrites the value of the characteristic.
setNotifyValueSets notifications or indications on the characteristic.
valueStream of characteristic's value when changed.

BluetoothDescriptor API #

AndroidiOSDescription
readRetrieves the value of the descriptor.
writeWrites the value of the descriptor.

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.6.3+1 #

  • Fix compilation issue with iOS
  • Bump protobuf version to 1.0.0

0.6.3 #

  • Update project files for Android and iOS
  • Remove dependency on protoc for iOS

0.6.2 #

  • Add mtu and requestMtu to BluetoothDevice

0.6.0+4 #

  • Fix duplicate characteristic notifications when connection lost.
  • Fix duplicate characteristic notifications when reconnecting.
  • Add minimum SDK version of 18 for the plugin.
  • Documentation updates

0.6.0 #

  • Breaking change. API refactoring with RxDart (see example)
  • Log a more detailed warning at build time about the previous AndroidX migration.
  • Ensure that all channel calls to the Dart side from the Java side are done on the UI thread. This change allows Transactions to work with upcoming Engine restrictions, which require channel calls be made on the UI thread. Note this is an Android only change, the iOS implementation was not impacted.

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.

example/README.md

flutter_blue_example #

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


dependencies:
  flutter_blue: ^0.6.3+1

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';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
96
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
98
Learn more about scoring.

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

  • Dart: 2.6.0
  • pana: 0.12.21
  • Flutter: 1.9.1+hotfix.6

Platforms

Detected platforms: Flutter

References Flutter, and has no conflicting libraries.

Health suggestions

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

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

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

Format lib/gen/flutterblue.pb.dart.

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

Format lib/gen/flutterblue.pbenum.dart.

Run flutter format to format lib/gen/flutterblue.pbenum.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.

Dependencies

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 ^1.0.0 1.0.1
rxdart ^0.22.0 0.22.6
Transitive dependencies
charcode 1.1.2
collection 1.14.11 1.14.12
fixnum 0.10.11
meta 1.1.7 1.1.8
sky_engine 0.0.99
typed_data 1.1.6
vector_math 2.0.8
Dev dependencies
flutter_test