Skip to main content

simpledartble 1.4.0 copy "simpledartble: ^1.4.0" to clipboard
simpledartble: ^1.4.0 copied to clipboard

SimpleBLE for Dart. Discover and connect Bluetooth peripherals, read and write GATT values, subscribe to notifications, and host local GATT services through one API.

SimpleBLE

SimpleDartBLE #

Build Bluetooth once. Ship it everywhere.

SimpleDartBLE brings SimpleBLE to Dart programs on Windows, macOS, and Linux. Discover and connect Bluetooth peripherals, read and write GATT values, subscribe to notifications, and host local GATT services through one API, with futures and streams. Flutter apps use the same API through SimpleFltBLE.

Why SimpleDartBLE #

  • One cross-platform API: Keep your Bluetooth code the same on Windows, macOS, and Linux, and share it with Flutter apps on Android and iOS through SimpleFltBLE.
  • Native underneath: Use WinRT, CoreBluetooth, and BlueZ through one maintained stack.
  • Built for real products: Act as a central or a peripheral on every supported platform.
  • Commercially supported: Ship proprietary applications with commercial licensing and direct technical support from the team that builds SimpleBLE.

Support and resources #

Get started #

Use Dart 3.10 or newer:

dart pub add simpledartble

The package includes the native SimpleBLE library for every supported platform, so nothing is compiled during your build. In a Flutter app, add SimpleFltBLE instead.

Scan for five seconds and print what was found:

import 'package:simpledartble/simpledartble.dart';

Future<void> main() async {
  final adapter = (await Adapter.getAdapters()).first;

  adapter.onScanFound.listen((peripheral) {
    print('${peripheral.identifier} [${peripheral.address}] ${peripheral.rssi} dBm');
  });
  await adapter.scanFor(const Duration(seconds: 5));
}

Permissions #

On macOS, Bluetooth permission belongs to the app that runs your program, such as your terminal, and macOS asks for it the first time. On Linux, SimpleDartBLE talks to BlueZ over the D-Bus system bus, so the user needs access to it. See Bluetooth permissions for each platform's setup.

Scanning and events #

Events are broadcast streams. An adapter has onScanStart, onScanStop, onScanFound, onScanUpdated, onPowerOn, and onPowerOff; a peripheral has onConnected and onDisconnected. Events arrive in the order they happened.

Use scanStart() and scanStop() when your application controls scan timing, and scanGetResults() for the peripherals seen by the last scan. An adapter always gives you the same Peripheral object for the same device, whether it came from an event or from a list.

Calls that talk to the Bluetooth stack return futures and never block your isolate, and calls on one object run in the order they were made. Data the library already holds, such as a peripheral's identifier, rssi, or manufacturerData(), is returned directly. An active scan, an open connection, or a started local peripheral keeps a Dart program running until it ends.

Connect and use GATT #

Read the battery level of a peripheral and follow its notifications:

const batteryService = '0000180f-0000-1000-8000-00805f9b34fb';
const batteryLevel = '00002a19-0000-1000-8000-00805f9b34fb';

final peripheral = adapter.scanGetResults().firstWhere((p) => p.identifier == 'My Sensor');

peripheral.onDisconnected.listen((_) => print('Disconnected'));
await peripheral.connect();

final level = await peripheral.read(batteryService, batteryLevel);
print('Battery: ${level[0]}%');

final levels = await peripheral.notify(batteryService, batteryLevel);
levels.listen((value) => print('Battery: ${value[0]}%'));

services() lists the services, characteristics, capabilities, and descriptors of a connected peripheral. read, writeRequest, and writeCommand access characteristic values; readDescriptor and writeDescriptor access descriptors. notify and indicate return a stream of values once subscribed, which ends on unsubscribe or a disconnection. Failures are SimpleDartBleExceptions with SimpleBLE's error code in code.

Host a GATT service #

Create a local peripheral from an adapter, add services and characteristics, then start advertising:

final host = adapter.createLocalPeripheral();
host.addAdvertisedService(batteryService);

final battery = host
    .addService(batteryService)
    .addCharacteristic(batteryLevel, {CharacteristicCapability.read, CharacteristicCapability.notify});
await battery.setValue(Uint8List.fromList([100]));

host.onClientConnected.listen((address) => print('$address connected'));
await host.start();

setValue updates the stored value and publishes it to subscribers. Characteristics have onWrite, onSubscribed, and onUnsubscribed, and setReadHandler computes a value for each read. Configure services before start(); call stop() before changing them.

Use a Dongl #

A Dongl is a USB Bluetooth adapter made for SimpleBLE. Its backend is on by default on Windows, macOS, and Linux; use the adapters of the Dongl backend:

final dongl = await Backend.getDongl();
final adapter = (await dongl.getAdapters()).first;

// ...

await adapter.close();

close() stops the adapter's scan and local peripherals, disconnects its peripherals, and frees the Dongl for other programs.

Config holds the Dongl, BlueZ, WinRT, and Android backend settings; set them before the first call that reaches a backend. Pairing through a Dongl happens without the operating system: set setPasskeyRequestHandler, setPasskeyDisplayHandler, or setNumericComparisonHandler on the peripheral before connect().

Pair on Linux #

On Linux, an adapter answers BlueZ's pairing requests for the peripherals it connects to and the clients of its local peripherals. For pairing with a passkey or numeric comparison, set Config.simpleBluez.interactivePairing = true before the first call that reaches the backend, then set setPasskeyRequestHandler, setPasskeyDisplayHandler, setNumericComparisonHandler, or setPairingAuthorizationHandler on the adapter. Each handler gets the peer's address:

Config.simpleBluez.interactivePairing = true;

final adapter = (await Adapter.getAdapters()).first;
await adapter.setNumericComparisonHandler((peer, passkey) async {
  stdout.write('Does $peer show $passkey? [y/N] ');
  return stdin.readLineSync() == 'y';
});

Platforms #

Platform Architectures Requirements
macOS 15.0 or newer x64, arm64 Bluetooth permission for the app that runs the program
Windows 10 or newer x64, arm64
Linux with glibc 2.28 or newer x64, arm64 BlueZ and the D-Bus library

Flutter apps on Android, iOS, macOS, Windows, and Linux use SimpleFltBLE.

Versions #

SimpleDartBLE follows SimpleBLE's release versions, X.Y.Z.

License #

SimpleDartBLE follows the SimpleBLE license, the Business Source License 1.1 (BUSL-1.1). It is free for non-commercial use, and commercial use needs a commercial license. We offer free commercial licenses for small projects and early-stage companies: email us or leave us a message to discuss your use case. Commercial licenses carry no royalties or revenue sharing; see the licensing page for terms.


SimpleBLE is a project powered by The California Open Source Company.

0
likes
145
points
1.09k
downloads

Documentation

Documentation
API reference

Publisher

verified publishersimpleble.org

Weekly Downloads

SimpleBLE for Dart. Discover and connect Bluetooth peripherals, read and write GATT values, subscribe to notifications, and host local GATT services through one API.

Homepage
Repository (GitHub)
View/report issues
Contributing

Topics

#bluetooth #ble #ffi

License

unknown (license)

Dependencies

code_assets, ffi, hooks, meta

More

Packages that depend on simpledartble