Skip to main content

Flutter Blue xamples

Flutter Blue xamples

Introduction

Flutter Blue is a Flutter plugin that allows you to communicate with Bluetooth Low Energy (BLE) devices. It provides an easy way to scan for nearby devices, connect to them, discover services and characteristics, and send/receive data.

In this tutorial, we will cover various examples of how to use Flutter Blue, starting from the installation process and moving on to more advanced features. Each example will include detailed step-by-step instructions and code snippets.

Installation

To get started with Flutter Blue, follow these steps:

  1. Open your Flutter project in your preferred code editor.
  2. Open the pubspec.yaml file.
  3. Add the following line under the dependencies section:
dependencies:
flutter_blue: ^0.7.3
  1. Save the file and run the following command in your terminal:
flutter packages get

Example 1: Scanning for Devices

In this example, we will demonstrate how to scan for nearby BLE devices using Flutter Blue.

  1. Import the Flutter Blue package in your Dart file:
import 'package:flutter_blue/flutter_blue.dart';
  1. Create an instance of the FlutterBlue class:
FlutterBlue flutterBlue = FlutterBlue.instance;
  1. Start scanning for nearby devices:
flutterBlue.startScan(timeout: Duration(seconds: 4));
  1. Listen for discovered devices:
flutterBlue.scanResults.listen((results) {
for (ScanResult result in results) {
print('Device found: ${result.device.name}');
}
});
  1. Stop scanning after a certain period of time:
flutterBlue.stopScan();

The code will scan for BLE devices for 4 seconds and print the name of each discovered device.

Example 2: Connecting to a Device

In this example, we will demonstrate how to connect to a specific BLE device using Flutter Blue.

  1. Scan for devices as shown in Example 1.
  2. Find the device you want to connect to:
BluetoothDevice device; // Assign the desired device
  1. Connect to the device:
device.connect();
  1. Listen for connection state changes:
device.state.listen((state) {
if (state == BluetoothDeviceState.connected) {
print('Device connected');
}
});
  1. Disconnect from the device:
device.disconnect();

The code will connect to the specified device and print "Device connected" when the connection is established.

Example 3: Discovering Services and Characteristics

In this example, we will demonstrate how to discover the services and characteristics of a connected BLE device using Flutter Blue.

  1. Connect to a device as shown in Example 2.
  2. Discover services:
List<BluetoothService> services = await device.discoverServices();
  1. Loop through the discovered services and characteristics:
for (BluetoothService service in services) {
print('Service UUID: ${service.uuid}');

for (BluetoothCharacteristic characteristic in service.characteristics) {
print('Characteristic UUID: ${characteristic.uuid}');
}
}

The code will print the UUIDs of the discovered services and characteristics of the connected device.

Example 4: Reading Characteristic Values

In this example, we will demonstrate how to read the values of a characteristic of a connected BLE device using Flutter Blue.

  1. Discover services and characteristics as shown in Example 3.
  2. Find the characteristic you want to read from:
BluetoothCharacteristic characteristic; // Assign the desired characteristic
  1. Read the value of the characteristic:
List<int> value = await characteristic.read();
print('Characteristic value: $value');

The code will read the value of the specified characteristic and print it.

Example 5: Writing Characteristic Values

In this example, we will demonstrate how to write values to a characteristic of a connected BLE device using Flutter Blue.

  1. Discover services and characteristics as shown in Example 3.
  2. Find the characteristic you want to write to:
BluetoothCharacteristic characteristic; // Assign the desired characteristic
  1. Write a value to the characteristic:
List<int> value = [0x01, 0x02, 0x03]; // Example value to write
await characteristic.write(value);
print('Value written successfully');

The code will write the specified value to the characteristic and print "Value written successfully".

Example 6: Monitoring Characteristic Notifications

In this example, we will demonstrate how to monitor notifications from a characteristic of a connected BLE device using Flutter Blue.

  1. Discover services and characteristics as shown in Example 3.
  2. Find the characteristic you want to monitor:
BluetoothCharacteristic characteristic; // Assign the desired characteristic
  1. Enable notifications for the characteristic:
await characteristic.setNotifyValue(true);
  1. Listen for notifications:
characteristic.value.listen((value) {
print('Notification received: $value');
});

The code will enable notifications for the specified characteristic and print the received notifications.

Example 7: Reading Descriptor Values

In this example, we will demonstrate how to read the values of a descriptor of a connected BLE device using Flutter Blue.

  1. Discover services and characteristics as shown in Example 3.
  2. Find the descriptor you want to read from:
BluetoothDescriptor descriptor; // Assign the desired descriptor
  1. Read the value of the descriptor:
List<int> value = await descriptor.read();
print('Descriptor value: $value');

The code will read the value of the specified descriptor and print it.

Example 8: Writing Descriptor Values

In this example, we will demonstrate how to write values to a descriptor of a connected BLE device using Flutter Blue.

  1. Discover services and characteristics as shown in Example 3.
  2. Find the descriptor you want to write to:
BluetoothDescriptor descriptor; // Assign the desired descriptor
  1. Write a value to the descriptor:
List<int> value = [0x01, 0x02, 0x03]; // Example value to write
await descriptor.write(value);
print('Value written successfully');

The code will write the specified value to the descriptor and print "Value written successfully".

Example 9: Monitoring Descriptor Notifications

In this example, we will demonstrate how to monitor notifications from a descriptor of a connected BLE device using Flutter Blue.

  1. Discover services and characteristics as shown in Example 3.
  2. Find the descriptor you want to monitor:
BluetoothDescriptor descriptor; // Assign the desired descriptor
  1. Enable notifications for the descriptor:
await descriptor.setNotifyValue(true);
  1. Listen for notifications:
descriptor.value.listen((value) {
print('Notification received: $value');
});

The code will enable notifications for the specified descriptor and print the received notifications.

Example 10: Parsing Characteristic Values

In this example, we will demonstrate how to parse the values of a characteristic of a connected BLE device using Flutter Blue.

  1. Discover services and characteristics as shown in Example 3.
  2. Find the characteristic you want to parse:
BluetoothCharacteristic characteristic; // Assign the desired characteristic
  1. Parse the value of the characteristic:
List<int> value = await characteristic.read();
int parsedValue = value[0] + (value[1] << 8);
print('Parsed value: $parsedValue');

The code will read the value of the specified characteristic, parse it, and print the parsed value.

Conclusion

In this tutorial, we covered various examples of how to use Flutter Blue for BLE communication. We learned how to scan for devices, connect to them, discover services and characteristics, read/write characteristic and descriptor values, monitor notifications, and parse characteristic values.

These examples provide a solid foundation for building Bluetooth-enabled apps using Flutter. Experiment with these examples and explore the Flutter Blue documentation to discover even more functionality.