Tuesday, September 26, 2023

Easy way to communicate your flutter apps with bluetooth

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((results) {
    // do something with scan results
    for (ScanResult r in results) {
        print('${r.device.name} found! rssi: ${r.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 requests of MTU size, and will always try to negotiate the highest possible MTU (iOS supports up to MTU size 185)

Reference

FlutterBlue API

AndroidiOSDescription
scan✅✅Starts a scan for Bluetooth Low Energy devices.
state✅✅Stream of state changes for the Bluetooth Adapter.
isAvailable✅✅Checks whether the device supports Bluetooth.
isOn✅✅Checks if Bluetooth functionality is turned on.

BluetoothDevice API

AndroidiOSDescription
connect✅✅Establishes a connection to the device.
disconnect✅✅Cancels an active or pending connection to the device.
discoverServices✅✅Discovers services offered by the remote device as well as their characteristics and descriptors.
services✅✅Gets a list of services. Requires that discoverServices() has completed.
state✅✅Stream of state changes for the Bluetooth Device.
mtu✅✅Stream of mtu size changes.
requestMtu✅Request to change the MTU for the device.

BluetoothCharacteristic API

AndroidiOSDescription
read✅✅Retrieves the value of the characteristic.
write✅✅Writes the value of the characteristic.
setNotifyValue✅✅Sets notifications or indications on the characteristic.
value✅✅Stream of characteristic’s value when changed.

BluetoothDescriptor API

AndroidiOSDescription
read✅✅Retrieves the value of the descriptor.
write✅✅Writes the value of the descriptor.